Cache-Aside Consistency Recovery
Design a cache-aside data path that bounds stale reads and repairs cache/database divergence after partial failures.
Problem statement
For this exercise, assume a product-detail service treats a relational database as authoritative. Reads check a distributed cache and load the database on a miss; writes commit the database before invalidating the cached key.
Design the read, write, invalidation, and repair paths so an acknowledged database write is never lost and a cache failure cannot leave a value stale for more than 30 seconds.
The system must tolerate invalidation retries, delayed messages, cache-node loss, concurrent readers and writers, and a single availability-zone failure. Exclude multi-object transactions and client-side caching.
What to build
- Return one product version from cache when safe and populate the cache from the authoritative database on a miss.
- Commit product updates durably before scheduling idempotent cache invalidation for the affected key.
- Prevent a concurrent miss from repopulating an older database version after a newer write has committed.
- Retry failed invalidations, observe stale-risk windows, and repair cache/database divergence within 30 seconds.
At scale
- Assume 200,000 product keys, 20,000 reads per second, and 1,000 writes per second at peak.
- Assume a healthy cache-hit target of 95 percent and p95 reads under 100 milliseconds.
- Assume cached data may be stale for at most 30 seconds during a cache or delivery failure, while acknowledged database writes must never be lost.
- Assume the service must recover from one cache-node or one availability-zone failure without mixing product versions in one response.