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.