Reading — step 1 of 5
Read
~1 min readVariants & Production
Putting It All Together
A production-grade cache layers:
- O(1) LRU (you've built it)
- Sharded for concurrency
- TTL for fresh data
- Metrics for observability
- Optional features: weight-based capacity, async loaders, refresh-ahead, listeners
Modern alternatives:
- TinyLFU (Caffeine in Java): better hit rate than LRU on real workloads. Uses a frequency sketch.
- ARC (ZFS): adapts to workload — favors recency vs frequency dynamically.
- 2Q / S3-FIFO: simple, very fast, near-LRU hit rates.
- W-TinyLFU: window TinyLFU, default in Caffeine.
For most code, LRU is good enough. When it isn't, profile first to confirm cache is the bottleneck (often the network or DB is bigger).
Where you've seen LRU:
- File system page cache
- Browser disk cache
- DNS resolver cache
- LRU-K in PostgreSQL buffer pool
- Memcached, Redis (allkeys-lru policy)
- Java's LinkedHashMap
You've now built the canonical version.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…