Django · 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
- New successful reservation: 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. - Repeating a request: 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: HTTP409, codeIDEMPOTENCY_CONFLICT. - Other errors: Malformed body: HTTP
400, codeINVALID_RESERVATION. Missing or unknown bearer token: HTTP401, codeUNAUTHORIZED. Missing, archived or another tenant’s project: HTTP404, codeNOT_FOUND. Version changed: HTTP409, codeVERSION_CONFLICT. Not enough seats: HTTP409, codeINSUFFICIENT_SEATS. Repository write failed: HTTP503, codeRETRY_LATER. Every error body is{code: "THE_CODE"}. - State after a failed write: Do not consume seats, increment the version or save a successful receipt. Retrying the same request must still be possible.
Constraints
- At most 10000 records. Requests have the exact quantity, version and key bounds listed above. Test data is local and disposable.
- Single application process only. In-memory receipts are not durable across restarts. Use the supplied exclusive boundary for the complete reservation decision.
- Use the provided Django ORM model. SQLite is used for isolated tests only; passing them does not prove PostgreSQL multi-process locking correctness.
Editable files
src/app.py— Real framework application, HTTP integration and error mapping.src/service.py— Repair the domain selection or consistency operation.src/helpers.py— Optional implementation helpers.tests/test_candidate.py— Add task-specific regressions through api.get/api.post.