Implement a case-review API for reviewers 29 and 31. Both may open case 47 at version 62, but the second save must not erase the first. Return a strong validator with the case representation and require it for a mutation. Compare version and update the stored case atomically, then return a new validator. For a report over 62 cases, persist a durable operation and enqueue intent before returning an accepted response; let its owner inspect a status resource later. Add one downstream analysis call with an overall deadline and a bounded retry policy. The client must preserve unsaved notes after a conflict or network uncertainty. Error bodies must identify recovery paths without copying private notes, credentials, or internal exceptions.
Project: conflict-safe case API
Build contract
- Require a current If-Match validator on case writes and reject missing or stale conditions without a partial update.
- Give analysis requests one retry owner, a deadline, and a stable idempotency key when commit outcome is uncertain.
- Return a report operation path only after its record and enqueue intent are durable; authorize status reads and result links.
- Use distinct HTTP status and machine codes for conflict, validation, denial, and temporary dependency failure.
Implementation checkpoint
function saveDecision(ifMatch, currentVersion) {
if (ifMatch === null) return 428;
if (ifMatch !== `"case-47-v${currentVersion}"`) return 412;
return 200;
}
console.log(saveDecision(`"case-47-v62"`, 63));
// Output: 412Cost and boundaries
An atomic version comparison adds little work to a keyed record update, but the UI needs a real conflict path. Retry attempts consume dependency capacity and can multiply under layered clients, so cap them within one deadline. Durable operation records and status polling add storage and request traffic; they are justified when work survives a request or browser session. A stable error code registry costs maintenance but prevents clients from parsing changing human prose. Record each operation's owner and expiry instead of treating a hard-to-guess identifier as permission.
Failure drill
Run two saves from version 62 at the same time; only one may apply. Pause the server between a separate version read and write to prove an unchecked implementation can lose data, then move the condition into the atomic write. Drop the downstream response after an accepted analysis request; a repeated call with the same key must not start a second job. Restart the API immediately after responding to a report request; its status resource must still exist. Finally return each planned error and verify that the browser neither reports success nor discards the user's draft on failure.
Acceptance checks
- A missing or stale validator prevents a write and keeps the rejected draft available for recovery.
- A timeout after commit and a bounded retry do not duplicate the downstream effect.
- A report operation survives process restart and only its authorized owner reads status and output.
- Error status and machine code identify recovery while responses omit raw private data.
Common Mistakes
- Checking a version before an unconditional update.
- Retrying an uncertain mutation without an idempotency contract.
- Returning accepted before writing the operation record.
- Sending status 200 with a free-text error for a failed save.
Related lessons
Conditional Writes and Lost-Update Prevention; Request Deadlines, Retries, and Backoff; Accepted Operations and Status Resources; Structured API Errors and Recovery; Idempotent Write Requests and Lost Responses.
