WAL Quick Reference Guide
WAL Quick Reference Guide
Configuration
Enable WAL
[storage]wal_enabled = truewal_sync_mode = "sync" # Options: "sync", "async", "group_commit"// Programmaticlet mut config = Config::default();config.storage.wal_enabled = true;config.storage.wal_sync_mode = WalSyncModeConfig::Sync;Sync Modes
| Mode | Fsync | Throughput | Latency | Durability | Use Case |
|---|---|---|---|---|---|
| Sync | Every | ~1K ops/s | 1-5ms | Maximum | Production |
| Async | OS | ~100K ops/s | <1ms | OS-level | Testing/Dev |
| GroupCommit | Batched | ~50K ops/s | 1-2ms | High | High-throughput |
Common Operations
Check WAL Status
// Is WAL enabled?if engine.is_wal_enabled() { println!("WAL is active");}
// Get current LSNlet lsn = engine.wal_lsn().unwrap_or(0);println!("Current LSN: {}", lsn);
// Get sync modeif let Some(mode) = engine.wal_sync_mode() { println!("Sync mode: {:?}", mode);}Flush WAL
// Force fsync (for async/group_commit modes)engine.flush_wal()?;Change Sync Mode
// Switch to async mode at runtimeengine.set_wal_sync_mode(WalSyncMode::Async)?;
// Switch to sync mode for critical operationengine.set_wal_sync_mode(WalSyncMode::Sync)?;Crash Recovery
// Open engine (auto-recovers LSN)let engine = StorageEngine::open("./data", &config)?;
// Replay WAL entries if neededlet recovered = engine.replay_wal()?;println!("Recovered {} operations", recovered);Truncate WAL (Checkpointing)
// After successful checkpoint at LSN 10000let checkpoint_lsn = 10000;engine.truncate_wal(checkpoint_lsn)?;Write Operations (Automatic WAL Logging)
// All writes automatically log to WAL firstengine.put(b"key", b"value")?; // → WAL append, then RocksDB putengine.delete(b"key")?; // → WAL append, then RocksDB deleteengine.insert_tuple("users", tuple)?; // → WAL append, then insertGraceful Shutdown
// Ensure all data is flushed before shutdownengine.flush_wal()?; // Flush WALengine.flush()?; // Flush RocksDBengine.close()?; // Close engineBest Practices
1. Production Configuration
config.storage.wal_enabled = true;config.storage.wal_sync_mode = WalSyncModeConfig::Sync;2. Periodic Checkpointing
const CHECKPOINT_INTERVAL: u64 = 10000;
if engine.wal_lsn().unwrap_or(0) % CHECKPOINT_INTERVAL == 0 { let lsn = engine.wal_lsn().unwrap(); engine.flush()?; // Checkpoint engine.truncate_wal(lsn - CHECKPOINT_INTERVAL)?; // Cleanup}3. Monitor WAL Size
let lsn = engine.wal_lsn().unwrap_or(0);if lsn > 100000 { warn!("WAL has {} entries, consider checkpointing", lsn);}4. Development/Testing
// Use async mode for faster testsconfig.storage.wal_sync_mode = WalSyncModeConfig::Async;Troubleshooting
WAL not enabled
if !engine.is_wal_enabled() { // Check config.storage.wal_enabled = true // Check for initialization errors in logs}High write latency
// Switch to async or group_commit modeengine.set_wal_sync_mode(WalSyncMode::GroupCommit)?;Large WAL size
// Implement checkpointinglet lsn = engine.wal_lsn().unwrap();engine.flush()?;engine.truncate_wal(lsn - 10000)?;Performance Tips
- Use Sync mode for durability: Maximum safety, ~1K writes/sec
- Use GroupCommit for throughput: Balanced, ~50K writes/sec
- Use Async for testing: Maximum speed, ~100K writes/sec
- Checkpoint regularly: Prevent unbounded WAL growth
- Flush before shutdown: Ensure all data is persisted
API Reference
StorageEngine Methods
is_wal_enabled() -> boolwal_lsn() -> Option<u64>flush_wal() -> Result<()>replay_wal() -> Result<usize>truncate_wal(up_to_lsn: u64) -> Result<()>wal_sync_mode() -> Option<WalSyncMode>set_wal_sync_mode(mode: WalSyncMode) -> Result<()>WriteAheadLog Methods
open(db: Arc<DB>, sync_mode: WalSyncMode) -> Result<Self>append(operation: WalOperation) -> Result<u64>flush() -> Result<()>replay() -> Result<Vec<WalEntry>>truncate(up_to_lsn: u64) -> Result<()>current_lsn() -> u64set_sync_mode(mode: WalSyncMode)Files
- Implementation:
/home/claude/HeliosDB Nano/src/storage/wal.rs - Integration:
/home/claude/HeliosDB Nano/src/storage/engine.rs - Configuration:
/home/claude/HeliosDB Nano/src/config.rs - Full Docs:
/home/claude/HeliosDB Nano/docs/implementation/WAL_IMPLEMENTATION.md