I run a Redis instance that holds several logical databases. It has an eviction cap, and for a long time I considered it boring infrastructure. That assumption is exactly why a slow leak sat in it for weeks. It ended up holding 1.5 million junk records, and it came within a few percent of pushing live data out of the cache.
The symptom
There was no outage. What I had was a sweep of the datastores where I looked at every logical database in the instance and counted what was in them. One of them held about 1.53 million records. That was roughly 72 percent of everything in the entire instance. Yet only about 634 of those records had an expiry set.
A cache where 99.96 percent of entries never expire is not a cache. It is a landfill.
I checked the memory history. At its worst, memory had peaked at 96 percent of the cap.
What I wrongly assumed
I had set a memory cap and an LRU eviction policy on the instance earlier. I considered the leak problem handled. If memory fills up, Redis evicts the least recently used entries, and life goes on.
The flaw in that reasoning is the word “across.” My eviction policy applied across all databases in the instance, not within the one that was leaking. Once memory hit the cap, Redis would start evicting the least recently used entries from every logical database, including the ones holding live application data. The dead pile in that one database was not harmless filler. It was crowding out things that applications actually needed. A leak behind an LRU cap does not fail loudly. It silently converts your cache into a worse cache, and the cache hit ratio quietly drops. Mine was sitting at about 7.9 percent when I looked.
How I found the real cause
The entries in the leaking database had a recognisable shape: an identifier of the form jobname:<uuid> with variants carrying :state and :history suffixes. That is the storage layout used by Hangfire, the .NET background-job library, when it uses Redis as its backing store. Each background job leaves a record behind describing its state and history.
Hangfire is supposed to expire those records after a job finishes. The worker service that wrote them simply was not configuring any expiration. Every job it ever ran was still there.
The proof was in the contrast. Another of my apps uses the exact same library and the exact same storage pattern, in a different logical database. In that one, roughly 555 thousand entries all expired correctly on their own. Same library, same data shape, different outcome, so the difference had to be configuration in the worker. I had a known-good reference sitting right next door.
A quick way to see this kind of problem on any Redis instance:
# how many entries live in one logical database? redis-cli -n <db> DBSIZE # sample a few and check whether they ever expire redis-cli -n <db> --scan --count 100 | head -5 redis-cli -n <db> TTL "<one of the names printed above>" # -1 means "no expiry, lives forever"
A TTL of -1 on a record that is obviously transient is the tell.
The fix
There were two parts, and they are different jobs.
First, clean up. I wrote a one-time script that walked the database and deleted the orphaned job records while leaving anything that still had a live reason to exist. Afterwards the database held 1,066 entries, of which 633 had an expiry. Memory use fell from the multi-gigabyte range to a small fraction of the cap, and the eviction counter read zero.
Second, stop the bleeding by configuring expiration in the worker itself, matching the working app. A cleanup only proves the past, so the record count is the cheap regression signal for the future: if it starts climbing again, the leak has returned.
There is a wrinkle worth admitting. My own execution log for the cleanup still showed that step as not done, even though the live numbers proved it had run. When the notes and the system disagree, trust the system.
While I was in there: WATCH on every GET
Once I was looking at command statistics anyway, I noticed something odd. The instance had processed about 1.2 billion plain GET commands. It had also processed about 1.2 billion WATCH commands, and about 1.19 billion UNWATCH commands. The WATCH count matched the GET count to within a couple of thousand calls out of 1.2 billion.
redis-cli INFO commandstats | grep -E 'cmdstat_(get|watch|unwatch):' # cmdstat_get calls=1201552553 usec_per_call=0.52 # cmdstat_watch calls=1201550317 usec_per_call=1.14 # cmdstat_unwatch calls=1190990527 usec_per_call=1.92
Something, almost certainly a client library, was wrapping every single plain read in optimistic-locking calls. The total CPU spent on the locking was about 1.01 hours of single-threaded time, versus 0.17 hours for the reads it wrapped. That is 5.8 times more CPU spent guarding the read than doing the read, plus roughly 2.4 billion extra round trips.
And yet: it did not matter. Across the whole instance, all command CPU added up to under one percent of wall-clock time. Redis was loafing.
I resisted the urge to treat it as an incident. It is a genuinely strange thing worth tracing to the client library before anyone decides to “scale Redis.” The same habit applies to large values: Redis is single-threaded, so a large write blocks every client, and the slowlog tells you what that actually costs before you panic. In my case the total blocked time came to 0.006 percent of wall time. Real, but not an emergency.
Why I did not just raise the memory cap
The easy response to an instance that reached 96 percent was to give it more room. I decided against it for the same reason I would not widen a drain to deal with something that keeps pouring junk into it. More headroom would have delayed the same event by a few weeks and made the eventual cleanup bigger. The cap was doing its job by telling me something was wrong. Reading the signal was better than silencing it.
Numbers recap
- Leaked records in the bad database: 1,527,859, falling to 1,066 after cleanup
- Entries with an expiry before: about 634. After: 633 of 1,066
- Peak memory: 96 percent of the cap. After cleanup: a small fraction of it, zero evictions
- Cache hit ratio at the time: about 7.9 percent
- WATCH calls: 1.2 billion, matching GET one for one
- Total Redis CPU duty cycle: under 1 percent
Lessons
- An eviction cap is a safety net, not a leak fix. When the policy spans every database, a leak in one database eats cache from all the others.
- Count entries per logical database and look at how many have an expiry. If the fraction is tiny and the data is transient, you have a leak.
- Find a working twin. A second app using the same library correctly turned a vague suspicion into a specific diagnosis in minutes.
- Verify the cleanup with live numbers, not with your notes. And keep a cheap regression signal, like a record count, for every fix.
- Measure before you alarm. A weird-looking access pattern and a big value are only problems if the stall time adds up. Mine added up to 0.006 percent.