Skip to content

Commit e9811e3

Browse files
author
Sam Kleinman
committed
DOCS-379 write concern revision
1 parent 6147564 commit e9811e3

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

source/applications/replication.txt

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,60 +21,77 @@ Write Concern
2121

2222
When a :term:`client` sends a write operation to a database server,
2323
the operation will return without waiting for the operation to succeed
24-
or complete. To verify that the operation has completed,
25-
and to check for an error, use the
26-
:dbcommand:`getLastError` command. You can configure
27-
:dbcommand:`getLastError` to wait until the journal flushes to disk or the data itself flushes to disk. For replica sets, you can
28-
configure :dbcommand:`getLastError` to return only after the write
29-
operation has propagated to more than one member of the set or to a
30-
majority of the set's members.
24+
or complete. The :dbcommand:`getLastError` command provides a way to
25+
check if write operations have succeeded, and provides operations to
26+
require various levels of write concern. [#write-concern]_ These
27+
levels are:
3128

32-
Many drivers have a "safe" mode or "write concern" that automatically
33-
issues a :dbcommand:`getLastError` command following write
34-
operations to ensure that they complete. "Safe mode,"
35-
provides confirmation of write operations to the client.
29+
- without options. Confirms that the server has received the write
30+
operations, and modified the :program:`mongod` instances in-memory
31+
representation of the data.
3632

37-
However, safe writes can take longer to return and are not required in
38-
all applications. Using the ``w: "majority"`` option for
39-
:dbcommand:`getLastError`, write operations to a replica set will
40-
return only after a write operation has replicated to a majority of
41-
the members of the set. At the :program:`mongo` shell, use the
42-
following command to ensure that writes have propagated to a majority
43-
of the replica set members:
33+
- with the ``journal`` option. Confirms that the server has committed
34+
the data to the on-disk journal. This ensures that the data will be
35+
durable in the event that :program:`mongod` experiences an
36+
unexpected shut down.
37+
38+
- with the ``fsync`` option. Confirms that the :program:`mongod` has
39+
flushed the write operation to disk.
40+
41+
- for replica sets, with the ``w`` option. Confirms that the write
42+
operation has replicated to the specified number of
43+
replica set members. You may specify a number of servers *or*
44+
``majority`` to ensure that the write propagates to a majority of
45+
set members.
46+
47+
Many drivers have a "safe" mode or "write concern" that automatically
48+
issues a :dbcommand:`getLastError` command following write operations
49+
to ensure that they complete. "Safe mode," provides confirmation of
50+
write operations to the client. However, safe writes can take longer
51+
to return and are not required in all applications. Consider the
52+
following operations.
4453

4554
.. code-block:: javascript
4655

4756
db.runCommand( { getLastError: 1, w: "majority" } )
4857
db.getLastErrorObj("majority")
4958

50-
You may also specify ``w: 2`` so that the write operation replicates
51-
to a second member before the command returns.
59+
Following a write operation, these equivalent
60+
:dbcommand:`getLastError` operations will ensure that write operations
61+
return only after a write operation has replicated to a majority of
62+
the members of the set. You might also specify ``w: 2`` so that the
63+
write operation replicates to a second member before the command
64+
returns.
5265

5366
.. note::
5467

55-
:dbcommand:`getLastError` assumes the primary replica node,
56-
therefore, ``w: 2`` waits until the :term:`primary` and one other
57-
member of the replica set acknowledges the write operation. The current
58-
primary always counts as ``w: 1``. Using ``w: 0`` makes no sense and should
59-
not be done.
68+
:dbcommand:`getLastError` assumes the primary replica node, which
69+
is equivalent to ``w: 1``. ``w: 2`` waits until 2 members of the
70+
set, or the :term:`primary` and one other member of the replica set
71+
before confirming the write operation.
6072

61-
You can also configure a "default" :dbcommand:`getLastError` behavior on the
62-
replica set configuration. For instance:
73+
Using ``w: 0`` is valid but does not produce useful behavior.
74+
75+
You can also configure a "default" :dbcommand:`getLastError` behavior
76+
for the replica set configuration. Use the
77+
:data:`settings.getLastErrorDefaults` setting in the :doc:`replica set
78+
configuration </reference/replica-configuration>`. For instance:
6379

6480
.. code-block:: javascript
6581

6682
cfg = rs.conf()
6783
cfg.settings.getLastErrorDefaults = {w: "majority", j: true}
6884
rs.reconfig(cfg)
6985

70-
When the new configuration is active, the effect of the
71-
:dbcommand:`getLastError` operation will wait until the write
72-
operation has completed on a majority of the set members before writing. By
73-
specifying ``j: true`` a complete commit of the operations to the
74-
journal is all that :dbcommand:`getLastError` requires to return. Use the
75-
:data:`getLastErrorDefaults`" setting in the :ref:`replica set` configuration
76-
to define the standards for a set-wide "safe mode."
77-
The default setting will only affect :dbcommand:`getLastError`
86+
When the new configuration is active, the :dbcommand:`getLastError`
87+
operation will wait for the write operation to complete on a majority
88+
of the set members before returning. By specifying ``j: true``,
89+
:dbcommand:`getLastError` waits for a complete commit of the
90+
operations to the journal before returning.
91+
92+
Use the :data:`getLastErrorDefaults`" setting in the :ref:`replica
93+
set` configuration to define the standards for a set-wide "safe mode."
94+
The default setting will only affect :dbcommand:`getLastError`
7895
commands with *no* other arguments.
7996

8097
.. index:: read preference
@@ -516,7 +533,7 @@ Database Commands
516533

517534
Because some :term:`database commands <database command>` read and
518535
return data from the database, all of the official drivers support
519-
full :ref:`read preference mode semantics <replica-set-read-preference-modes>`
536+
full :ref:`read preference mode semantics <replica-set-read-preference-modes>`
520537
for the following commands:
521538

522539
- :dbcommand:`group`

0 commit comments

Comments
 (0)