Skip to content

Commit 8c7e3af

Browse files
committed
PHPLIB-1278: FAQ entry on connection persistence
1 parent f76e0c0 commit 8c7e3af

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

source/faq.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,64 @@ for the correct environment.
101101
The aforementioned ``detect-extension`` script can also be used to determine the
102102
appropriate DLL for your PHP environment.
103103

104+
105+
Connection Handling and Persistence
106+
-----------------------------------
107+
108+
Connections to the MongoDB deployment are handled entirely by libmongoc and the
109+
:php:`mongodb extension <mongodb>`. When a :phpclass:`MongoDB\Client` is
110+
constructed, the |php-library| creates a
111+
:php:`MongoDB\Driver\Manager <class.mongodb-driver-manager>` using the
112+
same connection string and options. The extension also uses those constructor
113+
arguments to derive a hash key for persistent libmongoc clients. If a libmongoc
114+
client was previously persisted with a key, it will be reused; otherwise, a new
115+
libmongoc client will be created and persisted for the lifetime of the PHP
116+
worker process. This process is described in more detail in the
117+
:php:`extension documentation <manual/en/mongodb.connection-handling.php>`.
118+
119+
Each libmongoc client maintains its own connections to the MongoDB deployment
120+
and a view of its topology. When a persistent libmongoc client is reused, the
121+
PHP driver is able to avoid the overhead of establishing new connections and
122+
rediscovering the topology. This approach generally improves performance and is
123+
therefore the driver's default behavior.
124+
125+
Persistent libmongoc clients are not destroyed until the PHP worker process
126+
terminates. This means that connections to a MongoDB deployment may remain open
127+
long after a ``MongoDB\Driver\Manager`` object goes out of scope. While this is
128+
typically not an issue for applications that connect to one MongoDB deployment,
129+
it could be problematic in some situations. Consider the following cases:
130+
131+
- PHP-FPM is configured with ``pm.max_requests=0`` (i.e. workers never respawn)
132+
and a PHP application is deployed many times with small changes to its MongoDB
133+
connection string and/or options. This could lead to an accumulation of
134+
libmongoc client objects within each worker process.
135+
- An application occasionally connects to a separate MongoDB deployment in some
136+
backend component where request latency is not paramount.
137+
138+
In the first case, restarting PHP-FPM as part of the application deployment
139+
would allow the application to relinquish any unused libmongoc clients and still
140+
utilize a persistent client for the latest connection string.
141+
142+
The second case warrants a different solution. Specifying ``true`` for the
143+
``disableClientPersistence`` driver option will instruct the PHP driver to
144+
create a new libmongoc client and ensure it is destroyed when the corresponding
145+
``MongoDB\Driver\Manager`` goes out of scope.
146+
147+
.. code-block:: php
148+
149+
<?php
150+
151+
$client = new MongoDB\Client(
152+
uri: getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/',
153+
uriOptions: [],
154+
driverOptions: ['disableClientPersistence' => true],
155+
);
156+
157+
The ``disableClientPersistence`` driver option should be used sparingly, as
158+
opting out of client persistence means that additional time will be required to
159+
establish connections to the MongoDB deployment and discover its topology.
160+
161+
104162
Server Selection Failures
105163
-------------------------
106164

0 commit comments

Comments
 (0)