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 KEEPTTLfor 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 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 |
SET ... PX | Milliseconds; 30000 is the ownership-guide example, not a universal downstream budget | Same atomic lease command | lock guide |
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 |
PTTL | remaining milliseconds, -2 absent, -1 exists without expiry | Bounded waiting/expiry inspection | PTTL |
maxmemory | Documentation example 100mb, resized below process capacity with buffer headroom | Dedicated cache configuration | eviction |
| Eviction policy | Example allkeys-lru; volatile policies need expiring keys; noeviction returns an error for commands that cache new data | Disposable cache | eviction |
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) | 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 |
-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, 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