Why Cache?
In any distributed system, the database is often the bottleneck. Disk I/O is slow (10-100ms). Caching solves this by keeping frequently accessed data in memory (RAM), which is orders of magnitude faster (1-5ms).
The Latency Hierarchy:
- L1 Cache: ~1 ns
- RAM (Memory): ~100 ns
- SSD (Disk): ~100 µs (1,000x slower)
- Network Call: ~50 ms (500,000x slower)
The Core Concepts
When implementing a cache, you must make three major architectural decisions.
1. Where does the cache live?
- Client Side: Browser cache, iOS local storage. Saves network calls entirely.
- CDN: Content Delivery Network. Geographic caching for static assets (images, CSS).
- Server Side: Redis/Memcached. sitting in front of your database.
2. How do we write to it? (Strategies)
Do you write to the cache first? Or the database? Where is the source of truth?
- Should you use Cache-Aside (safest)?
- Or Write-Back (fastest)?
- Read the Deep Dive on Caching Strategies →
3. What do we delete? (Eviction)
When your 16GB Redis instance is full, what gets thrown out?
- Do you delete the Oldest stuff?
- Or the Least Frequently Used stuff?
- Read the Deep Dive on Eviction Policies (LRU/LFU) →
Common Pitfalls
The "Stale Data" Problem
Caching introduces a new problem: Consistency. If you update a User in the DB, but the Cache still shows the old version, the user sees wrong data.
"There are only two hard things in Computer Science: cache invalidation and naming things."
Cache Stampede (Dog-piling)
If a popular key (e.g., "homepage_news") expires, 10,000 users might hit the database simultaneously to regenerate it.
- Solution: Locking (Mutex) or probabilistic early expiration.
Popular Caching Technologies
| Technology | Type | Best For |
|---|---|---|
| Redis | In-memory key-value store | Sessions, real-time features, pub/sub |
| Memcached | Simple in-memory cache | High-throughput simple caching |
| CDN (CloudFlare, Akamai) | Edge caching | Static assets, global distribution |
| Varnish | HTTP accelerator | Web page caching |
| Browser Cache | Client-side | Static assets, reducing server load |
Real-World Examples
1. Netflix
Netflix caches movie metadata, user preferences, and thumbnails at multiple layers: CDN edge, regional data centers, and in-memory on servers.
2. Twitter Timeline
Twitter's timeline is pre-computed and cached using Redis. When you tweet, it's fanned out to followers' cached timelines.
3. E-commerce Product Pages
Amazon caches product details, reviews, and pricing. Inventory is read-through to ensure accuracy.
The Math: Why Hit Ratio Is Everything
The value of a cache is captured in one formula — the effective latency your users experience:
Plug in realistic numbers (cache: 1ms, database: 50ms):
| Hit Ratio | Average Latency | DB Load Reduction |
|---|---|---|
| 0% | 50 ms | none |
| 90% | 5.9 ms | 10x fewer queries |
| 99% | 1.5 ms | 100x fewer queries |
| 99.9% | 1.05 ms | 1,000x fewer queries |
Two non-obvious lessons hide in this table. First, the jump from 90% to 99% matters more than from 0% to 90% in database load terms — each "nine" cuts the miss traffic by 10x. Second, and more dangerous: a system running at 99% hit ratio has a database sized for 1% of true traffic. If the cache layer restarts cold, the database suddenly receives 100x its normal load and dies instantly. This is why mature systems warm caches before taking traffic and treat "cache cluster restart" as a serious operational event, not a routine one.
Sizing: The 80/20 Reality
Real access patterns are heavily skewed (Zipfian): a small fraction of keys receives most of the traffic. In practice this means you usually don't need RAM equal to your dataset — caching the hottest 20% of data often serves 80%+ of requests. Start by estimating: (number of hot objects) × (average object size) × ~1.5 overhead factor, then measure the actual hit ratio and adjust. Chasing the last few percent of hit ratio by doubling memory is often worse ROI than fixing TTLs or key design.
How Fresh Is Fresh Enough? (Invalidation, Simply)
Every caching conversation eventually becomes a freshness conversation. There are only three basic tools, usually combined:
- TTL (time-to-live): every entry expires after N seconds. Simple, self-healing, and the right default. The question "what TTL?" is really a business question: how stale can a price / profile / article be before it causes harm? Seconds for inventory, minutes for social profiles, hours for a blog post.
- Explicit invalidation: when data changes, delete the corresponding cache key. Precise but fragile — every write path must remember every affected key, and a missed one means indefinite staleness. (This is the "hard problem" the famous quote is about.)
- Versioned keys: embed a version in the key (
user:42:v7). Updates write a new version and change the pointer; old entries simply age out. Sidesteps invalidation races at the cost of extra memory.
A robust rule of thumb: use explicit invalidation for correctness, but always keep a TTL as the backstop — so any bug in invalidation heals itself within minutes instead of persisting forever.
Metrics That Tell You the Truth
A cache without instrumentation is a rumor. The four numbers to dashboard:
- Hit ratio (per key-family, not just global — a 95% global ratio can hide a 40% ratio on your most expensive query).
- Eviction rate: high evictions with a full cache means you're memory-starved; entries die before they can be re-used.
- p99 latency of misses: this is what your users actually feel when it matters.
- Origin traffic: the database QPS the cache is absorbing — this number is your blast radius if the cache disappears.
Interview Tips 💡
When discussing caching in system design interviews:
- Identify read vs. write ratio: Caching benefits read-heavy systems most.
- Estimate cache size: How much data will you cache? Can it fit in RAM?
- Discuss invalidation strategy: How will you keep cache fresh?
- Consider cache stampede: What happens when many requests hit an expired key simultaneously? (Solution: lock and single-fetch, or staggered TTLs)
Related Concepts
- Redis Internals — Deep dive into how Redis works
- CDN — Edge caching for global content delivery
- Database Replication — Another strategy for read scaling
- Rate Limiting — Often used alongside caching
- Consistent Hashing — Used to distribute cache keys across nodes
About ScaleWiki
ScaleWiki is an interactive educational platform dedicated to demystifying distributed systems, software architecture, and system design. Our mission is to provide high-quality, technically accurate resources for software engineers preparing for interviews or solving complex scaling challenges in production.
Read more about our Editorial Guidelines & Authorship.
Educational Disclaimer: The architectural patterns and system designs discussed in this article are based on common industry practices, technical whitepapers, and public engineering blogs. Actual implementations in enterprise environments may vary significantly based on specific product requirements, legacy constraints, and evolving technologies.
Related Articles
Backpressure
Flow control mechanism that prevents fast producers from overwhelming slow consumers by signaling when to slow down, pause, or drop data in streaming systems.
Bloom Filters
Space-efficient probabilistic data structure for membership testing that allows false positives but guarantees no false negatives, using minimal memory compared to hash sets.
Cache Eviction Policies
When the cache is full, something has to go. A comprehensive guide to LRU, LFU, ARC, and other replacement algorithms with implementation details.