@@ -10,13 +10,11 @@ FAQ: Concurrency
10
10
:depth: 1
11
11
:class: twocols
12
12
13
- MongoDB allows multiple clients to read and write the same data.
14
- In order to ensure consistency, it uses locking and other
15
- :term:`concurrency control` measures to prevent multiple clients
16
- from modifying the same piece of data simultaneously.
17
- Together, these mechanisms guarantee that all writes to a single
18
- document occur either in full or not at all and that clients never
19
- see an inconsistent view of the data.
13
+ MongoDB allows multiple clients to read and write the same data. To
14
+ ensure consistency, MongoDB uses locking and :term:`concurrency control`
15
+ to prevent clients from modifying the same data simultaneously. Writes
16
+ to a single document occur either in full or not at all, and clients
17
+ always see consistent data.
20
18
21
19
.. _faq-dev-concurrency:
22
20
.. _faq-concurrency-locking:
@@ -29,24 +27,24 @@ What type of locking does MongoDB use?
29
27
In addition to a shared (S) locking mode for reads and an exclusive
30
28
(X) locking mode for write operations, intent shared (IS) and intent
31
29
exclusive (IX) modes indicate an intent to read or write a resource
32
- using a finer granularity lock. When locking at a certain granularity,
30
+ using a finer granularity lock. When locking at a certain granularity,
33
31
all higher levels are locked using an :term:`intent lock`.
34
32
35
33
For example, when locking a collection for writing (using mode X),
36
34
both the corresponding database lock and the global lock must be
37
35
locked in intent exclusive (IX) mode. A single database can
38
36
simultaneously be locked in IS and IX mode, but an exclusive (X)
39
37
lock cannot coexist with any other modes, and a shared (S) lock can
40
- only coexists with intent shared (IS) locks.
38
+ only coexist with intent shared (IS) locks.
41
39
42
40
.. |rarr| unicode:: 0x2192 .. right arrow
43
41
44
- Locks are fair, with reads and writes being queued in order. However,
45
- to optimize throughput, when one request is granted, all other
46
- compatible requests will be granted at the same time, potentially
47
- releasing them before a conflicting request. For example, consider
48
- a case in which an X lock was just released, and in which the
49
- conflict queue contains the following items :
42
+ Locks are fair, with lock requests for reads and writes queued in order.
43
+ However, to optimize throughput, when one lock request is granted, all
44
+ other compatible lock requests are granted at the same time, potentially
45
+ releasing the locks before a conflicting lock request is performed. For
46
+ example, consider a situation where an X lock was just released and the
47
+ conflict queue contains these locks :
50
48
51
49
IS |rarr| IS |rarr| X |rarr| X |rarr| S |rarr| IS
52
50
@@ -75,8 +73,8 @@ How granular are locks in MongoDB?
75
73
How do I see the status of locks on my :binary:`~bin.mongod` instances?
76
74
-----------------------------------------------------------------------
77
75
78
- For reporting on lock utilization information on locks, use any of the
79
- following methods:
76
+ For reporting on lock utilization information on locks, use any of these
77
+ methods:
80
78
81
79
- :method:`db.serverStatus()`,
82
80
- :method:`db.currentOp()`,
@@ -110,7 +108,7 @@ Does a read or write operation ever yield the lock?
110
108
In some situations, read and write operations can yield their locks.
111
109
112
110
Long running read and write operations, such as queries, updates,
113
- and deletes, yield under many conditions. MongoDB operations can
111
+ and deletes, yield locks under many conditions. MongoDB operations can
114
112
also yield locks between individual document modifications in write
115
113
operations that affect multiple documents like
116
114
:method:`~db.collection.update()` with the ``multi`` parameter.
@@ -209,15 +207,16 @@ use for document level locking storage engines:
209
207
Which administrative commands lock the database?
210
208
------------------------------------------------
211
209
212
- Certain administrative commands can exclusively lock the database for
213
- extended periods of time. In some deployments, for large databases,
214
- you may consider taking the :binary:`~bin.mongod` instance offline so that
215
- clients are not affected. For example, if a :binary:`~bin.mongod` is part
216
- of a :term:`replica set`, take the :binary:`~bin.mongod` offline and let
217
- other members of the set service load while maintenance is in progress.
210
+ Some administrative commands can exclusively lock the database for
211
+ extended time periods. For large database deployments, you may consider
212
+ taking the :binary:`~bin.mongod` instance offline so that clients are
213
+ not affected. For example, if a :binary:`~bin.mongod` is part of a
214
+ :term:`replica set`, take the :binary:`~bin.mongod` offline and let
215
+ other members of the replica set process requests while maintenance is
216
+ performed.
218
217
219
- The following administrative operations require an exclusive
220
- lock at the database level for extended periods:
218
+ These administrative operations require an exclusive lock at the
219
+ database level for extended periods:
221
220
222
221
.. list-table::
223
222
:header-rows: 1
@@ -261,7 +260,7 @@ lock at the database level for extended periods:
261
260
exclusive (W) lock when renaming a collection across
262
261
databases and blocks other operations until it finishes.
263
262
264
- The following administrative operations lock the database but only hold
263
+ These administrative operations lock the database but only hold
265
264
the lock for a very short time:
266
265
267
266
.. list-table::
@@ -289,8 +288,8 @@ Which administrative commands lock a collection?
289
288
290
289
.. versionchanged:: 4.2
291
290
292
- The following administrative operations require an exclusive lock at
293
- the collection level:
291
+ These administrative operations require an exclusive lock at the
292
+ collection level:
294
293
295
294
.. list-table::
296
295
:header-rows: 1
@@ -375,8 +374,8 @@ until the operation completed.
375
374
Does a MongoDB operation ever lock more than one database?
376
375
----------------------------------------------------------
377
376
378
- The following MongoDB operations may obtain and hold a lock on more
379
- than one database:
377
+ These MongoDB operations may obtain and hold a lock on more than one
378
+ database:
380
379
381
380
.. list-table::
382
381
:header-rows: 1
@@ -429,10 +428,9 @@ How does sharding affect concurrency?
429
428
-------------------------------------
430
429
431
430
:term:`Sharding <sharding>` improves concurrency by distributing
432
- collections over multiple :binary:`~bin.mongod` instances, allowing shard
433
- servers (i.e. :binary:`~bin.mongos` processes) to perform any number of
434
- operations concurrently to the various downstream :binary:`~bin.mongod`
435
- instances.
431
+ collections over multiple :binary:`~bin.mongod` instances, allowing
432
+ shard servers (specifically, :binary:`~bin.mongos` processes) to run
433
+ concurrently with the downstream :binary:`~bin.mongod` instances.
436
434
437
435
.. include:: /includes/extracts/lock-sharding.rst
438
436
@@ -447,7 +445,7 @@ which is a special collection in the ``local`` database. Therefore,
447
445
MongoDB must lock both the collection's database and the ``local``
448
446
database. The :binary:`~bin.mongod` must lock both databases at the same
449
447
time to keep the database consistent and ensure that write operations,
450
- even with replication, are " all-or- nothing" operations.
448
+ even with replication, are all or nothing operations.
451
449
452
450
.. include:: /includes/extracts/lock-replica-set-primary.rst
453
451
@@ -483,6 +481,18 @@ Depending on the read concern, clients can see the results of writes
483
481
before the writes are :term:`durable`. To control whether the data read
484
482
may be rolled back or not, clients can use the ``readConcern`` option.
485
483
484
+ .. _faq-concurrency-lock-free-read-operations:
485
+
486
+ What are lock-free read operations?
487
+ -----------------------------------
488
+
489
+ .. versionadded:: 5.0
490
+
491
+ A lock-free read operation runs immediately: it is not blocked when
492
+ another operation has an exclusive (X) write lock on the collection.
493
+
494
+ .. include:: /includes/lock-free-commands.rst
495
+
486
496
For information, see:
487
497
488
498
- :doc:`/core/read-isolation-consistency-recency`
0 commit comments