Mongodb – Write commit in replica set MongoDB

mongodbmongodb-3.2replicationtransactional-replication

Suppose We have a replica set with 1 primary and 2 secondaries.

During write operation on Primary, will write commit happen at once upon finishing write operation on primary and before spreading changes to secondaries? Or it will happen once all changes will be apllied on secondaries?

Best Answer

This can be specified for every write operation:

From the docs about Verify Write Operations to Replica Sets:

For a replica set, the default write concern requests acknowledgement only from the primary. You can, however, override this default write concern, such as to confirm write operations on a specified number of the replica set members.

[...]

To override the default write concern, specify a write concern with each write operation. For example, the following method includes a write concern that specifies that the method return only after the write propagates to the primary and at least one secondary or the method times out after 5 seconds.

db.products.insert(
   { item: "envelopes", qty : 100, type: "Clasp" },
   { writeConcern: { w: 2, wtimeout: 5000 } }
)