HLD Interview Notes: Day 8/30
Topic: Cache Consistency: TTL, Invalidation and Write Strategies
Your cache makes applications faster. But how do you ensure users don't keep receiving outdated data?
Three important concepts every system design interview candidate should understand.
1. Time-To-Live (TTL)
Problem: Cached data can become outdated as the source data changes.
How it works:
Every cache entry gets an expiration time.
Once the TTL expires, the entry becomes eligible for removal or refresh.
The next request may fetch fresh data from the database.
Example: A product listing is cached for 5 minutes. After expiration, the application fetches updated product details.
Best for: Data that can tolerate some staleness.
Trade-off: A short TTL increases database load. A long TTL increases the chance of serving stale data.
2. Cache Invalidation
Problem: Database data changes, but the cache still contains the old value.
How it works:
Update the database.
Invalidate or delete the corresponding cache entry.
The next cache miss loads the updated value.
Example: When a product's price changes, delete its cached price so the next request retrieves the latest value.
Best for: Applications that need fresher data after updates.
Trade-off: Incorrect invalidation logic can leave stale entries behind. Concurrent updates can also introduce race conditions.
3. Write Strategies and Consistency
Problem: Updating the cache and database independently can cause them to disagree.
Common approaches:
Write-through: Update the cache and database as part of the same write flow.
Write-back: Update the cache first and persist to the database asynchronously.
Cache-aside: The application updates the database and invalidates or refreshes the cache.
Example: A shopping application updates a product price in the database and invalidates the cached price.
Best for: Choosing a consistency model that fits the application's requirements.
Trade-off: Stronger coordination can increase latency, while asynchronous writes and invalidation can introduce consistency risks.
4. Quick Comparison
TTL: Expire cached entries after a configured duration.
Invalidation: Remove or refresh entries when data changes.
Write-through: Coordinate cache and database updates.
Write-back: Persist cached writes asynchronously.
Cache-aside: Let the application manage cache reads and updates.
5. Interview Tip
When designing cache consistency, ask:
How stale can the data be?
What should happen when the database value changes?
Can two requests update the same key concurrently?
What happens if cache invalidation fails?
Is eventual consistency acceptable, or is stronger consistency required?
FAANG Takeaway
There is no single cache consistency strategy for every workload. Choose based on data freshness, write patterns, latency requirements and failure scenarios.
A fast cache is useful only when its consistency guarantees match what the application needs.
Next: Day 9/30
Cache Stampede Prevention: Locks, Request Coalescing and Refresh-Ahead