Repository navigation
fix(idempotency): reserve keys before execution and never cache 5xx - #1865
Open
devogechukwu wants to merge 1 commit into
Open
devogechukwu wants to merge 1 commit into
devogechukwu wants to merge 1 commit into
Conversation
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.
Closes #1721
Summary
Implements atomic reservation, commit, and release semantics for mutating routes (
settleandwithdraw), preventing concurrent execution races and stopping transient 5xx responses from poisoning idempotency caches for 24 hours.Protocol Sequence Diagram
Client Server / Route Idempotency Store
│ │ │
│── POST /api/streams/:id/settle ────────────────────────────────────►│
│ (Idempotency-Key: K) │ │
│ │── 1. Reserve key (setIfAbsent) ────►│
│ │◄─ [in_flight, match] ───────────────┤ (If in-flight: 409 IDEMPOTENCY_IN_FLIGHT)
│ │◄─ [in_flight, stale] ───────────────┤ (Takeover if > IDEMPOTENCY_INFLIGHT_TTL_MS)
│ │◄─ [committed, match] ───────────────┤ (Replay cached response with 200)
│ │◄─ [acquired] ───────────────────────┤
│ │ │
│ │── 2. Execute Handler ───────────────┤
│ │ │
│ ├─── Status = 2xx or deterministic 4xx┤
│ │ (400, 404, 409, 422) │
│ │── COMMIT ──────────────────────────►│ (TTL = 24 hours)
│ │ │
│ ├─── Status = 5xx, 429, or throw ─────┤
│ │── RELEASE (Delete reservation) ────►│ (Enables retry with same key)
│◄── Response ──────────────────┤ │
Key Changes
app/lib/idempotency.ts):{ state: "in_flight", fingerprint, reservedAt, expiresAt }viasetIfAbsent.409with codeIDEMPOTENCY_IN_FLIGHTandRetry-After: 1.409with codeIDEMPOTENCY_CONFLICT.IDEMPOTENCY_INFLIGHT_TTL_MS(default 30s) and logs takeover with correlation context.503 IDEMPOTENCY_STORE_UNAVAILABLEif the store read/reservation fails.400,404,409,422) with 24h TTL.502 SETTLEMENT_FAILED), 429, or thrown errors so retries re-execute.app/api/streams/concurrency.test.ts):jest.config.jsquarantine.openapi.json):app/lib/idempotency.reservation.test.ts):app/lib/idempotency.tsis 96.69% (≥95%).Test Output