Skip to content

Chapter 5 of 8

Replication for High Availability

A replica set is MongoDB's primary mechanism for high availability and data redundancy. It consists of a group of MongoDB instances that maintain the same data set, including one primary node that handles all write operations and one or more secondary nodes that replicate data from the primary. If the primary fails, the replica set automatically triggers an election to promote a secondary to primary, ensuring continuous service with minimal downtime.

Replication itself works through the oplog, a special capped collection on the primary that records every write operation. Secondary nodes continuously tail the oplog and apply the operations locally, keeping their copies of the data in sync. This architecture provides data redundancy, enables automatic failover, and allows read operations to be distributed across secondaries to reduce load on the primary. When a replica set has an even number of data-bearing members, an arbiter can be added to break ties during primary elections. Arbiters participate in elections but do not hold any data, making them lightweight and resource-efficient, though they should be used sparingly in production environments.

Two important settings govern how clients interact with replica sets. Write concern specifies the level of acknowledgment requested for write operations: w: 0 means no acknowledgment, w: 1 means acknowledgment from the primary alone (the default), and w: "majority" requires acknowledgment from a majority of members. Higher write concern improves durability at the cost of latency. Read preference, on the other hand, determines which members receive read operations. Options include primary (the default), primaryPreferred, secondary, secondaryPreferred, and nearest (the member with the lowest latency). Together, these settings allow applications to balance consistency, availability, and performance based on their specific requirements.

All chapters
  1. 1Foundations of MongoDB
  2. 2CRUD Operations and Querying
  3. 3The Aggregation Framework
  4. 4Indexing and Query Performance
  5. 5Replication for High Availability
  6. 6Sharding for Horizontal Scaling
  7. 7Schema Design Patterns
  8. 8Advanced Features and Ecosystem

Drill it

Reading is not remembering. These come from the Nosql MongoDB deck:

Q

What is MongoDB?

MongoDB is a NoSQL document-oriented database that stores data in flexible, JSON-like documents called BSON. It is designed for scalability and high performance...

Q

What is the document model in MongoDB?

The document model stores data as documents, which are rich data structures similar to JSON objects. Each document can contain nested fields, arrays, and sub-do...

Q

What is BSON?

BSON (Binary JSON) is the binary-encoded serialization format MongoDB uses to store documents and make remote procedure calls. It extends JSON with additional d...

Q

What is an ObjectId in MongoDB?

An ObjectId is a 12-byte unique identifier automatically generated by MongoDB as the default _id field for each document. It consists of: A 4-byte timestampA 5-...