Reference · Cheat sheet

Replica sets

Lesson 0007 distilled — one primary, oplog, elections, failover. Built to print.

From lesson 0007Context: MongoDB current (7.0/8.0+)

Architecture

One primary (all writes go here). One or more secondaries (replicate via oplog). Optional arbiter (votes only, holds no data).

Standard deployment: 3 full nodes — tolerates 1 failure. 2-node set requires arbiter for elections.

The oplog

local.oplog.rs — a capped collection on every member.

Logical: records operations (insert/update/delete), not page changes.

Idempotent: applying the same entry twice = same result as once. Enables safe crash recovery.

Window: when full, oldest entries overwritten. Lagging secondary that misses the window needs a full resync.

Elections

Heartbeat every 2s. If no primary heard for electionTimeoutMillis (default 10s), a secondary calls an election.

Winner must have: most up-to-date oplog AND votes from a majority of voting members.

Election window ~10–30s: writes fail during this period. Use retryable writes (default in drivers ≥4.2).

Read preference

primary (default): all reads → primary. Strong consistency, always read your own writes.

secondaryPreferred: secondary if available, else primary. Possible stale reads.

secondary: always secondary. Accepts staleness for scale-out reads.

Oplog vs WAL vs binlog

Postgres WALMySQL binlogMongoDB oplog
LevelPhysicalLogicalLogical
IdempotentNoPartialYes
Tied to versionYesLess soNo
Locationpg_wal/binlog fileslocal.oplog.rs

Failover built in

Automatic election on primary failure — no Patroni/PaceMarker needed. Applications see a brief write gap (~10–30s), then a new primary is elected.

Priority 0 member: can vote but never becomes primary (good for distant data centers).