🗄️ Database Indexing & Caching
⚡ Interactive Cache Simulator
Experiment with Cache-Aside behavior — hit/miss ratios, TTL, and traffic bursts
Use this interactive simulator to experience the Cache-Aside pattern in real-time. Adjust the TTL slider to see how cache freshness affects hit ratios. Use Auto Burst to simulate production traffic patterns.
Key Concepts
Hit Rate (%)
The single most important metric for any cache. A 99% hit rate means only 1 in 100 requests touches your database. A 50% hit rate means your cache is practically useless.
Latency Difference
Notice how Cache Hits take ~1ms while DB Misses take ~100ms. In high-traffic systems, this 100x difference dictates whether your servers stay up or melt down.
The Cache Stampede
When a highly-trafficked key's TTL expires, 100 concurrent requests might all experience a Cache MISS simultaneously, slamming your database with 100 identical queries.
⚡ Interactive Architecture Simulator
High-Performance Caching Visualizer
Cache Pattern:
5s60s
0Cache Hits~2ms RAM response
0Cache Misses~100ms DB fetch
0%Hit RatioTarget: >85%
0msAvg Latency0 total reqs
Live Cache Performance Timeline (Hit Ratio % & Latency ms)
Hit Ratio %: 0
Avg Latency (ms): 0
85% Target Hit Ratio: 85
20:05:54Cache Simulator ready. Click "Send Request" or "Simulate Stampede" to test.
The Cache Stampede (Thundering Herd) Problem
How to fix a Cache Stampede?
- **TTL Jitter:** Add a random variance (e.g. +/- 10%) to your TTLs. Instead of all keys expiring at exactly 5:00:00, they expire randomly between 4:55:00 and 5:05:00. This prevents massive simultaneous expires.
- **Mutex/Distributed Locks:** When a Cache MISS occurs, only the *first* thread is allowed to fetch the data from the database. The other 99 threads must wait for the first thread to populate the cache.
- **Probabilistic Early Expiration:** Also known as XFetch. The cache starts randomly acting as if it's expired *slightly before* the real TTL hits. A lucky background thread gets a 'fake MISS' and refreshes the cache asynchronously while everyone else still gets the old data.
💡
Senior Architect Insight: To solve Cache Stampedes, engineers use two common techniques: 1) TTL Jitter (adding a random +/- 10% to the TTL so keys don't expire simultaneously) and 2) Probabilistic Early Expiration (PERF) where the cache occasionally auto-refreshes itself slightly before the TTL actually expires.