MongoDB, developed by MongoDB Inc., is a cross-platform database program. It is categorized as a NoSQL database program that stores data in JSON-like documents.
MongoDB provides the key feature of Replication. Replication allows MongoDB to have multiple
Replication also increases read capacity, which allows users to send read requests to different replicas for retrieving data. Sending read requests to multiple replicas instead of one node makes the request processing time much faster. Replication also provides is Automatic Failover.
Automatic Failover is a sub-feature of Replication that provides fault tolerance and persistence. Here is what a typical replica set looks like:
The primary node receives all write operations. A replica set can have only one primary capable of confirming writes. The secondaries replicate the primary’s oplog(operation log) and apply the operations to their data sets such that the secondaries’ data sets reflect the primary’s.
But what happens if the primary fails/crashes?
At this point, the secondaries
would realize that the primary has failed when they ping the primary and it does not respond for ten seconds (default). You can configure the timeout by changing the electionTimeoutMillis
period. When this happens, the secondaries conduct an election to delegate a new primary.
The replica set cannot process write operations until the election completes successfully.
It typically takes 12 seconds to successfully conduct the election.
Lowering the electionTimeoutMillis
replication configuration option from the default 10000 (10 seconds) can result in faster detection of primary failure. However, the cluster may call elections more frequently due to factors such as temporary network latency even if the primary is otherwise healthy.
To read more about replication and its features in MongoDB, visit the official documentation.