What are Snapshots?
A snapshot is an immutable reference to the database state at a specific sequence number:- Consistent reads: See the same data across multiple operations
- No blocking: Concurrent writes proceed normally
- Lightweight: Creating a snapshot is O(1)
- Read-only: Snapshots only affect reads, not writes
Snapshots don’t copy data. They’re lightweight markers that prevent old data from being garbage collected while the snapshot exists.
Creating Snapshots
Basic Snapshot Usage
Always release snapshots when finished. Holding snapshots prevents compaction from removing old data, increasing space usage.
Managed Snapshots (RAII)
UseManagedSnapshot for automatic cleanup:
Snapshot Properties
Sequence Numbers
Each write operation increments the database sequence number:Understanding Sequence Numbers
Understanding Sequence Numbers
- Every write operation (Put, Delete, Merge) gets a unique sequence number
- Sequence numbers are monotonically increasing
- A snapshot captures the sequence number at creation time
- Reads with a snapshot only see data with sequence numbers ≤ snapshot sequence
Use Cases
Consistent Backups
Create consistent backups without blocking writes:Multi-Read Consistency
Ensure multiple reads see consistent data:Without snapshots, concurrent writes between the two reads could lead to inconsistent data (e.g., reading old user data but new balance).
Range Scan Consistency
Scan ranges without interference from concurrent writes:Snapshots and Column Families
Snapshots work across all column families:A single snapshot provides consistency across ALL column families in the database.
Snapshots and Compaction
Snapshots prevent data from being removed:Important: Long-lived snapshots increase space amplification. They prevent compaction from removing old versions of keys, causing disk space usage to grow.
Iterator Snapshots
Iterators implicitly use snapshots:Explicit vs Implicit Snapshots
- Explicit Snapshot
- Implicit Snapshot
Create snapshot first, then use it:
Monitoring Snapshots
List Active Snapshots
Check Oldest Snapshot
Performance Considerations
Snapshot Creation Cost
Snapshot Creation Cost
- Time: O(1) - just records current sequence number
- Space: Negligible - a few bytes per snapshot
- Overhead: No impact on write performance
Snapshot Read Cost
Snapshot Read Cost
- Same as normal reads if data is still in cache
- May require reading old data from disk if not cached
- No blocking - reads from snapshots don’t block writes
Long-Lived Snapshot Cost
Long-Lived Snapshot Cost
- Prevents compaction from removing old data
- Increases space amplification over time
- More disk I/O as obsolete data accumulates
Best Practices
- Release snapshots promptly - Don’t hold them longer than necessary
- Use ManagedSnapshot for automatic cleanup with RAII
- Monitor snapshot age - Watch for leaked snapshots
- Snapshots for consistency - Use when multiple reads must be consistent
- Implicit snapshots - Let iterators create snapshots when appropriate
- Avoid long-lived snapshots - They prevent compaction and increase space
- Backup with snapshots - Ensures consistent backup without blocking writes
Comparison with Transactions
Use snapshots for consistent reads. Use transactions (from RocksDB utilities) when you need atomic multi-key updates with rollback capability.
Example: Consistent Multi-Step Query
Next Steps
Write-Ahead Log
Understand durability with WAL
Architecture
See how snapshots fit in RocksDB
Transactions
Use transactions for atomic updates
Backup & Restore
Create consistent backups with snapshots