Skip to content

Commit abf009a

Browse files
author
Joe Bennett
committed
#27345 Added Component/Lock/Store/MongoDbStore
1 parent d9c0793 commit abf009a

File tree

1 file changed

+83
-5
lines changed

1 file changed

+83
-5
lines changed

components/lock.rst

Lines changed: 83 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ Store Scope Blocking Expiring
220220
============================================ ====== ======== ========
221221
:ref:`FlockStore <lock-store-flock>` local yes no
222222
:ref:`MemcachedStore <lock-store-memcached>` remote no yes
223+
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes
223224
:ref:`PdoStore <lock-store-pdo>` remote no yes
224225
:ref:`RedisStore <lock-store-redis>` remote no yes
225226
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no
@@ -266,6 +267,39 @@ support blocking, and expects a TTL to avoid stalled locks::
266267

267268
Memcached does not support TTL lower than 1 second.
268269

270+
.. _lock-store-mongodb:
271+
272+
MongoDbStore
273+
~~~~~~~~~~~~
274+
275+
.. versionadded:: 4.4
276+
277+
The ``MongoDbStore`` was introduced in Symfony 4.4.
278+
279+
The MongoDbStore saves locks on a MongoDB server, it requires a
280+
``\MongoDB\Client`` connection from `mongodb/mongodb`_. This store does not
281+
support blocking and expects a TTL to avoid stalled locks::
282+
283+
use Symfony\Component\Lock\Store\MongoDbStore;
284+
285+
$mongoClient = new \MongoDB\Client('mongo://localhost/');
286+
287+
$options = [
288+
'database' => 'my-app',
289+
];
290+
291+
$store = new MongoDbStore($mongoClient, $options);
292+
293+
The ``MongoDbStore`` takes the following ``$options``:
294+
295+
============ ========= ========================================================================
296+
Option Default Description
297+
============ ========= ========================================================================
298+
database The name of the database [Mandatory]
299+
collection ``lock`` The name of the collection
300+
gcProbablity ``0.001`` Should a TTL Index be created expressed as a probability from 0.0 to 1.0
301+
============ ========= ========================================================================
302+
269303
.. _lock-store-pdo:
270304

271305
PdoStore
@@ -402,7 +436,7 @@ Remote Stores
402436
~~~~~~~~~~~~~
403437

404438
Remote stores (:ref:`MemcachedStore <lock-store-memcached>`,
405-
:ref:`PdoStore <lock-store-pdo>`,
439+
:ref:`MongoDbStore <lock-store-mongodb>`, :ref:`PdoStore <lock-store-pdo>`,
406440
:ref:`RedisStore <lock-store-redis>` and
407441
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
408442
the true owner of the lock. This token is stored in the
@@ -427,7 +461,7 @@ Expiring Stores
427461
~~~~~~~~~~~~~~~
428462

429463
Expiring stores (:ref:`MemcachedStore <lock-store-memcached>`,
430-
:ref:`PdoStore <lock-store-pdo>` and
464+
:ref:`MongoDbStore <lock-store-mongodb>`, :ref:`PdoStore <lock-store-pdo>` and
431465
:ref:`RedisStore <lock-store-redis>`)
432466
guarantee that the lock is acquired only for the defined duration of time. If
433467
the task takes longer to be accomplished, then the lock can be released by the
@@ -545,6 +579,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
545579
The method ``flush()`` must not be called, or locks should be stored in a
546580
dedicated Memcached service away from Cache.
547581

582+
MongoDbStore
583+
~~~~~~~~~~~~
584+
585+
.. caution::
586+
587+
The locked resource name is indexed in the ``_id`` field of the lock
588+
collection. Beware that in MongoDB an indexed field's value can be
589+
`a maximum of 1024 bytes in length`_ inclusive of structural overhead.
590+
591+
A TTL index MUST BE used on MongoDB 2.2+ to automatically clean up expired locks.
592+
Such an index can be created manually:
593+
594+
.. code-block:: javascript
595+
596+
db.lock.ensureIndex(
597+
{ "expires_at": 1 },
598+
{ "expireAfterSeconds": 0 }
599+
)
600+
601+
Alternatively, the method ``MongoDbStore::createTtlIndex(int $expireAfterSeconds = 0)``
602+
can be called once to create the TTL index during database setup. Read more
603+
about `Expire Data from Collections by Setting TTL`_ in MongoDB.
604+
605+
.. tip::
606+
607+
``MongoDbStore`` will attempt to automatically create a TTL index on MongoDB
608+
2.2+. It's recommended to set constructor option ``gcProbablity = 0.0`` to
609+
disable this behavior if you have manually dealt with TTL index creation.
610+
611+
.. caution::
612+
613+
This store relies on all PHP application and database nodes to have
614+
synchronized clocks for lock expiry to occur at the correct time. To ensure
615+
locks don't expire prematurely; the lock TTL should be set with enough extra
616+
time in ``expireAfterSeconds`` to account for any clock drift between nodes.
617+
618+
``writeConcern``, ``readConcern`` and ``readPreference`` are not specified by
619+
MongoDbStore meaning the collection's settings will take effect. Read more
620+
about `Replica Set Read and Write Semantics`_ in MongoDB.
621+
548622
PdoStore
549623
~~~~~~~~~~
550624

@@ -663,10 +737,14 @@ instance, during the deployment of a new version. Processes with new
663737
configuration must not be started while old processes with old configuration
664738
are still running.
665739

740+
.. _`a maximum of 1024 bytes in length`: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
666741
.. _`ACID`: https://en.wikipedia.org/wiki/ACID
742+
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
743+
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
744+
.. _`Expire Data from Collections by Setting TTL`: https://docs.mongodb.com/manual/tutorial/expire-data/
667745
.. _`locks`: https://en.wikipedia.org/wiki/Lock_(computer_science)
668-
.. _`PHP semaphore functions`: https://php.net/manual/en/book.sem.php
746+
.. _`mongodb/mongodb`: https://packagist.org/packages/mongodb/mongodb
669747
.. _`PDO`: https://php.net/pdo
670-
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
671-
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
748+
.. _`PHP semaphore functions`: https://php.net/manual/en/book.sem.php
749+
.. _`Replica Set Read and Write Semantics`: https://docs.mongodb.com/manual/applications/replication/
672750
.. _`ZooKeeper`: https://zookeeper.apache.org/

0 commit comments

Comments
 (0)