What is the WAL?
The Write-Ahead Log is an append-only file that records all write operations:- Sequential writes for high throughput
- Crash recovery by replaying operations
- Durability guarantees when fsync is enabled
- Shared across all column families
The WAL is the foundation of RocksDB’s durability. Without it, only data flushed to SST files would survive a crash.
How WAL Works
Every write operation follows this sequence:Write Path with WAL
- 1. Append to WAL
- 2. Apply to MemTable
- 3. Return to Client
First, the operation is serialized and appended to the WAL file:
With
sync=true, the write latency includes disk fsync time (typically 1-10ms). With sync=false, latency is much lower but recent writes may be lost on system crash.Durability Modes
RocksDB provides different durability guarantees:Synchronous Writes
Maximum durability with higher latency:Latency: 1-10ms depending on storage device. Use for critical data that cannot be lost.
Asynchronous Writes
Higher throughput with relaxed durability:Latency: 10-100μs depending on system. WAL is buffered in OS page cache and fsynced periodically.
Manual WAL Sync
Batch multiple writes, then sync:This approach provides good throughput while maintaining durability at application-defined boundaries (e.g., end of transaction).
WAL Configuration
WAL Size Limits
WAL Archival
Keep old WAL files for backup or replication:WAL Recovery Mode
Control how RocksDB handles corrupted WAL on recovery:Crash Recovery
When RocksDB opens a database, it replays the WAL:Recovery Process
Step 1: Identify WAL Files
Step 1: Identify WAL Files
RocksDB scans the database directory for WAL files:
Step 2: Replay Operations
Step 2: Replay Operations
Each operation in the WAL is replayed:
- Puts: Inserted into MemTable
- Deletes: Marked as deleted in MemTable
- Merges: Applied to MemTable
Step 3: Resume Normal Operation
Step 3: Resume Normal Operation
After replay completes:
- Old WAL files are deleted (unless archived)
- New WAL file is created for future writes
- Database is ready for reads and writes
WAL and Column Families
All column families share a single WAL:Sharing a WAL ensures atomic writes across column families and simplifies recovery, but means WAL size is affected by writes to all column families.
Atomic Writes
WriteBatch provides atomic multi-key writes:The entire WriteBatch is written as a single record in the WAL, ensuring atomicity even across crashes.
Disabling WAL
For non-critical writes, you can disable WAL:Warning: Data written with WAL disabled is lost if the process crashes before the MemTable is flushed. Only use for temporary or reconstructible data.
Use Cases for Disabled WAL
- Bulk loading: Fast initial data load, then call
Flush() - Temporary cache: Data that can be regenerated
- Replicas: Secondary instances that can re-sync from primary
WAL Performance Tuning
Reduce Sync Overhead
WAL Recycling
Concurrent Writes
Monitoring WAL
Get WAL Statistics
Get WAL Size
Best Practices
- Use
sync=truefor critical data that cannot be lost - Batch writes with
WriteBatchand sync periodically for throughput - Set
max_total_wal_sizeto prevent unbounded WAL growth - Archive WALs if you need point-in-time recovery or replication
- Monitor WAL size and flush frequency to identify issues
- Disable WAL only for temporary or reconstructible data
- Use
recycle_log_file_numto reduce file creation overhead
Next Steps
Architecture
See how WAL fits into RocksDB architecture
Snapshots
Learn about consistent point-in-time reads
Compaction
Understand when WAL files are deleted
Backup & Restore
Use WAL for point-in-time recovery