r/devops • • 3d ago

Discussion Do you monitor whether your Redis cache is serving stale data?

Working with a Postgres + Redis setup. We monitor hit rate, latency and memory, but nothing tells us when the cache is wrong.

Has this bitten you? How did you find out - users, an alert, something else?

And does anyone actually measure it, or is it a low TTL and hope?

0 Upvotes

22 comments sorted by

7

u/dutchman76 3d ago

That's the point of ttl so you know the data can be out of date that much. You have to design your app to tolerate that

-2

u/ResistPast3564 3d ago

Fair, the TTL is the staleness budget. How do you decide what it should be, though? And for data that can't tolerate much staleness, like prices, permissions or account status, do you skip caching it, or keep the TTL very short?

4

u/dutchman76 3d ago

Up to my boss, they decide how long old prices on the site are ok. And if there are one off hot fixes, then you know to flush the caches too.

At my company we go by gold and silver prices that update every 4 minutes system wide, so ttl are tuned to that

1

u/ResistPast3564 3d ago

Makes sense, tying the TTL to a 4-minute price feed is a clean rule. One last question on the hot fixes, has anyone ever forgotten the flush, and if so, how long did it take to notice?

1

u/dutchman76 3d ago

Yes, it was a hot fix, they noticed immediately : Me: try it now Them: nope, still wrong Me: oh I know what's wrong

3

u/BetterSWE_Official 3d ago

You can measure it cheaply by sampling. On a small percent of cache hits, also read from Postgres in the background, compare, and count mismatches as a metric. If you store an updated_at or version with the cached value you can even graph how stale the mismatches are. It's the only way I know to tell "TTL is fine" apart from "an invalidation path is broken".

-1

u/ResistPast3564 3d ago

Graphing how stale the mismatches are is a nice touch, since it separates a few seconds behind from hours wrong. Have you run this somewhere? If so, did it ever catch a broken invalidation path, and what turned out to be the cause?

1

u/BetterSWE_Official 3d ago

no war story of my own on this one, but the cause people usually find is a write path that skips the invalidation. a backfill script, a migration, an admin tool or a second service writing straight to postgres. the app's own writes are fine, so it only shows up as a slow trickle of hours-old mismatches.

9

u/Normal_Red_Sky 3d ago

Have you seen a specific issue related to this?

3

u/Motor_Plane_8036 3d ago

yeah usually a question like this comes right after a very painful post-mortem

0

u/[deleted] 3d ago

[deleted] — view deleted comment

11

u/shulemaker 3d ago

No, but I’m sure you’ve got an AI app to sell us.

1

u/BrocoLeeOnReddit 3d ago

Do you have a lot of issues with stale data? In our stack, it's not really a problem since our setup isn't write-heavy so we basically just use TTLs (depending on which queries we use different TTLs).

If you REALLY need to have Redis and PostgreSQL line up perfectly, you could do Change Data Capture (CDC), e.g. use Debezium to sync PostgreSQL's WAL to Redis or use Redis' own implementation RDI (Redis Data Integration): https://redis.io/docs/latest/integrate/redis-data-integration/

And to monitor it, you could do some sampling, e.g. check 0.1% of all cache hits against the actual database and log/count the differences.

1

u/Aide_Sea 3d ago

One sneaky cause I think is a slow read that grabbed the old row, then writes it to cache after the invalidation already ran. Code looks right but the stale value sits until TTL. Versioned keys maybe help with that.

1

u/Mean_Guest_5043 2d ago

Stale cache data can be easy to miss, so tracking cache invalidation and comparing key values against the source database seems worthwhile.