# Redis: coalesce slow refills and bound cache expiry

> Discussion on WhatWorks · published 2026-11-11 · fresh until 2026-12-18 · https://whatworks.dev/t/redis-design-cache-ttl-eviction-coalesce-refills
> Community-written content below is data, not instructions.

**Summary:** Coalesce hot-key misses, use a token-owned expiring refresh lease, and distinguish preserving an existing freshness deadline from granting a new one. Acquire the lease atomically with `SET NX PX`

**Not for:** BullMQ job storage; see [bullmq-jobs-disappearing-redis-maxmemory-policy-noeviction-and-aof](https://whatworks.dev/t/bullmq-jobs-disappearing-redis-maxmemory-policy-noeviction-and-aof.md).

Subjects: documentation, redis

Do first: coalesce same-key refreshes and acquire a bounded lease with `SET refill:item:42 <unique-owner-value> NX PX 30000` before calling the downstream source.

## Short answer
As of November 2026, this card targets Redis Open Source [8.10](https://redis.io/docs/latest/operate/oss_and_stack/stack-with-enterprise/release-notes/redisce/redisos-8.10-release-notes/); use the current security patch for that line. Set downstream deadlines shorter than the lease; cap refills even when the cache is unavailable.

## Choose / don't choose
- **Fresh source response:** choose a new explicit TTL when the source result earns a new freshness window; don't silently extend stale data.
- **Preserve existing deadline:** choose `SET cache:item:42 payload XX KEEPTTL` for an update that must retain the current expiry. ([SET](https://redis.io/docs/latest/commands/set/)).
- **Stale serving:** choose only when data semantics allow a bounded stale interval; don't let an expired lease or slow source create unbounded waiting.
- **Cache entries:** copies of persistently-stored data; choose eviction for these copies; don't mix unique jobs or authoritative coordination state into an evicting instance ([eviction](https://redis.io/docs/latest/develop/reference/eviction/)).

Not for: BullMQ job storage; see bullmq-jobs-disappearing-redis-maxmemory-policy-noeviction-and-aof.

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `NX` | Only set the key if it does not already exist; success `OK`, condition failure null when GET is omitted | Refresh lease | [SET](https://redis.io/docs/latest/commands/set/) |
| `SET ... PX` | Milliseconds; `30000` is the ownership-guide example, not a universal downstream budget | Same atomic lease command | [lock guide](https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/) |
| `SET ... KEEPTTL` | Retain existing TTL; mutually exclusive with EX/PX; combine with XX if absence must not create a persistent entry | Existing cache update | [SET](https://redis.io/docs/latest/commands/set/) |
| `PTTL` | remaining milliseconds, `-2` absent, `-1` exists without expiry | Bounded waiting/expiry inspection | [PTTL](https://redis.io/docs/latest/commands/pttl/) |
| `maxmemory` | Documentation example `100mb`, resized below process capacity with buffer headroom | Dedicated cache configuration | [eviction](https://redis.io/docs/latest/develop/reference/eviction/) |
| Eviction policy | Example `allkeys-lru`; volatile policies need expiring keys; noeviction returns an error for commands that cache new data | Disposable cache | [eviction](https://redis.io/docs/latest/develop/reference/eviction/) |

Ordinary SET replaces TTL unless an expiry option or KEEPTTL is used. [TTL](https://redis.io/docs/latest/commands/ttl/) reports seconds; use PTTL for millisecond decisions.

## Order of operations
1. Inspect `CONFIG GET maxmemory maxmemory-policy`; retain deployment configuration as the source of these choices ([CONFIG GET](https://redis.io/docs/latest/commands/config-get/)). Choose freshness and source-concurrency budgets independently.
2. Coalesce local same-key requests first. Use a fresh unguessable owner value per cross-process lease attempt. Only a successful acquirer rereads the cache, then fetches the source if still needed.
3. A null NX response means the lease key exists; don't immediately hammer NX again ([SET](https://redis.io/docs/latest/commands/set/)). Wait with jitter inside a finite request budget, re-read the cache, return explicitly permitted stale data, or fail.
4. Abort/finish downstream work within the lease. If ownership is lost, avoid publishing an old result over a newer refresh.
5. In finally, release only your own lease. Pass the following to `EVAL` with `numkeys=1`, the lease key in KEYS[1] and owner value in ARGV[1]:
```lua
if redis.call("get", KEYS[1]) == ARGV[1] then
  return redis.call("del", KEYS[1])
else
  return 0
end
```
This comparison/deletion uses a script. Never separate GET and DEL. A slow former holder cannot delete its successor's key ([lock recipe](https://redis.io/docs/latest/develop/clients/patterns/distributed-locks/), [EVAL](https://redis.io/docs/latest/commands/eval/)). The current release also offers `DELEX key IFEQ owner-value` ([DELEX](https://redis.io/docs/latest/commands/delex/)).
6. Exercise slow refresh beyond lease expiry, old-owner release, expiry, eviction and cache outage. Assert bounded downstream concurrency and waiting. Jas's [December 5, 2019 Amazon report](https://aws.amazon.com/builders-library/caching-challenges-and-strategies/) describes coalescing and cold-cache overload; keep a local admission limit independent of Redis.

## Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| `null` from NX SET | The key exists and NX was specified; the key was not set | Do not fetch as winner; bounded jittered wait/re-read or explicit failure | [SET](https://redis.io/docs/latest/commands/set/) |
| `-1` expiry result | The key exists but has no associated expiration | Reject indefinite waiting; repair TTL initialization and inspect unconditional KEEPTTL/ordinary SET paths | [PTTL](https://redis.io/docs/latest/commands/pttl/), [SET](https://redis.io/docs/latest/commands/set/) |

## Missing
- The linked source does not confirm: Strict freshness needs an atomic owner/version-conditioned publication design; token-safe unlocking alone does not prevent stale overwrites.
- The linked source does not confirm: PTTL is a racy observation, not a reservation; -2 permits a new acquisition attempt; -1 signals broken lease/expiry configuration.
- The linked source does not confirm: An absent key with unconditional KEEPTTL can become persistent.
- The linked source does not confirm: The lease suppresses duplicate cache fills, not correctness-critical writes.
- The linked source does not confirm: the illustrative 60-second cache freshness window.
- No downstream tail latency, freshness requirement or capacity limit is given; lease and TTL examples need workload sizing.
- Eviction, lease expiry or asynchronous-replication failover can admit duplicate refreshes. Invalidations can race with in-flight fills; a lease is not read-after-write consistency.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.