Skip to content

Commit bfe48ec

Browse files
author
Joe Bennett
committed
#27345 Added Symfony\Component\Lock\Store\MongoDbStore
1 parent 278b1e9 commit bfe48ec

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

components/lock.rst

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ Store Scope Blocking Expiring
216216
============================================ ====== ======== ========
217217
:ref:`FlockStore <lock-store-flock>` local yes no
218218
:ref:`MemcachedStore <lock-store-memcached>` remote no yes
219+
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes
219220
:ref:`PdoStore <lock-store-pdo>` remote no yes
220221
:ref:`RedisStore <lock-store-redis>` remote no yes
221222
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no
@@ -262,6 +263,39 @@ support blocking, and expects a TTL to avoid stalled locks::
262263

263264
Memcached does not support TTL lower than 1 second.
264265

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

267301
PdoStore
@@ -398,7 +432,7 @@ Remote Stores
398432
~~~~~~~~~~~~~
399433

400434
Remote stores (:ref:`MemcachedStore <lock-store-memcached>`,
401-
:ref:`PdoStore <lock-store-pdo>`,
435+
:ref:`MongoDbStore <lock-store-mongodb>`, :ref:`PdoStore <lock-store-pdo>`,
402436
:ref:`RedisStore <lock-store-redis>` and
403437
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
404438
the true owner of the lock. This token is stored in the
@@ -423,7 +457,7 @@ Expiring Stores
423457
~~~~~~~~~~~~~~~
424458

425459
Expiring stores (:ref:`MemcachedStore <lock-store-memcached>`,
426-
:ref:`PdoStore <lock-store-pdo>` and
460+
:ref:`MongoDbStore <lock-store-mongodb>`, :ref:`PdoStore <lock-store-pdo>` and
427461
:ref:`RedisStore <lock-store-redis>`)
428462
guarantee that the lock is acquired only for the defined duration of time. If
429463
the task takes longer to be accomplished, then the lock can be released by the
@@ -541,6 +575,46 @@ method uses the Memcached's ``flush()`` method which purges and removes everythi
541575
The method ``flush()`` must not be called, or locks should be stored in a
542576
dedicated Memcached service away from Cache.
543577

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

@@ -661,9 +735,13 @@ are still running.
661735

662736
.. _`ACID`: https://en.wikipedia.org/wiki/ACID
663737
.. _`locks`: https://en.wikipedia.org/wiki/Lock_(computer_science)
738+
.. _`mongodb/mongodb`: https://packagist.org/packages/mongodb/mongodb
664739
.. _Packagist: https://packagist.org/packages/symfony/lock
665740
.. _`PHP semaphore functions`: http://php.net/manual/en/book.sem.php
666741
.. _`PDO`: https://php.net/pdo
667742
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/Connection.php
668743
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
669744
.. _`ZooKeeper`: https://zookeeper.apache.org/
745+
.. _`a maximum of 1024 bytes in length`: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
746+
.. _`Expire Data from Collections by Setting TTL`: https://docs.mongodb.com/manual/tutorial/expire-data/
747+
.. _`Replica Set Read and Write Semantics`: https://docs.mongodb.com/manual/applications/replication/

0 commit comments

Comments
 (0)