Why Cache Aside Can Fail: Hidden Risks and the Double-Delete Fix
Cache Aside is a popular caching pattern that reads from cache first and writes through the database before invalidating the cache, but concurrent read‑write scenarios can cause stale data; the article explains these pitfalls and recommends the double‑delete strategy to keep cache and database consistent.
What is Cache Aside?
Cache Aside is a cache‑update strategy that is the preferred solution for scenarios where strong consistency between cache and database is not required.
Read strategy
First try to read data from the cache; if the cache hits, return the data directly. If the cache misses, load the data from the database, return it to the client, and store it in the cache.
Write strategy
Update the database first, then delete the corresponding entry from the cache.
Problems with Cache Aside
1. Thread A reads data; the cache misses, so it fetches from the database (value 20). At this moment its CPU time slice ends.
2. Thread B performs an update, changes the database value to 21, and deletes the cache entry. Its CPU time slice ends, and then Thread A gets CPU time again.
3. Thread A writes the cached value 20 back, which is stale because the database now holds 21, leading to inconsistency between cache and database.
Inconsistent cache‑database data occurs when:
The read operation finds no data in the cache, forcing a database load.
A write operation happens concurrently with the read.
The read operation takes longer than the write.
During concurrent reads and writes, the read obtains an old value.
Solution
The double‑delete (cache‑double‑delete) approach can resolve cache‑database inconsistency.
Cache Aside works well for read‑heavy, write‑light scenarios, but it is unsuitable for write‑heavy workloads because frequent cache invalidations reduce cache hit rates.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.