A catalog endpoint reads product summaries through Redis. Most requests are fast, but latency spikes after a popular key expires and customers sometimes see outdated descriptions. In an interview, define the source of truth and the acceptable staleness before choosing a Redis command. A cache hit for a product description and an authoritative check of available inventory are different promises.
Follow a read through a miss
1. How does cache-aside fill the key?
The application checks Redis. On a miss, it reads the database and fills the cache with an expiration. Use a single command to set both value and TTL, rather than a separate write and expire that can be interrupted between commands:
SET product:42:summary "New desk lamp" EX 300
The example uses seconds and a simple string. In a real key scheme, include dimensions that change the response, such as locale, or compute those parts outside the cached value. Choose the TTL according to the agreed staleness bound and source load, not a universal rule. A TTL limits the residency of that fill; it does not prove that the value was current when it was cached.
2. Why can a key disappear before its TTL ends?
Expiration and eviction are different. A key is due to expire at its deadline. Under a configured memory limit, Redis can remove keys earlier depending on its eviction policy. With noeviction, writes that need additional memory can instead fail when the limit is reached. The application must tolerate misses either way. Check expired and evicted counters separately rather than reacting to every missing key by increasing the TTL.
Consider concurrent readers and writers
3. Does invalidating after a database write eliminate stale fills?
No. A reader can miss, read the old database value, and pause. A writer commits a new value and deletes the cache key. The reader resumes and writes the old value back. Invalidating after commit avoids some races but not that ordering. For strict freshness, serve the authoritative read or use a design that checks versions before publishing a fill. For a best-effort display cache, document the stale window and decide whether the simpler design meets it.
4. What if a hundred requests miss the same key?
Without coordination, all hundred can load the same record from the database. Collapse concurrent fills when possible. A per-process mechanism only combines callers within that process; it does not cover every replica. Randomizing TTLs can spread expiry for different keys, but will not separate requests for one hot key. If the interface permits stale responses, refresh in the background while serving an explicitly bounded stale value.
5. Is bypassing Redis a safe fallback?
Only if the database can handle the extra load. Timeout the cache lookup within the request budget and limit concurrent fallback requests. Consider rejecting or degrading nonessential work instead of moving a large surge downstream. Track cache latency, hit rate, database saturation, and fallback volume together; a healthy hit rate can hide a single high-traffic key that causes trouble whenever it expires.
Rehearse two failure timelines
- Put a delayed reader on either side of an invalidation and label the final cached version.
- Simulate expiry of a single hot key, then imagine Redis is unavailable entirely. Name the different controls each scenario needs.
- Choose one field whose staleness is acceptable and another that must come from the source of truth.
Compare the fallback budget with the microservices failure walkthrough, then answer a sample card using Mentaro's free practice demo.