Fastify · Hard · Idempotency
A project reservation endpoint already handles a single successful request. Under duplicate delivery or concurrent requests, it rejects valid retries or oversells the final seat. A repository write can also fail. Repair the consistency boundary while preserving the HTTP application and public receipt format.
Task
Make reservations atomic and idempotent within one service process, with deterministic concurrency and failure tests.
Contract
- Request:
POST /projects/:id/reservations;:idis the project being reserved. Send the body as JSON withContent-Type: application/json. - JSON body:
quantity— seats requested; integer from1through10.expectedVersion— project version last read by the caller; positive integer.requestId— identity of this reservation attempt; 1–40 lowercase letters, digits or hyphens. Example:{"quantity": 1, "expectedVersion": 3, "requestId": "reserve-42"}. - Authentication:
Authorization: Bearer fixture-alphaidentifies tenantalpha;fixture-bravoidentifies tenantbravo. The supplied authentication helper resolves the tenant. A query/body field is not an identity source. - Request delivery and storage: The same request may arrive again after a timeout. Different requests may overlap. Repository operations from different requests may overlap, and a write may fail. Use the provided single-process exclusive boundary. No distributed storage is part of this task.
Acceptance criteria
- Return HTTP
200with{id, quantity, remaining, version}. remainingis the seat count after subtractingquantity;versionincreases by exactly one.- Concurrent requests must not reserve more seats than exist.
- The key is the authenticated tenant plus
requestId. - Same key and same project, quantity and expected version: return the original receipt without changing stock again.
- Check for that receipt before checking the project’s current version.
- Same key with different reservation data: HTTP
409, codeIDEMPOTENCY_CONFLICT. - Malformed body: HTTP
400, codeINVALID_RESERVATION. - Missing or unknown bearer token: HTTP
401, codeUNAUTHORIZED. - Missing, archived or another tenant’s project: HTTP
404, codeNOT_FOUND. - Version changed: HTTP
409, codeVERSION_CONFLICT. - Not enough seats: HTTP
409, codeINSUFFICIENT_SEATS. - Repository write failed: HTTP
503, codeRETRY_LATER. - Every error body is
{code: "THE_CODE"}. - Do not consume seats, increment the version or save a successful receipt.
- Retrying the same request must still be possible.
Constraints
- At most 10000 projects in the repository; reservation JSON body at most 16 KiB.
- Use the supplied repository and authenticated tenant resolver. No external database, network service or production credential is needed.
- The reservation guarantee is single-process only. Do not claim distributed or durable idempotency from an in-memory store.
- Keep response fields and error codes stable. Do not modify fixtures, manifests or evaluator tests.
Editable files
src/app.js— Framework router/controller, middleware and error response integration.src/service.js— Repair filtering/pagination or the reservation consistency boundary.src/helpers.js— Optional helpers; import from service when needed.tests/candidate.test.js— Add the required regression tests here using fixture(t), assert and node:test.