Skip to content

Commit 4b3a32a

Browse files
committed
CDRIVER-1920 connection pooling guide
1 parent ebb485f commit 4b3a32a

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed

doc/connection-pooling.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
:man_page: mongoc_connection_pooling
2+
3+
Connection Pooling
4+
==================
5+
6+
The MongoDB C driver has two connection modes: single-threaded and pooled. Single-threaded mode is optimized for embedding the driver within languages like PHP. Multi-threaded programs should use pooled mode: this mode minimizes the total connection count, and in pooled mode a background thread monitors the MongoDB server topology, so the program need not block to scan it.
7+
8+
Single Mode
9+
-----------
10+
11+
In single mode, your program creates a :symbol:`mongoc_client_t` directly:
12+
13+
.. code-block:: c
14+
15+
mongoc_client_t *client = mongoc_client_new (
16+
"mongodb://hostA,hostB/?replicaSet=my_rs");
17+
18+
The client connects on demand when your program first uses it for a MongoDB operation. Using a non-blocking socket per server, it begins a check on each server concurrently, and uses the asynchronous ``poll`` or ``select`` function to receive events from the sockets, until all have responded or timed out. Put another way, in single-threaded mode the C Driver fans out to begin all checks concurrently, then fans in once all checks have completed or timed out. Once the scan completes, the client executes your program's operation and returns.
19+
20+
In single mode, the client re-scans the server topology roughly once per minute. If more than a minute has elapsed since the previous scan, the next operation on the client will block while the client completes its scan. This interval is configurable with ``heartbeatFrequencyMS`` in the connection string. (See :symbol:`mongoc_uri_t`.)
21+
22+
A single client opens one connection per server in your topology: these connections are used both for scanning the topology and performing normal operations.
23+
24+
Pooled Mode
25+
-----------
26+
27+
To activate pooled mode, create a :symbol:`mongoc_client_pool_t`:
28+
29+
.. code-block:: c
30+
31+
mongoc_uri_t *uri = mongoc_uri_new (
32+
"mongodb://hostA,hostB/?replicaSet=my_rs");
33+
34+
mongoc_client_pool_t *pool = mongoc_client_pool_new (uri);
35+
36+
When your program first calls :symbol:`mongoc_client_pool_pop`, the pool launches a background thread for monitoring. The thread fans out and connects to all servers in the connection string, using non-blocking sockets and a simple event loop. As it receives ismaster responses from the servers, it updates its view of the server topology. Each time the thread discovers a new server it begins connecting to it, and adds the new socket to the list of non-blocking sockets in the event loop.
37+
38+
Each thread that executes MongoDB operations must check out a client from the pool:
39+
40+
.. code-block:: c
41+
42+
mongoc_client_t *client = mongoc_client_pool_pop (pool);
43+
44+
/* use the client for operations ... */
45+
46+
mongoc_client_pool_push (pool, client);
47+
48+
The :symbol:`mongoc_client_t` object is not thread-safe, only the :symbol:`mongoc_client_pool_t` is.
49+
50+
When the driver is in pooled mode, your program's operations are unblocked as soon as monitoring discovers a usable server. For example, if a thread in your program is waiting to execute an "insert" on the primary, it is unblocked as soon as the primary is discovered, rather than waiting for all secondaries to be checked as well.
51+
52+
The pool opens one connection per server for monitoring, and each client opens its own connection to each server it uses for application operations. The background thread re-scans the server topology roughly every 10 seconds. This interval is configurable with ``heartbeatFrequencyMS`` in the connection string. (See :symbol:`mongoc_uri_t`.)
53+
54+
See :ref:`connection_pool_options` to configure pool size and behavior, and see :symbol:`mongoc_client_pool_t` for an extended example of a multi-threaded program that uses the driver in pooled mode.

doc/guides.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Guides
99

1010
mongoc-common-task-examples
1111
advanced-connections
12+
connection-pooling
1213
cursors
1314
bulk
1415
aggregate

doc/mongoc_client_pool_t.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mongoc_client_pool_t
44
====================
55

6-
Connection pooling abstraction
6+
A connection pool for multi-threaded programs. See :doc:`connection-pooling`.
77

88
Synopsis
99
--------

doc/mongoc_client_t.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
mongoc_client_t
44
===============
55

6-
MongoDB Connection Abstraction
6+
A single-threaded MongoDB connection. See :doc:`connection-pooling`.
77

88
Synopsis
99
--------
@@ -18,8 +18,8 @@ Synopsis
1818
void *user_data,
1919
bson_error_t *error);
2020
21-
``mongoc_client_t`` is an opaque type that provides access to a MongoDB node,
22-
replica-set, or sharded-cluster. It maintains management of underlying sockets
21+
``mongoc_client_t`` is an opaque type that provides access to a MongoDB server,
22+
replica set, or sharded cluster. It maintains management of underlying sockets
2323
and routing to individual nodes based on :symbol:`mongoc_read_prefs_t` or :symbol:`mongoc_write_concern_t`.
2424

2525
Streams

doc/mongoc_uri_t.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ MONGOC_URI_SOCKETCHECKINTERVALMS socketcheckintervalms Onl
142142

143143
Setting any of the \*TimeoutMS options above to ``0`` will be interpreted as "use the default value".
144144

145+
.. _connection_pool_options:
146+
145147
Connection Pool Options
146148
-----------------------
147149

0 commit comments

Comments
 (0)