Regardless of whether MongoDB exists as a single node or as a replica set, it is transparent to the application. By default, MongoDB read requests are performed on the primary node and remain consistent with the last write operation. The user can configure the replication set read option for each connection to send its read request to the slave node. This situation is generally related to the eventual consistency of the data, although the data set from the node will eventually be consistent with the master node, but the read operations from the node do not guarantee strict data consistency. To ensure data consistency from the read operation of the node, the client can be configured to ensure that the write operation is applied to all nodes of the replication set for success.
Write Concern for Replica set Verify Write Operations to Replica set
Replica set under the default secure write level, only confirm that the data is written to the master node. However, we can modify the secure write level to ensure that the write operation completes the write operation at the specified number of nodes in the replication set.
We can bypass the default security write level by specifying a secure write level for each write operation. Setting a time-out limit at the secure write level avoids blocking when the replica set node does not reach the target.
Modify Default Write Concern
We can set the default security level for replication sets by modifying the getlasterrordefaults in the replica set configuration. The following configuration command effect is: The write operation is completed only after the majority of the nodes in the replication set respond.
Custom Write Concerns
You can label a member of a replication set and then set the security level based on the label.
Read Preference
The copy set read option determines how the read operation is routed in the replication set.
Use Cases indications counter-indications read Preference Modes Tag sets read Preference Processes
Mongodb-replica Set Read and Write