diff --git a/.changeset/single-flight-wording.md b/.changeset/single-flight-wording.md new file mode 100644 index 0000000..4966236 --- /dev/null +++ b/.changeset/single-flight-wording.md @@ -0,0 +1,5 @@ +--- +"@leejpsd/nextjs-cache-handler": patch +--- + +Docs: correct the `singleFlight` description. The lock reports which instance saw a stale entry first (`cache.stale.refresh.leader` / `.follower`); it does not stop other instances from refreshing, because Next.js decides to refresh after `get()` returns. The README, API reference, option JSDoc and agent skill said or implied that followers skip the refresh. No behavior change. Also links #34 for an in-process L1 and `fetch` miss coalescing wrapper. diff --git a/README.md b/README.md index 55b31cd..8ab7a33 100644 --- a/README.md +++ b/README.md @@ -296,12 +296,14 @@ refresh — N parallel re-renders for the same key, each hitting your origin once. `singleFlight: true` adds an opt-in Redis lock (`refresh-tag-lock.lua`, -default TTL 10s) at the SWR boundary. The first instance to acquire it -becomes the **leader** and runs the refresh; the rest become -**followers**, keep serving the same stale entry, and wait for the -leader's write to land. The lock is observability-only at the handler -layer — Next.js still drives the actual refresh; we just suppress the -stampede. +default TTL 10s) at the SWR boundary. The first instance to read a stale +entry acquires it and is reported as the **leader**; the others are +reported as **followers**. The lock is a signal, not a guard: Next.js +decides to refresh after `get()` returns, so every instance still serves +the stale entry and still runs its own refresh. What you get is a +per-key count of how many instances refresh the same entry at once, +which tells you whether duplicate refresh work is worth solving in your +setup. ```js createCacheComponentsHandler({ @@ -315,17 +317,24 @@ Two new `MetricEvent` types appear on `onMetric`: | event type | meaning | |---|---| -| `cache.stale.refresh.leader` | this instance just acquired the lock and is the designated refresher | -| `cache.stale.refresh.follower` | another instance holds the lock; we serve stale and skip the refresh | +| `cache.stale.refresh.leader` | this instance acquired the lock: it was the first to see this entry stale within the lock TTL | +| `cache.stale.refresh.follower` | another instance holds the lock; this one serves the stale entry too, and Next.js still refreshes it here | If lock acquisition fails (Redis hiccup, TTL race), the handler -defaults to the follower path — the stale entry is always served, never -dropped. This is intentional: the lock is an optimization, not a +reports the follower path — the stale entry is always served, never +dropped. This is intentional: the lock is a measurement, not a correctness-critical primitive. -When **not** to enable single-flight: small fleets (1–2 instances) where -Next's per-process serialization already covers the stampede risk. -Adding a Redis round-trip per stale read isn't free. +When **not** to enable single-flight: small fleets (1–2 instances), +where there is little duplicate work to measure. Adding a Redis +round-trip per stale read isn't free. + +The handler does not coalesce refreshes or misses across instances, and +it keeps no in-process copy of entries in front of Redis. If you need +either, [#34](https://github.com/leejpsd/nextjs-cache-handler/issues/34) +has a small wrapper for the singular `cacheHandler` (in-process L1, +shared reads, a latch for concurrent `fetch` misses), with notes on what +Next.js already de-duplicates. See [`docs/architecture.md`](./docs/architecture.md#single-flight-refresh-lock-v02-) for the full state machine and a reference to the Lua script body. diff --git a/docs/api.md b/docs/api.md index 9b0eb5c..b897ebc 100644 --- a/docs/api.md +++ b/docs/api.md @@ -45,7 +45,7 @@ ctx contract are accepted (`ctx.cacheControl.revalidate` / `kind` and | `memoryMaxEntries` | `number` | `1000` | LRU cap for the in-memory fallback (and its tag index) | | `compression` | `"gzip" \| "brotli"` | off | transparent Redis value compression (node:zlib). Reads auto-detect, so mixed fleets interoperate; < 1 KiB stored plain | | `staleWhileRevalidate` | `boolean` | `true` | plural handler only: serve stale in the SWR window | -| `singleFlight` | `boolean` | `false` | opt-in refresh lock at the SWR boundary (stampede suppression) | +| `singleFlight` | `boolean` | `false` | opt-in refresh lock at the SWR boundary (leader/follower metrics; does not suppress refreshes) | | `singleFlightLockTtlSec` | `number` | `10` | lock TTL for `singleFlight` | | `isBuildPhase` | `() => boolean` | `NEXT_PHASE === "phase-production-build"` | build-phase gate override | | `hashTag` | `boolean` | `false` | wrap namespace in `{}` — **required on Redis Cluster** (multi-key Lua) | diff --git a/skills/nextjs-redis-cache/SKILL.md b/skills/nextjs-redis-cache/SKILL.md index 6431644..d2664f7 100644 --- a/skills/nextjs-redis-cache/SKILL.md +++ b/skills/nextjs-redis-cache/SKILL.md @@ -114,7 +114,7 @@ Facts that prevent misdiagnosis: | `compression: "gzip"\|"brotli"` | off | Large RSC payloads; safe to enable on a live cache (reads auto-detect) | | `memoryMaxEntries` | 1000 | LRU cap of the in-memory outage fallback | | `fallback: "auto"\|"always"\|"never"` | auto | `never` = strict (miss on Redis failure); `always` = no Redis (build/dev) | -| `singleFlight: true` | off | Many instances + observed duplicate origin work at revalidate boundaries | +| `singleFlight: true` | off | To measure duplicate refresh work across instances at revalidate boundaries (leader/follower metrics; it does not suppress refreshes) | | `abortTimeoutMs` | 1500 | Per-op Redis deadline; timeouts degrade to miss | | `hashTag: true` | off | REQUIRED on Redis Cluster | | `onMetric` | — | Wire `createOtelMetricEmitter()` from `@leejpsd/nextjs-cache-handler/otel` | diff --git a/src/types.ts b/src/types.ts index 38bc710..822179b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -271,16 +271,17 @@ export interface CacheHandlerOptions { /** * Single-flight refresh lock for the SWR boundary. * - * When enabled, only one handler instance per cache key attempts a - * background refresh during the stale window. Other instances continue - * serving the same stale entry until the leader publishes a fresh one or - * the lock TTL expires. Prevents N parallel refreshes (cache stampede) - * when many instances cross the `revalidate` boundary at the same time. + * When enabled, the first handler instance to read a stale entry takes a + * short Redis lock for that cache key and is reported as the leader + * (`cache.stale.refresh.leader`); the others are reported as followers + * (`cache.stale.refresh.follower`). The lock is a signal, not a guard: + * Next.js decides to refresh after `get()` returns, so every instance + * still serves the stale entry and still runs its own refresh. Use the + * two events to measure how many instances refresh the same entry at + * once. * - * Default: `false`. The marginal benefit at small scale is small because - * Next.js already serializes per-cacheKey refresh per process; turn this - * on when you have many instances and observe duplicate origin work - * around the revalidate boundary. + * Default: `false`. Costs one Redis round trip per stale read; with one + * or two instances there is little duplicate work to measure. */ singleFlight?: boolean;