Database Reference
In-Depth Information
Understanding replica set election problems
In a replica set, only the
primary
node can accept write operations. By default, clients
perform read operations from the
primary
node too. However, you can change this con-
dition for a read operation and route the client's read requests to the
secondary
nodes.
Suppose we have the
testcollection
collection and we want to read data with
mongo
shell. At the first attempt, you will face with an error as shown in the following
screenshot:
Then, you can allow this node to be readable from clients using the following command:
rs.slaveOk(true)
The following screenshot shows the output after using the preceding command:
It's recommended that you enable read operations for secondary members. Using this ap-
proach, you can route read operations for secondary members. Thus, clients can perform
read operations even if the
primary
node is not available.
Now, we don't have any issues with read operations while election, but the write operations
still won't execute. This is because the
primary
node is not accessible. This is the only
part that the MongoDB replica set doesn't handle.