Services

Resources

Company

/

MongoDB oplog

MongoDB oplog

The MongoDB oplog (operations log) is a special capped collection, local.oplog.rs, that records every write to a replica set in order. Secondaries copy and apply these entries to stay in sync with the primary. Backup tools and change streams read the same log, which makes it the basis for point-in-time recovery.

How it works

Each oplog entry describes one idempotent operation, such as an insert, update or delete, with a timestamp. Because the collection is capped, old entries are overwritten once it is full. The time span it covers is the oplog window. For example, a 50 GB oplog on a cluster writing 2 GB of oplog per hour holds about 25 hours of history. A backup that combines a full mongodump with saved oplog slices can be replayed with mongorestore --oplogReplay to a chosen moment.

Why it matters in production

If a secondary falls behind by more than the oplog window, it cannot catch up and needs a full resync. If you back up oplog slices less often than the window, you lose the ability to restore between full backups. Size the oplog for your peak write rate, and watch replication lag against the window.

See also

On the One2N blog

References

  1. MongoDB manual: replica set oplog

The MongoDB oplog drawn as a ring of 25 slices, each 1 hour and 2 GB of writes. The center works out the window: a 50 GB oplog divided by 2 GB per hour equals 25 hours. The primary writes at the head of the ring, and a secondary reading partway round is in sync. A red arrow points at the oldest slice, about to be overwritten: a secondary more than 25 hours behind needs a full resync.

Putting this into practice?

Talk to One2N about our backend engineering services

Talk to One2N about our backend engineering services