The Two Hardest Problems in Computer Science
Cache Invalidation Strategies: From Redis Write-Through to CloudFlare Edge - A Production Guide
Phil Karlton famously said there are only two hard problems in computer science:
Cache invalidation
Naming things
Off-by-one errors
Today we're tackling the first one. And I'm going to tell you something that took me 10 years to learn:
Most caching strategies fail not because they're technically wrong, but because they solve the wrong problem.
Let me explain with a $7 million story.
The $7 Million Cache Miss
In 2018, a major e-commerce platform (name redacted, NDAs are fun) ran a Black Friday sale. Their architecture:
PostgreSQL primary database
Redis cache layer
"Write-through" caching (we'll define this)
99.9% cache hit ratio during normal traffic
Black Friday arrives. Traffic spikes 50x. The cache hit ratio drops to 94%.
That 5.9% difference? It meant:
50x more database connections than expected
Database CPU at 100%
17 minutes of complete downtime
$7 million in lost sales
The problem wasn't their caching strategy. They chose the wrong approach for their actual problem. They optimized for consistency when they needed to optimize for thundering herds.
Let's make sure you never make that mistake.
The Three Caching Strategies You Actually Need to Know
Strategy 1: Write-Through (The Safe Choice)
How it works:
Write request → Update cache → Update database → Return to client
Read request → Check cache → (If miss: load from DB, update cache) → Return
The mental model: Think of it as a write-ahead log. Every write goes through the cache first, then to the database. The cache is always consistent with the database.
Real-world example:
def save_user(user_id, data):
# Write to cache first
cache.set(f"user:{user_id}", data)
# Then write to database
database.save_user(user_id, data)
return "Success"
When to use it:
Read-heavy workloads (90%+ reads)
When consistency matters more than write speed
Session stores, user profiles, product catalogs
Keep reading with a 7-day free trial
Subscribe to Kubenatives to keep reading this post and get 7 days of free access to the full post archives.