fix(idempotency): shared bounded store + in-flight coalescing - #234
Open
nyuiela wants to merge 1 commit into
Open
fix(idempotency): shared bounded store + in-flight coalescing#234nyuiela wants to merge 1 commit into
nyuiela wants to merge 1 commit into
Conversation
Close the per-middleware Map hole that let the same key execute twice across mounts, add a hard entry cap with soonest-expiry eviction, and coalesce concurrent same-key requests onto one in-flight handler. Replay semantics stay response-body based; headers are never cached. Cross-replica sharing waits on the separate persistence issue.
nyuiela
force-pushed
the
fix/221-idempotency-shared-store
branch
from
August 21, 2026 09:28
3cf7a91 to
a2570ac
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #221.
Replaces the per-middleware closed-over
Mapwith a process-wideMemoryIdempotencyStorethat is shared across mounts, hard-capped, and coalesces concurrent same-key requests onto a single handler execution.Design decisions
Where the state belongs. Process-wide in-memory store shared by every
idempotency()mount that does not inject its ownstore. This service still has no persistence layer (compression/cors/expressonly), so bolting on Redis here would invent a second stack ahead of the tracked persistence issue. Cross-replica duplicates remain a known limit until that store lands; this PR closes the within-process holes (cross-mount + concurrent) without an external dependency. Sequencing: swapIdempotencyStorefor a Redis/DB implementation once persistence exists — the middleware options already accept an injectablestore.What a replay returns. Keep stored-response replay (status + JSON body). Clients of settlement APIs expect a safe retry to return the original outcome, not a conflict that forces a separate GET. Returning 409/422 on every duplicate would break the existing contract and the curl walkthrough in the README.
Eviction and bounds. Default TTL unchanged (
IDEMPOTENCY_TTL_MS/ 24h). Hard cap default 1024 entries: purge expired on write, then drop soonest-to-expire live entries until under the cap.MemoryIdempotencyStore(maxEntries)is injectable for tighter bounds in tests.Sensitive header audit
Cached entries store only
{ status, body, expiresAt, bodyHash }. Response headers (Set-Cookie,Authorization, custom secrets) are never written into the cache. Covered by a regression test.Public options
IdempotencyOptionsstays backward compatible:ttlMsunchanged. Additive optional fields:store,maxEntries(used when constructing a private store in tests; production continues to use the process-wide default).Test plan
3 * maxEntriesdistinct keys →store.size() <= maxEntriesnpm run lint && npm run build && npm test(42 suites / 493 tests)