Skip to content

Commit e570edd

Browse files
rustagirkevinAlbs
andauthored
DOCSP-31470: describe connection pool in perf page (#93)
Co-authored-by: Kevin Albertson <[email protected]>
1 parent 7a5b04c commit e570edd

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

source/fundamentals/performance.txt

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
Performance Considerations
55
==========================
66

7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, optimization, speed, memory
13+
714
.. contents:: On this page
815
:local:
916
:backlinks: none
@@ -20,6 +27,24 @@ discovering server topology, monitoring your connection, and maintaining
2027
an internal connection pool. This guide describes best practices for
2128
configuring and using your ``Client`` instance.
2229

30+
This guide includes the following sections:
31+
32+
- :ref:`Client Lifecycle <rust-performance-client-lifecycle>` describes
33+
best practices for creating and managing a ``Client`` instance
34+
35+
- :ref:`Connection Pool <rust-performance-pool>` describes
36+
how connection pooling works in the driver
37+
38+
- :ref:`Parallelism <rust-performance-parallelism>` provides sample code
39+
for running parallel, asynchronous tasks
40+
41+
- :ref:`Runtime <rust-performance-runtime>` describes how to manage
42+
runtimes by using functionalities of the ``tokio`` and ``async_std`` crates
43+
44+
- :ref:`Additional Information <rust-perf-addtl-info>`
45+
provides links to resources and API documentation for types
46+
and methods mentioned in this guide
47+
2348
.. _rust-performance-client-lifecycle:
2449

2550
Client Lifecycle
@@ -39,6 +64,90 @@ by using the same client:
3964
:language: rust
4065
:dedent:
4166

67+
.. _rust-performance-pool:
68+
69+
Connection Pool
70+
---------------
71+
72+
Every ``Client`` instance has a built-in connection pool for each server
73+
in your MongoDB topology. Connection pools open sockets on demand to
74+
support concurrent requests to MongoDB in your application. You can
75+
tune the connection pool to best fit the needs of your application
76+
and optimize performance.
77+
78+
.. tip::
79+
80+
To learn more about configuring a connection pool, see
81+
:manual:`Tuning Your Connection Pool Settings
82+
</tutorial/connection-pool-performance-tuning/>` in the Server manual.
83+
84+
The maximum size of each connection pool is set by the ``max_pool_size``
85+
option, which defaults to ``10``. If the number of in-use connections to
86+
a server reaches the value of ``max_pool_size``, the next request to
87+
that server waits until a connection becomes available.
88+
89+
In addition to the sockets needed to support your application's requests,
90+
each ``Client`` instance opens two more sockets per server
91+
in your MongoDB topology for monitoring the server's state.
92+
For example, a client connected to a three-node replica set opens six
93+
monitoring sockets. If the application uses the default setting for
94+
``max_pool_size`` and only queries the primary (default) node, then
95+
there can be at most 16 total connections in the connection pool. If the
96+
application uses a read preference to query the secondary nodes, those
97+
connection pools grow and there can be 36 total connections.
98+
99+
To support high numbers of concurrent MongoDB requests
100+
within one process, you can increase the value of the ``max_pool_size``
101+
option. The following code demonstrates how to specify a value for
102+
``max_pool_size`` when instantiating a ``Client``:
103+
104+
.. code-block:: rust
105+
106+
let mut client_options = ClientOptions::parse("<connection string>").await?;
107+
client_options.max_pool_size = Some(20);
108+
109+
let client = Client::with_options(client_options)?;
110+
111+
Connection pools are rate-limited. The ``max_connecting`` option
112+
determines the number of connections that the pool can create in
113+
parallel at any time. For example, if the value of ``max_connecting`` is
114+
``2``, the default value, the third request that attempts to concurrently
115+
check out a connection succeeds only when one of the following cases occurs:
116+
117+
- The connection pool finishes creating a connection and the number of
118+
connections in the pool is less than or equal to the value of ``max_pool_size``.
119+
- An existing connection is checked back into the pool.
120+
- The driver's ability to reuse existing connections improves due to
121+
rate limits on connection creation.
122+
123+
You can set the minimum number of concurrent connections to
124+
each server with the ``min_pool_size`` option, which defaults to ``0``.
125+
The driver initializes the connection pool with this number of sockets. If
126+
sockets are closed and the total number of sockets, both in use and
127+
idle, drops below the minimum, the connection pool opens more sockets until the
128+
minimum is reached.
129+
130+
You can set the maximum amount of time that a connection can
131+
remain idle in the pool by setting the ``max_idle_time`` option.
132+
Once a connection has been idle for the duration specified in
133+
``max_idle_time``, the connection pool removes and replaces that
134+
connection. This option defaults to ``0``, or no limit.
135+
136+
When the ``Client::shutdown()`` method is called at any point in your
137+
application, the driver closes all idle sockets and closes all sockets
138+
that are in use as they are returned to the pool. Calling ``Client::shutdown()``
139+
closes only inactive sockets, so you cannot interrupt or terminate
140+
any ongoing operations by using this method. The driver closes these
141+
sockets only when the process completes.
142+
143+
The default configuration for a ``Client`` works for most applications.
144+
The following code shows how to create a client with default connection
145+
settings:
146+
147+
.. code-block:: rust
148+
149+
let client = Client::with_uri_str("<connection string>").await?;
150+
42151
.. _rust-performance-parallelism:
43152

44153
Parallelism
@@ -88,6 +197,8 @@ ensuring that there are no unintended interactions between runtimes:
88197
:language: rust
89198
:dedent:
90199

200+
.. _rust-perf-addtl-info:
201+
91202
Additional Information
92203
----------------------
93204

@@ -101,6 +212,7 @@ API Documentation
101212
~~~~~~~~~~~~~~~~~
102213

103214
- `Client() <{+api+}/struct.Client.html>`__
215+
- `ClientOptions <{+api+}/options/struct.ClientOptions.html>`__
104216
- `spawn() <https://docs.rs/tokio/latest/tokio/task/fn.spawn.html>`__ in
105217
the ``tokio::task`` module
106218
- `tokio::runtime <https://docs.rs/tokio/latest/tokio/runtime/index.html>`__ module

0 commit comments

Comments
 (0)