Redis and caching · Trade-off
Cache-aside: update or delete the key?
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
We cache product details in Redis using cache-aside. When a product's price changes, should we update the cache or delete the key? And should we touch the cache before or after writing the database?
Then
Follow-ups the interviewer asks next
- Walk me through the race where the cache ends up with the old price.
- Why delete instead of update?
- What if the delete fails?
- How does reading from a lagging replica make it worse?
- How would you invalidate from the database's change log instead of the app?
The answer
What a strong answer covers
- Cache-aside read: get the key; on a miss, read the database and set the key with a TTL.
- On write: update the database, then delete the key. Deleting is idempotent, and two writers can't leave their values in the wrong order.
- Deleting before the write is worse: a reader can refill the old value before the write commits.
- The remaining race: a reader misses and reads the old row, the writer commits and deletes, then the reader sets the old value; it stays stale until the TTL. Mitigations: short TTLs for volatile fields, a delayed second delete, version numbers in the value with a compare-and-set (Lua, or Redis 8.4's conditional
SEToptions). - A failed delete: retry through a queue or an outbox; the TTL is the safety net.
- Refilling from a lagging replica writes old data into the cache; refill from the primary right after writes.
- Change data capture (Debezium reading the binlog or WAL) can delete keys for every change, whichever code made it.
- Where correctness matters (the price at checkout), read the database.
The red pen
Common traps
Avoid:"update the cache in the same code, it's fine"; no TTL as a safety net; caching the checkout price.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.