Skip to main content
Snapshots in RocksDB provide a consistent, point-in-time view of the database. They allow you to read data as it existed at a specific moment, even as concurrent writes continue to modify the database.

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)

Use ManagedSnapshot for automatic cleanup:

Snapshot Properties

Sequence Numbers

Each write operation increments the database sequence number:
  • 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

Create snapshot first, then use it:

Monitoring Snapshots

List Active Snapshots

Check Oldest Snapshot

Performance Considerations

  • Time: O(1) - just records current sequence number
  • Space: Negligible - a few bytes per snapshot
  • Overhead: No impact on write performance
Creating snapshots is very cheap.
  • 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
Reading from snapshots is efficient.
  • Prevents compaction from removing old data
  • Increases space amplification over time
  • More disk I/O as obsolete data accumulates
Release snapshots promptly to minimize impact.

Best Practices

  1. Release snapshots promptly - Don’t hold them longer than necessary
  2. Use ManagedSnapshot for automatic cleanup with RAII
  3. Monitor snapshot age - Watch for leaked snapshots
  4. Snapshots for consistency - Use when multiple reads must be consistent
  5. Implicit snapshots - Let iterators create snapshots when appropriate
  6. Avoid long-lived snapshots - They prevent compaction and increase space
  7. 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