Introducing Dragonfly Cloud! Learn More

Question: What is MongoDB replication architecture?

Answer

MongoDB's replication architecture is designed to provide high availability, data redundancy, and disaster recovery. It relies on the concept of replica sets, which are groups of MongoDB servers that maintain the same dataset, providing automatic failover and data redundancy.

How Does It Work?

A replica set consists of multiple nodes (MongoDB instances), with one primary node and several secondary nodes. The primary node receives all write operations, while the secondary nodes replicate the primary's data set, allowing them to take over quickly in case the primary goes down.

Primary Node:

  • Receives all write operations.
  • Upon receiving a write operation, the primary node records the operation in an oplog (operations log).

Secondary Nodes:

  • Continuously poll the primary’s oplog and apply the operations to themselves in an asynchronous process.
  • Can serve read operations if configured to do so, but by default, all reads go to the primary to ensure consistency.

Elections and Failover

  • If the primary node fails or becomes unreachable, the secondary nodes hold an election to choose a new primary from among themselves.
  • This process ensures minimal downtime and promotes a member with the most up-to-date data to primary.

Other Components:

Arbiter:

  • Arbiters participate in elections but do not hold a copy of the database. They exist only to provide a majority vote in elections, useful in configurations with an even number of nodes.

Priority Settings:

  • Replica set members can have priorities that influence their likelihood of being elected as the primary. A higher priority increases the chances of election as primary.

Read Preferences:

  • Clients can specify read preferences to control whether they read from the primary, secondaries, or a combination thereof, influencing consistency and load distribution.

Write Concern:

  • This setting allows clients to specify the level of assurance for write operations, such as requiring acknowledgment from a certain number of nodes.

Example Configuration

Creating a simple three-member replica set might involve starting each MongoDB instance with settings like these:

mongod --replSet myReplicaSet --dbpath /srv/mongodb/rs0-0 --port 27017 mongod --replSet myReplicaSet --dbpath /srv/mongodb/rs0-1 --port 27018 mongod --replSet myReplicaSet --dbpath /srv/mongodb/rs0-2 --port 27019

After starting the instances, you'd connect to one and initiate the replica set:

rs.initiate({ _id: 'myReplicaSet', members: [ { _id: 0, host: 'localhost:27017' }, { _id: 1, host: 'localhost:27018' }, { _id: 2, host: 'localhost:27019' } ] })

This setup provides a basic replication framework, offering a foundation for building scalable, resilient applications.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.