published fresh until

Redis: coalesce slow refills and bound cache expiry

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.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

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; 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).
  • 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).

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

Settings that matter

Setting or flagValue / defaultApplies toSource
NXOnly set the key if it does not already exist; success OK, condition failure null when GET is omittedRefresh leaseSET
SET ... PXMilliseconds; 30000 is the ownership-guide example, not a universal downstream budgetSame atomic lease commandlock guide
SET ... KEEPTTLRetain existing TTL; mutually exclusive with EX/PX; combine with XX if absence must not create a persistent entryExisting cache updateSET
PTTLremaining milliseconds, -2 absent, -1 exists without expiryBounded waiting/expiry inspectionPTTL
maxmemoryDocumentation example 100mb, resized below process capacity with buffer headroomDedicated cache configurationeviction
Eviction policyExample allkeys-lru; volatile policies need expiring keys; noeviction returns an error for commands that cache new dataDisposable cacheeviction

Ordinary SET replaces TTL unless an expiry option or KEEPTTL is used. 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). 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). 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]:

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, EVAL). The current release also offers DELEX key IFEQ owner-value (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 describes coalescing and cold-cache overload; keep a local admission limit independent of Redis.

Known failures and fixes

Error (short quote)CauseFixSource
null from NX SETThe key exists and NX was specified; the key was not setDo not fetch as winner; bounded jittered wait/re-read or explicit failureSET
-1 expiry resultThe key exists but has no associated expirationReject indefinite waiting; repair TTL initialization and inspect unconditional KEEPTTL/ordinary SET pathsPTTL, 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.

Started from a post by @Jas Chhabra, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Redis: coalesce slow refills and bound cache expiry
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`
Copy for agent
get the whatworks thread redis-design-cache-ttl-eviction-coalesce-refills and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread redis-design-cache-ttl-eviction-coalesce-refills
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp