Caching Strategies
Cut read latency from 20 ms to <1 ms while avoiding stale data and cache stampedes.
In plain words
A cache is a small, very fast memory that keeps answers you need often, so you don’t have to fetch them from the slow database every time.
Think of it like…
Keeping your daily tools on your desk instead of walking down to the basement storage room every time you need a pen.
Where you’ve seen it
A news site caches its homepage in Redis. Millions of visitors get it in about 1 ms, and the database is queried only when the cache expires.
Words you’ll hear
- Cache hit / miss
- A hit means the answer was in the cache; a miss means you had to fetch it.
- TTL
- How long an item stays in the cache before it expires.
- Eviction (LRU)
- Removing the least recently used items when the cache is full.
- Cache-aside
- The app checks the cache first and, on a miss, loads from the DB and fills the cache.
Visual diagram
Theory & deep dive
Why caching is the single highest-ROI scaling tool
Reading from Redis takes ~0.5 ms vs. ~5–20 ms for a database query - and RAM easily handles 100,000+ ops/sec per node. Offloading 90% of reads to a cache instantly multiplies your database’s lifespan by 10×.
The two hard problems of caching are staleness (cached value is out of date) and failure modes (what happens when a million keys expire at once).
Cache Patterns: Aside, Write-Through, Write-Back
Cache-Aside lets the app read/populate the cache lazily - simple, resilient if Redis restarts, and only caches data people actually ask for.
Write-Through updates the cache and DB together on writes (always fresh, slightly slower writes). Write-Back writes only to cache and flushes asynchronously to the DB later (blazing-fast writes, but risks losing dirty data if cache crashes).
How it works, step by step
Step 1 of 41. Choose read/write pattern (Cache-Aside default)
On read: check cache → on miss, read DB, populate cache with TTL, return. On write: update DB, then delete (invalidate) the cache key.
- Read Hit
- <1 ms
- Cache Miss
- DB RTT + fill
When to use it & trade-offs
What it is
Caching stores frequently accessed data in fast memory (Redis, Memcached, or local RAM) to avoid repeating expensive database queries or computations.
Why it matters: Most applications are heavily read-skewed (80/20 rule); caching drops P99 latency dramatically and protects the database from read overload.
When to reach for it
- Read-heavy profiles, product catalogs, permissions, and rendered feeds
- Distributed counters, leaderboards (Redis Sorted Sets), and session stores
- Coalescing repeated expensive computations or external API calls
When to avoid or be careful
- Write-back caching for financial balances or irreversible orders without durable WAL
- Setting identical TTLs on 10M keys seeded at midnight (causes synchronized expiry avalanche)
Caching Read/Write Strategies
Strengths: Only requested keys occupy RAM; cache outage degrades gracefully to DB.
Watch out: Cold-miss penalty; brief stale window between DB write and cache delete.
Strengths: Cache is always warm and in sync with the DB on every write.
Watch out: Higher write latency (2 hops) and unused writes pollute cache RAM.
Strengths: Ultra-fast writes (batched asynchronously to DB); coalesces repeated updates.
Watch out: Risk of data loss if the cache node crashes before flushing to disk/DB.
Interview signals & numbers to know
When this topic comes up
- 80/20 reads
- sub-millisecond reads
- hot key
- thundering herd
- Redis
Benchmarks & rules of thumb
- Redis latency (VPC)
- 0.3–1 ms
- Single Redis throughput
- ~100k ops/sec
- Target cache hit ratio
- 85–99%
- TTL jitter range
- ±10–20%
Common mistakes & follow-ups
Common interview mistakes
- Updating cache before DB or updating both concurrently (out-of-order writes leave stale cache forever)
- Letting a hot key expire without single-flight coalescing, crashing the DB under a stampede
- Ignoring memory fragmentation and oversized values (>1 MB keys slow Redis single-threaded loop)
Likely interviewer follow-ups
- Why is deleting a cache key on write usually safer than updating it?
- Explain the difference between cache stampede, cache penetration, and cache avalanche.
- How do you keep an L1 in-process cache synced across 50 app servers?
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Caching Strategies inside real High-Level Design prompts or review Q&A flashcards.