Skip to content

Commit 64fa7ff

Browse files
jmikolarustagir
authored andcommitted
PHPLIB-1278: FAQ entry on connection persistence
(cherry picked from commit 8c7e3af)
1 parent b9fda16 commit 64fa7ff

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
@@ -100,6 +100,64 @@ for the correct environment.
100100
The aforementioned ``detect-extension`` script can also be used to determine the
101101
appropriate DLL for your PHP environment.
102102

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

0 commit comments

Comments
 (0)