Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

mongodb

What is Automatic Failover in MongoDB?

Shahpar Khan
svg viewer

Background

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 replica setsA group of mongod processes that maintain the same data set. to provide redundancy and high availability of data across servers.

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.

What 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:

svg viewer

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.

svg viewer

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.

svg viewer

To read more about replication and its features in MongoDB, visit the official documentation.

RELATED TAGS

mongodb

CONTRIBUTOR

Shahpar Khan
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring