Skip to content

fix(idempotency): shared bounded store + in-flight coalescing - #234

Merged
Jagadeeshftw merged 1 commit into
AnchorNet-Org:mainfrom
nyuiela:fix/221-idempotency-shared-store
Aug 29, 2026
Merged

Jagadeeshftw merged 1 commit into
AnchorNet-Org:mainfrom
nyuiela:fix/221-idempotency-shared-store

Conversation

@nyuiela

@nyuiela nyuiela commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Closes #221.

Replaces the per-middleware closed-over Map with a process-wide MemoryIdempotencyStore that is shared across mounts, hard-capped, and coalesces concurrent same-key requests onto a single handler execution.

Design decisions

  1. Where the state belongs. Process-wide in-memory store shared by every idempotency() mount that does not inject its own store. This service still has no persistence layer (compression / cors / express only), 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: swap IdempotencyStore for a Redis/DB implementation once persistence exists — the middleware options already accept an injectable store.

  2. 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.

  3. 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

IdempotencyOptions stays backward compatible: ttlMs unchanged. Additive optional fields: store, maxEntries (used when constructing a private store in tests; production continues to use the process-wide default).

Test plan

  • Cross-instance: two middleware instances, shared store, same key → one execution
  • Bound: 3 * maxEntries distinct keys → store.size() <= maxEntries
  • Concurrent same-key → one handler run
  • Keyless mutating requests unaffected
  • Header audit: cached entry has no header fields
  • npm run lint && npm run build && npm test (42 suites / 493 tests)

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
nyuiela force-pushed the fix/221-idempotency-shared-store branch from 3cf7a91 to a2570ac Compare August 21, 2026 09:28
@Jagadeeshftw
Jagadeeshftw merged commit 77f9df6 into AnchorNet-Org:main Aug 29, 2026
Jagadeeshftw added a commit that referenced this pull request Aug 29, 2026
… (#231)

* feat(config): fail-fast validation + standalone typecheck script (#230)

* fix(#225): replace float arithmetic with bigint for exact monetary precision (#236)

* fix(#225): replace float arithmetic with bigint for exact monetary precision

* fix(#225): add all bigint migration files missing from previous commit

* feat(metrics): protect metrics reads and bound history [Issue #228] (#235)

Aggregate metrics (participant counts, liquidity totals, settlement
volume and fees over time) describe the network's operational state.
Exposing that publicly should be deliberate, not a side effect of the
write-only auth middleware, whose MUTATING_METHODS set left every GET
unauthenticated and unlimited.

- Auth: new metricsAuth guards GET /api/v1/metrics and /history. When
  API_KEY or the new read-only METRICS_API_KEY is set, reads require a
  matching x-api-key (401 otherwise); when neither is set they stay
  open, matching the existing write-auth model. METRICS_API_KEY unlocks
  metrics only, so a scraper never needs the write key.
- Rate limiting: opt-in limitReads flag on rateLimiter (default off, so
  global behaviour is unchanged) enabled only on the metrics mount via
  METRICS_RATE_LIMIT_MAX (default 120/min), so the history endpoint is
  not an unlimited load generator. Global read limiting and the shared
  store remain owned by the separate rate-limiter issue.
- Retention: history stays bounded at MAX_HISTORY = 50, now pinned by
  route-level tests (eviction of the oldest entry).
- openapi.ts declares an ApiKeyAuth scheme and marks both metrics
  operations as protected; README/CHANGELOG document the scraper path.

npm run lint, npm run build and npm test (43 suites, 509 tests) pass.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>

* fix(idempotency): share a bounded in-process replay store (#234)

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.

* fix: make audit log an explicit operator convenience buffer (#233)

* feat: implement memory-bounded bucket eviction for rate limiter and add operational documentation (#232)

* Add fail-fast config validation and standalone typecheck (#230) (#237)

- validateConfig() enforces required values before the server binds:
  API_KEY required in production, PORT valid 1-65535, non-negative
  rate-limit/idempotency values; warns loudly (non-prod) on open access.
- Wire validateConfig into createApp/getConfig in app.ts.
- Add typecheck script (tsc --noEmit) and a distinct CI Typecheck step.
- Extend config.test.ts with a validateConfig suite.

closes #230

Co-authored-by: ChainBid Developer <developer@chain-bid.io>

---------

Co-authored-by: Paranoa-dev <paranoa-dev@users.noreply.github.com>
Co-authored-by: Mauricio Gil | GramSeo Studio <gramseostudio@gmail.com>
Co-authored-by: jahswillb-dev <tech.jahswillb@gmail.com>
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: kaleel <128490484+nyuiela@users.noreply.github.com>
Co-authored-by: martinshub-tech <jambemail2003@gmail.com>
Co-authored-by: Oyakhilome Gift A <asekhamegift@gmail.com>
Co-authored-by: ChainBid Developer <developer@chain-bid.io>
Co-authored-by: Jagadeeshftw <92681651+Jagadeeshftw@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Idempotency replay cache is a per-middleware in-memory Map holding responses for 24 hours — duplicate settlements across replicas, unbounded memory

2 participants