Bỏ qua, tới nội dung chính

Kiến trúc · ADR 0030

Redis-backed throttling and refresh-rotation single-flight

Trạng thái
accepted
Ngày
2026-09-29
Người quyết định
Alex

Context

Review items 1 and 2 (docs/research/architecture-review-2026-09.md). The throttler counted in process memory: it reset on every Render cold start and each replica had its own count, so the only brake on password guessing was weaker the more we scaled. Rotation de-dup lived in each web instance's in-process Map; two instances presenting one refresh token both reached the API, one lost the atomic revoke, and reuse detection (ADR-0007) signed the user out everywhere.

Decision

  • Throttler storage in Redis (infra/redis/redis-throttler.storage.ts): one Lua script per hit (fixed window + block, same semantics as the in-memory store), keys throttle:{name:key}:*. All named throttles (LOGIN_, REGISTER_, FORGOT_THROTTLE, global) are unchanged.
  • Redis down → per-instance memory, not fail-open, not fail-closed. Closed would turn a Redis outage into a full API outage (the global throttle guards every route); open would drop the login brake. Local counters keep the brake (weaker by the replica count) and the service up. Logged once per transition.
  • Rotation single-flight in Redis (modules/auth/rotation-single-flight.ts): SET NX PX 15s lock keyed by a hash of the token, renewed (owner-checked) while the leader rotates. The leader publishes its outcome (SET NX: first write wins) — new pair or 401 for 10 s, "unavailable" for 1 s — and keeps the lock for the 10 s window. Followers poll and return that outcome. Only the leader ever rotates: a lock gone without an outcome, a lost publish or a wait > 6 s answers 503 (the web's unavailable, cookies kept), because re-rotating a possibly spent token is the reuse sweep we are avoiding. The outcome is AES-256-GCM-encrypted under a key HKDF-derived from the raw old token, which is never stored — a Redis dump yields nothing.
  • Rotation is one DB transaction (revoke + read + create), so a DB error leaves the token live and the retry after the 1 s "unavailable" window is an ordinary rotation, not a replay.
  • Failure policy: only the first Redis read failing (no leader can be known) → rotate directly (old behaviour); any later Redis failure → 503. Contract rotated / refused / unavailable unchanged.
  • Both use a fail-fast connection (RedisService.fastClient: no offline queue, 1 s command timeout; boot waits up to 3 s for it). While Redis fails, the throttler counts locally and retries Redis only every 5 s, so a hung Redis does not add its timeout to every request. /health/live is @SkipThrottle — it touches no dependency (ADR-0032 keep-warm).

Alternatives considered

  • @nest-lab/throttler-storage-redis — a second client and a dependency for ~30 lines of Lua.
  • Accept a grace window in the database — ADR-0021 rejected it; this bounds reuse to 10 s, in Redis, and only for a caller already holding the old token.

Consequences

  • For 10 s after a rotation, the old token returns the same new pair instead of tripping reuse detection — unless that pair was revoked since (logout, password change, reuse sweep): a cached pair is re-checked with one indexed read and refused if dead. The price of multi-instance safety.
  • Tests use ioredis-mock (runs the Lua via fengari); e2e call resetRedis() between tests.
  • ADR-0007's "counters are in memory" consequence no longer holds.