Problem Statement
Describe the replication architecture in MongoDB. How do secondary nodes stay synchronized with the primary?
Explanation
MongoDB's replication architecture centers around the primary node, which is the only node accepting write operations. When a write occurs, the primary records it in the oplog, a special capped collection in the local database. The oplog stores operations in the order they are executed.
Secondary nodes continuously query the primary's oplog for new entries. When new operations appear, secondaries copy these oplog entries and apply them to their own data files in the same order. This process is called replication. The operations in the oplog are idempotent, meaning they can be applied multiple times safely without changing the result.
Secondaries maintain a cursor on the primary's oplog, tracking the timestamp of the last operation they applied. They fetch operations with timestamps greater than their last applied timestamp. This allows secondaries to catch up if they fall behind due to network issues or heavy load.
Sometimes a secondary can replicate from another secondary instead of the primary, a process called chained replication. This reduces load on the primary and is enabled by default. However, data always originates from the primary through the oplog.
If a secondary falls too far behind and the operations it needs have been overwritten in the oplog, it must perform an initial sync, copying all data from another member. This is why oplog sizing is important for production deployments.
Code Solution
SolutionRead Only
// Replication flow:
// 1. Write operation on primary
db.users.insertOne({ name: "Alice" }) // On primary
// 2. Primary writes to oplog
// local.oplog.rs: { ts: Timestamp(...), op: "i", ns: "db.users", o: {...} }
// 3. Secondaries query oplog
// Secondary runs: db.oplog.rs.find({ ts: { $gt: lastApplied } })
// 4. Secondaries apply operations
// Secondary executes same insert
// 5. Secondaries update their sync state
// Track timestamp of last applied operation