Skip to content

Commit 381885c

Browse files
author
Joe Bennett
committed
#27345 Added Component/Lock/Store/MongoDbStore
1 parent c6138eb commit 381885c

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
@@ -225,6 +225,7 @@ Store Scope Blocking Expiring
225225
============================================ ====== ======== ========
226226
:ref:`FlockStore <lock-store-flock>` local yes no
227227
:ref:`MemcachedStore <lock-store-memcached>` remote no yes
228+
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes
228229
:ref:`PdoStore <lock-store-pdo>` remote no yes
229230
:ref:`RedisStore <lock-store-redis>` remote no yes
230231
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no
@@ -277,6 +278,39 @@ support blocking, and expects a TTL to avoid stalled locks::
277278

278279
Memcached does not support TTL lower than 1 second.
279280

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

282316
PdoStore
@@ -413,7 +447,7 @@ Remote Stores
413447
~~~~~~~~~~~~~
414448

415449
Remote stores (:ref:`MemcachedStore <lock-store-memcached>`,
416-
:ref:`PdoStore <lock-store-pdo>`,
450+
:ref:`MongoDbStore <lock-store-mongodb>`, :ref:`PdoStore <lock-store-pdo>`,
417451
:ref:`RedisStore <lock-store-redis>` and
418452
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
419453
the true owner of the lock. This token is stored in the
@@ -438,7 +472,7 @@ Expiring Stores
438472
~~~~~~~~~~~~~~~
439473

440474
Expiring stores (:ref:`MemcachedStore <lock-store-memcached>`,
441-
:ref:`PdoStore <lock-store-pdo>` and
475+
:ref:`MongoDbStore <lock-store-mongodb>`, :ref:`PdoStore <lock-store-pdo>` and
442476
:ref:`RedisStore <lock-store-redis>`)
443477
guarantee that the lock is acquired only for the defined duration of time. If
444478
the task takes longer to be accomplished, then the lock can be released by the
@@ -556,6 +590,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
556590
The method ``flush()`` must not be called, or locks should be stored in a
557591
dedicated Memcached service away from Cache.
558592

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

@@ -674,10 +748,14 @@ instance, during the deployment of a new version. Processes with new
674748
configuration must not be started while old processes with old configuration
675749
are still running.
676750

751+
.. _`a maximum of 1024 bytes in length`: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
677752
.. _`ACID`: https://en.wikipedia.org/wiki/ACID
753+
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
754+
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
755+
.. _`Expire Data from Collections by Setting TTL`: https://docs.mongodb.com/manual/tutorial/expire-data/
678756
.. _`locks`: https://en.wikipedia.org/wiki/Lock_(computer_science)
679-
.. _`PHP semaphore functions`: https://php.net/manual/en/book.sem.php
757+
.. _`mongodb/mongodb`: https://packagist.org/packages/mongodb/mongodb
680758
.. _`PDO`: https://php.net/pdo
681-
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
682-
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
759+
.. _`PHP semaphore functions`: https://php.net/manual/en/book.sem.php
760+
.. _`Replica Set Read and Write Semantics`: https://docs.mongodb.com/manual/applications/replication/
683761
.. _`ZooKeeper`: https://zookeeper.apache.org/

0 commit comments

Comments
 (0)