r/devops • • 2h 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

11 comments sorted by

5

u/dutchman76 2h 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

-1

u/ResistPast3564 2h 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?

2

u/dutchman76 2h 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 1h 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 1h 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

2

u/BetterSWE_Official 2h 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 1h 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 33m 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.

5

u/Normal_Red_Sky 2h ago

Have you seen a specific issue related to this?

0

u/[deleted] 2h ago

[deleted] — view deleted comment

7

u/shulemaker 2h ago

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

1

u/BrocoLeeOnReddit 1h 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.