In this lesson, you will learn how to evaluate the trade-offs between snapshot-based and oplog-based backup strategies to ensure data durability in your document store.
The Snapshot Method
A snapshot is a point-in-time "frozen" image of your entire database files. Think of it like taking a photograph of your desk at a specific second.
- Pros: It is simple to restore; you just replace the database files with the snapshot.
- Cons: It is resource-intensive. If you take a snapshot every hour, you might experience performance degradation during the capture process, and you lose any data changes that occurred between the snapshot and a potential crash.
The Oplog-based Method
The "oplog" (or operations log) tracks every write operation performed on the database. Instead of copying the whole database, you keep a running record of changes.
- Pros: It allows for "Point-in-Time Recovery" (PITR). You can restore a snapshot from last night and then "replay" the oplog to bring the database up to the exact millisecond before a failure occurred.
- Cons: It requires managing log retention and can consume significant storage space as the log grows.
Comparing the Strategies
Consider a document store managing an E-commerce Catalog. If you only use snapshots, a crash at 4:55 PM might mean losing all updates made since the 4:00 PM snapshot. By combining them, you gain the best of both worlds:
// Conceptual flow of Oplog Replay
restoreSnapshot('2023-10-27_0400');
replayOplog('2023-10-27_0400', '2023-10-27_1655');
In practice, you typically use snapshots as your baseline and the oplog as your "delta" to bridge the gap to the present moment. This hybrid approach is essential for high-availability systems where losing even an hour of user interaction is unacceptable. When evaluating your strategy, look at your Recovery Point Objective (RPO): if you cannot afford data loss, an oplog-based strategy is mandatory. If storage and simplicity are your primary constraints, periodic snapshots may suffice.
Key Takeaway: Snapshots provide a stable foundation for recovery, while oplog-based logging provides the precision needed to minimize data loss between snapshots.