How do I fix stale data with Redis cache-aside pattern and TTLs?
#1
I’m trying to implement a simple caching layer in my API using Redis, but I keep running into issues where stale data is being served after an update. I’ve set what I think are reasonable TTLs and I’m invalidating keys on write, but there’s still a small window where a get request fetches the old cached value. I’m not sure if my invalidation logic is flawed or if I’m misunderstanding the cache-aside pattern.
Reply
#2
Yeah I ran into this and TTLs did not save me from the stale window. I was invalidating on write but a read could still pull the old value before the invalidate finished. It felt like a race between writing the source of truth and clearing the old cache.
Reply
#3
I tried a few things and one approach that helped a bit was thinking of it as cache aside and making the write to the DB happen before I invalidate the cache.
Reply
#4
I keep hitting a little race window between the update and the cache invalidate when there are multiple app instances behind a load balancer. Could that race window be the real cause?
Reply
#5
Maybe the window is tiny and the real issue is something else like clock skew or an async write that never blocks caching.
Reply


[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Forum Jump: