CodePark/ questions / Caching patterns
START PRACTICINGPRACTICE
Backend question · Redis & caching › Caching patterns

In a cache-aside setup, a user updates their profile. After committing the database change, what should the service usually do with the cached user:42 entry?

Delete user:42 so the next read reloads it from the database

Delete the key, so the next read reloads it from the database. Deleting is idempotent: doing it twice gives the same result as doing it once. It also avoids a race between two writers. Imagine two requests update the user at the same time, and their cache writes arrive in the wrong order. The older value can end up in the cache.

"Overwrite
user:42 with the new value" looks fresher, but it is exactly where that race happens. "Leave it and let the TTL expire" serves stale (old) data for the whole TTL.