When traffic spikes expose hidden coupling, eliminate read-path lock contention via 128-way lock striping or Caffeine-style read ring buffers (or SIEVE 1-bit `visited` flags), and pair with W-TinyLFU (1% window + 4-bit Count-Min Sketch) for scan resistance.
TL;DR30-second simplified summary▾
Core Mechanics: Every textbook teaches HashMap + DoublyLinkedList for O(1) LRU.
Visual & Structural Flow: Depicts 1% Admission Window -> 4-bit Count-Min Sketch -> Segmented Main (128 Striped Shards -> Read Ring Buffer -> W-TinyLFU (CMS) -> Timing Wheel TTL); Lock-Free Read Buffer Drain & W-TinyLFU Admission Filter (64 Reader Cores -> Striped Read Buffer -> 4-bit CMS Filter -> Main SLRU (99%)).
Trade-Offs & Production: A ReentrantReadWriteLock still requires an exclusive write lock on get() to mutate the LRU list. Scale to 1.2M+ ops/sec within 8 GB (~880 MB metadata + 7.12 GB payload): Zero-Contention Read Path: Partition into 128 shards (78,125 entries and 9,375 ops/s per.
Failure Modes & Application: Caffeine (Java) and Ristretto/Otter (Go) implement this exact architecture: L1 In-Process Caches: Sits in front of Redis/DB in high-QPS API servers to serve 95% hot reads in < 50 us.
Why classic LRU fails on 64-core servers.
Why this matters: The mechanism: understand how Why classic LRU fails on 64-core servers works from first principles.
Think of Why classic LRU fails on 64-core servers as the intuition layer for Design a High-Concurrency LRU & W-TinyLFU Cache: The mechanism: understand how Why classic LRU fails on 64-core servers works from first principles.
Every textbook teaches HashMap + DoublyLinkedList for O(1) LRU. On a 64-core server handling 1,200,000 get()/sec (95% reads), get() is not read-only: it must unlink the node and move it to the MRU head—forcing a global write lock that caps throughput at ~140,000 ops/sec (>85% CPU spent spinning).
Mutex convoying, one-hit scan pollution, and expired-key leaks.
Why this matters: The problem: see what breaks without Mutex convoying, one-hit scan pollution, and expired-key leaks under real concurrency or scale.
Three bottlenecks destroy in-process cache performance:
- Global Write-Lock Contention: 64 threads serialize on one mutex (
140kvs1.2Mops/sec). - One-Hit Scan Pollution: A batch job scanning
10Mcold keys flushes the entire hot working set out of a pure LRU. - Lazy-TTL Memory Leak: Keys that expire without being read again occupy the
8 GBbudget unless swept.
1% Admission Window -> 4-bit Count-Min Sketch -> Segmented Main
Striped read ring buffers remove read-path locks while W-TinyLFU protects hot entries from sequential scans.
- Problem
- How to implement Design a High-Concurrency LRU & W-TinyLFU Cache without hitting unbounded tail latency, state divergence, or operational fragility.
- Naive Solution
- Use a single unpartitioned path with unbounded retries and synchronous global coordination.
- Why It Fails
- Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
- Alternatives
- Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
- Engineering Decision
- When traffic spikes expose hidden coupling, eliminate read-path lock contention via 128-way lock striping or Caffeine-style read ring buffers (or SIEVE 1-bit
visitedflags), and pair with W-TinyLFU (1% window + 4-bit Count-Min Sketch) for scan resistance. - Trade-Offs
- Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
When Design a High-Concurrency LRU & W-TinyLFU Cache experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?
Naive `RWLock` and un-aged frequency counters.
Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.
Think of Naive RWLock and un-aged frequency counters as the intuition layer for Design a High-Concurrency LRU & W-TinyLFU Cache: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.
A ReentrantReadWriteLock still requires an exclusive write lock on get() to mutate the LRU list. Meanwhile, a naive LFU without periodic counter halving lets historically hot keys permanently block newly hot keys.
Naive `RWLock` and un-aged frequency counters
- Synchronized cache avalanche
- 1,000 duplicate DB queries
- Big-key event-loop freeze
- 1 origin fetch per expired key
- Randomized TTL spreads expiry
- O(1) HashMap + DLL eviction
Enterprise solution: Lock Striping / SIEVE + W-TinyLFU + Timing Wheel.
Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.
Scale to 1.2M+ ops/sec within 8 GB (~880 MB metadata + 7.12 GB payload):
- Zero-Contention Read Path: Partition into
128shards (78,125entries and9,375ops/s per shard) + a Caffeine-style striped read ring buffer (drains promotions asynchronously undertryLock) or SIEVE (if (!node.visited) node.visited = true;avoids even dirtying the cache line on hot reads!). - Scan Resistance (W-TinyLFU): Admit new keys into a
1%Window LRU; on eviction, a 4-bit Count-Min Sketch (periodically halved) admits the candidate to Main SLRU only if its frequency beats the Main victim. - Active TTL: Hierarchical timing wheel sweeps expired entries in
O(1).
Lock-Free Read Buffer Drain & W-TinyLFU Admission Filter
Readers never wait on list locks; a single tryLock drain updates LRU order and filters one-hit scans via CMS.
// SIEVE / Deferred-Read Pattern: avoid write-locking on hot reads
public V get(K key) {
Node<K, V> node = map.get(key); // ConcurrentHashMap lock-free read
if (node == null || node.isExpired(clock.millis())) return null;
if (!node.visited) {
node.visited = true; // Avoids cache-line bouncing on hot keys
}
readBuffer.offer(node); // Lossy ring buffer drained asynchronously
return node.value;
}
Where high-concurrency W-TinyLFU caches are used.
Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.
Caffeine (Java) and Ristretto/Otter (Go) implement this exact architecture:
- L1 In-Process Caches: Sits in front of Redis/DB in high-QPS API servers to serve
95%hot reads in< 50 us. - Database & Storage Block Caches: RocksDB/CockroachDB Pebble use segmented/clock-pro caches to resist large range-scan pollution.
Practice: Make the trade-off yourself.
Why does Caffeine use a lossy read ring buffer and W-TinyLFU instead of a global-lock LRU?