“There are only two hard things in Computer Science: cache invalidation and naming things. — Phil Karlton*”*
Caching is supposed to make systems faster.
But most production bugs I’ve seen around performance had one thing in common — stale cache.
The system was fast.
The database was correct.
Monitoring dashboards were green.
And yet… users were seeing wrong data.
#A Quick Recap: Cache in Distributed Systems
In my previous blog, we talked about caching in distributed systems:
-
What cache is
-
Why we need it
-
Cache hits vs cache misses
-
How multiple servers access the same cache
-
How concurrent requests hit the cache at the same time
All of that helps us add cache to a system.
But adding cache is only half the story.
#The Problem That Still Remains
Let’s say your cache is working perfectly:
-
Requests are fast
-
Cache hit ratio looks great
Then a new question appears:
What happens when the cache is full?
When new data comes in, something old must go out.
But what should we remove?
-
Oldest data?
-
Least used data?
-
Random data?
These questions force us to design cache eviction algorithms like:
-
LRU (Least Recently Used)
-
LFU (Least Frequently Used)
-
FIFO
This process of removing or updating cached data is what we call
cache invalidation
Yes, tools like Redis or DynamoDB support key-value storage and handle eviction internally.
But as engineers, it’s important to understand how cache invalidation actually works, and why it’s so hard to get right.
#What Happens When Cache Invalidation Goes Wrong?
When cache invalidation fails, your system might still be fast —
but it quietly becomes incorrect.
Some common examples:
-
Updated profile bio not reflecting
-
Old prices shown after a discount
-
Incorrect like or comment counts
The system looks healthy, but users slowly lose trust.
And once users stop trusting the data, performance no longer matters.
#The Naive Approach: (Time-Based TTL)
The most common solution is TTL (Time To Live).
In simple terms:
-
Each cache entry has an expiry time
-
After that time, the entry is removed automatically
Example:
-
Cache user profile for 20 minutes
-
After 20 minutes, it expires
Sounds reasonable, right?
But here’s the problem.
Where TTL Breaks
Imagine this:
-
User profile is cached for 20 minutes
-
User updates their bio after 2 minutes
-
Cache still serves old data for the next 18 minutes
Now flip the scenario:
-
Cache expires at exactly 20 minutes
-
A request comes at 20 minutes + 1 second
-
System has to recompute everything again
TTL optimizes for time, not correctness.
Key Insight
Time ≠ Correctness
#When TTL Completely Fails
TTL works fine for read-heavy, rarely-changing data.
But it breaks badly in:
-
Write-heavy systems
-
Counters (likes, views)
-
Feeds
-
Financial data
-
Real-time dashboards
In these systems, data changes continuously, not occasionally.
#A Real-World Example (Instagram-like System)
Imagine:
-
A post has millions of views
-
Like count is cached
-
Thousands of users like the post every second
If you rely on TTL:
-
Cache is updated too slowly → stale counts
-
Or expires too frequently → constant recomputation
TTL doesn’t work here because:
-
Writes are frequent
-
Data changes every second
-
Correctness matters more than freshness by time
#Common Cache Invalidation Strategies
There’s no silver bullet, but here are common approaches.
Write-Through Cache
-
Update database and cache together
-
Strong consistency
-
Higher write latency
Write-Behind Cache
-
Update cache first
-
Database updated asynchronously
-
Faster writes, but riskier
Explicit Invalidation
-
Update DB
-
Manually remove or update cache key
-
More control, more complexity
Event-Based Invalidation
-
DB update emits an event
-
Cache listens and invalidates data
-
Scales well, but introduces eventual consistency
#Why There Is No Perfect Solution:
Every strategy comes with trade-offs:
-
Consistency vs performance
-
Cost vs correctness
-
Simplicity vs control
At scale, engineering judgment matters more than algorithms.
The “right” solution depends on:
-
Read/write patterns
-
User expectations
-
Business impact of stale data
#Thoughts:
Caching makes systems fast.
But cache invalidation decides whether they stay correct.
In real production systems — adding cache is easy.
Designing how it gets invalidated is what separates good systems from broken ones.