Database Reference
In-Depth Information
meansfortheapplication developeristhat,onaverage,anyindividualwriteswith
j=True
will
take around 50 ms to complete, which is generally even more time than it would take to rep-
licate the data to another server. If you want to ensure that MongoDB not only
acknowledges
receipt of a write operation but also commits the write operation to the on-disk journal before
returning successfully to the application, you can use the
j=True
option:
>>>
>>>
db
.
events
.
insert
(
event
,
j
=
True
)
It's important to note that the journal does
not
protect against any failure in which the disk it-
self might fail, since in that case the journal file itself can be corrupted. Replication, however,
does
protect against single-server failures, and is the recommended way to achieve real dur-
ability.
NOTE
j=True
requires acknowledgment from the server, so
w=1
is implied unless you explicitly
set
w=N
with N greater than 1.
You can require that MongoDB replicate the data to multiple secondary replica set members
before returning:
>>>
>>>
db
.
events
.
insert
(
event
,
w
=
2
)
This will force your application to acknowledge that the data has replicated to two members
of the replica set. You can combine options as well:
>>>
>>>
db
.
events
.
insert
(
event
,
j
=
True
,
w
=
2
)
In this case, your application will wait for a successful journal commit
and
a replication ac-
knowledgment. This is the safest option presented in this section, but it is the slowest. There
is always a trade-off between safety and speed.