Redis · 30 November 2026

How long should a Redis key live? Lifetimes, caps and cold keys

When Redis fills up, the instinct is to add memory. Often the memory is full of things nobody will read again. Here is how to see what your keys are doing, and why a cap on lifetimes helps more than a bigger server.

By Shane Rounce · 4 minute read

The problem: Redis is full, and the site is no faster

Redis has a memory limit. When it is reached, Redis either evicts keys or refuses writes, depending on its policy. Eviction works well when most keys are useful. It works badly when much of the memory is holding values that were written once and never asked for again.

That is the normal state of a WordPress object cache left to itself. Plugins ask for week-long and month-long transients as a matter of habit, and some writes ask for no expiry at all. Most of those keys are cold within the hour.

Why not just add memory?

Because the working set, the data your pages actually re-read, does not grow when you add memory. Only the leftovers do. A bigger limit gives cold keys more room and puts the same problem off by a few weeks.

Capping lifetimes does the opposite. It keeps the memory for the data that sites come back for.

Does a shorter lifetime mean more cache misses?

Not for anything in use. A value that is read often is rebuilt and stored again when it expires, and that costs one rebuild a day under a one day cap. What changes is that a key nobody reads stops taking up memory after a day instead of a week or a month.

A write asks forIt is stored for
No expiry1 day (the backstop)
1 hour1 hour
12 hours12 hours
1 day1 day
1 week1 day
1 month1 day
Pick the lifetime a plugin asks for, and see what is stored under a one day cap.

Step 1: look at lifetimes, not only counts

Most dashboards show how many keys there are and how much memory they use. Neither tells you whether the keys are any use. Sample some lifetimes instead:

redis-cli --scan --pattern 'mysite:*' | head -200 | while read k; do
  echo "$(redis-cli ttl "$k") $k"
done | sort -n | tail -20

TTL answers in seconds. -1 means the key has no expiry at all. Look for those, and for anything measured in weeks. redis-cli info keyspace gives the average for a whole database in its avg_ttl figure, in milliseconds.

Step 2: check the eviction policy

redis-cli config get maxmemory-policy

For a pure cache, allkeys-lru is the usual choice: when memory is full the least recently used keys go first. With noeviction, a full Redis refuses writes instead, which a cache should treat as a miss and carry on. I cover that side in what happens when Redis goes down.

Step 3: cap what you can control

In your own code, give every write a lifetime that matches how long the value is really useful, and think in hours.

For everything else, cap it at the cache. In Ace Redis Cache every object cache write honours a maximum lifetime, including writes that ask for a longer one:

  • set, add, set_multiple and the transient path all store for at most the smaller of the requested lifetime and the cap.
  • Increments and decrements pull an existing longer lifetime down to the cap.
  • A write with no expiry gets a one day backstop instead of living for ever.
  • The cap defaults to 86400 seconds, one day. It can be changed per site or turned off, and the ACEREDIS_OC_MAX_TTL constant in wp-config.php wins over the setting.
  • The generation counter that the cache uses to flush without scanning is exempt and keeps no expiry.

The page cache has its own optional cap on fresh time plus stale grace. Both are in the configuration reference.

The value that was never stored

There is a related trap that looks like the opposite problem. You cache something large, and nothing gets faster.

An object cache usually has a maximum value size, to protect Redis from very large values. In ARC the default is 65536 bytes. Anything bigger is not stored. The call returns, the next request finds nothing, and the expensive thing is built again.

I met this with a set of sitemaps that took several seconds each to build and were far over the limit. Caching them as transients changed nothing. The fixes that work:

  • Store very large output as a file, written to a temporary name and renamed into place, and let the web server send it.
  • Compress large fragments before storing them.
  • Split a large structure into smaller entries that are each worth caching alone.
  • Time the second request. If it is as slow as the first, the value was not stored. There is more on this in why a transient is not saving.

Checklist

  • Look at the remaining lifetimes of keys in your Redis, not only how many there are.
  • Find the keys with no expiry and decide which of them truly must persist.
  • Set a maximum lifetime, and make sure explicit expiries respect it.
  • Know your object cache's value size limit, and time anything large you cache.
  • Add memory only after the cold keys have gone and the working set still does not fit.

If several sites share the instance, read sharing one Redis between several WordPress sites next. The requirements page lists what ARC needs from Redis itself.

All notes Get the latest release