Database DesignKey-Value Stores (Redis, DynamoDB)Hard⏱️ ~3 min

Storage Engine Patterns: In Memory vs Log Structured Merge Trees

In-Memory Storage

In-memory key-value stores keep both index and data in RAM for microsecond to sub-millisecond access. A single shard sustains 100,000-500,000 operations per second with sub-millisecond P50 and low single-digit millisecond P99 latency. The trade-off is cost: RAM is 10-100x more expensive per GB than SSD.

Persistence Options

In-memory stores offer persistence to survive restarts. Periodic snapshots (RDB) copy the dataset to disk at intervals, compact and fast to load but losing writes since last snapshot. Append-only logs (AOF) record every write, losing at most one second of data but requiring periodic rewriting to prevent unbounded growth. Hybrid approaches combine both for fast recovery with minimal data loss.

LSM Tree Storage

Log-Structured Merge trees optimize for write-heavy workloads by exploiting a key insight: sequential disk I/O is 10-100x faster than random I/O. Writes go to an in-memory buffer (memtable). When full, it flushes sequentially to disk as an immutable sorted file (SSTable). Background compaction merges SSTables, reclaiming space and maintaining read performance. LSM trees accept slower reads (must check multiple files) for fast sequential writes.

Read and Write Amplification

LSM trees have write amplification: data is written multiple times as it moves through compaction levels. Read amplification: reads may check multiple SSTables before finding a key. Bloom filters (probabilistic data structures that quickly test if a key might exist) reduce unnecessary disk reads by ruling out SSTables that definitely do not contain a key, achieving 1% false positive rate with minimal memory overhead.

Memory Management

Eviction policies handle memory pressure when data exceeds capacity. LRU (Least Recently Used) removes items not accessed recently, good for recency-based workloads. LFU (Least Frequently Used) removes items accessed infrequently, better for workloads with stable hot sets. TTL-based expiration (Time-To-Live, where items automatically expire after a configured duration) removes items predictably, useful for sessions and caches.

💡 Key Takeaways
In-memory stores achieve 100,000-500,000 ops/sec but RAM costs 10-100x more than SSD per GB
Snapshots are fast to restore but lose recent data; AOF logs preserve more but grow large
LSM trees exploit sequential I/O being 10-100x faster than random to optimize writes
Bloom filters reduce read amplification by ruling out SSTables without reading them
LRU suits recency-based access; LFU suits stable hot sets; TTL for predictable expiration
📌 Interview Tips
1Discuss the cost trade-off when choosing in-memory vs disk-based: RAM for hot data with strict latency, disk for larger datasets
2Explain write amplification in LSM trees when discussing storage engine trade-offs
3Mention Bloom filters as a specific optimization for reducing read amplification
← Back to Key-Value Stores (Redis, DynamoDB) Overview
Storage Engine Patterns: In Memory vs Log Structured Merge Trees | Key-Value Stores (Redis, DynamoDB) - System Overflow