In this article
A profile editor can lose data when two people read the same version and save in turn. In an interview, make that failure concrete, state the API contract, and verify a stale write is rejected with an ETag precondition instead of replacing newer data. This is an illustrative exercise; use an AI assistant in an assessment only when its rules explicitly allow it.
Clarify the interview rules and contract
Before opening an assistant, ask whether tools are permitted and what the interviewer expects you to demonstrate. If assistance is allowed, keep your own reasoning visible: the useful signal is how you identify an invariant, compare options, and test the result. The example below is a practice scenario, not a claim about any employer's interview policy.
Suppose two administrators open the same profile. One changes the display name while the other updates the role. A plain save endpoint accepts both requests, so whichever arrives last can erase the earlier edit. Ask whether the product wants last-write-wins, field merging, or conflict detection. Those are product choices, and an implementation cannot infer the intended policy from the word update alone.
Reproduce the silent lost update
Give both clients a copy marked version v1. Client A changes the display name and saves. Client B, still holding v1, changes a different field and sends its whole stale profile. A naive server replaces the current document with B's copy; A's accepted change disappears. This is the lost-update failure to reproduce before proposing a repair.
The failing behavior is not that either payload is malformed. It is that the server accepts a write without checking which version the writer edited. A test that submits only one save cannot expose the problem. Model two reads before either write, then two writes based on that shared starting state.
type Profile = { name: string; etag: string };
const initial = { name: "Ada", etag: "v1" };
let stored = { ...initial };
function blindPut(name: string) {
stored = { name, etag: "v" + (Number(stored.etag.slice(1)) + 1) };
}
const clientA = { ...stored };
const clientB = { ...stored };
blindPut("Ada Lovelace");
blindPut("Ada Byron");
console.log(stored.name); // Ada Byron: A's save vanishedInvariant: a write based on an obsolete whole-document version must not silently replace the newer version.
Expected outcome: one write succeeds; the stale write receives a conflict response and the committed value remains visible.
State a version precondition
HTTP already has a conditional-write mechanism for this contract. A server can return an ETag with the representation. A client sends that tag in If-Match when saving; the method proceeds only if the stored representation still matches. A failed condition produces 412 Precondition Failed, allowing the client to reload or present a diff instead of pretending the save succeeded. HTTP conditional requests
Treat the ETag as an opaque validator. The client should return the exact value it received rather than inventing a version number or parsing the tag. Strong comparison matters for If-Match. The server must check the condition atomically with the write; checking in application code and then saving later would leave another race window. ETag header
Implement the conditional update
Here is a small in-memory model of the rule. It is deliberately not an HTTP server: it makes the comparison and state transition easy to inspect. A real endpoint must parse headers, persist the update transactionally, and return the new ETag after success.
Run two writes against the same initial tag. The first receives 204 and changes the validator. The second presents the old tag and receives 412. The saved profile still contains the first accepted value. If an endpoint requires conditional writes, it may also reject a missing precondition; MDN describes 428 for that policy. 428 Precondition Required
type Profile = { name: string; etag: string };
type Result = { status: 204 | 412 | 428; profile: Profile };
function put(current: Profile, name: string, ifMatch?: string): Result {
if (!ifMatch) return { status: 428, profile: current };
if (ifMatch !== current.etag) return { status: 412, profile: current };
const next: Profile = { name, etag: "v" + (Number(current.etag.slice(1)) + 1) };
return { status: 204, profile: next };
}
const original = { name: "Ada", etag: "v1" };
const first = put(original, "Ada Lovelace", "v1");
const stale = put(first.profile, "Ada Byron", "v1");
console.log(first.status, stale.status, stale.profile.name);
// 204 412 Ada LovelaceVerify behavior independently
Do not use the same helper to generate both the implementation and expected result. Write a small scenario from the outside: fetch version v1 twice, submit A with v1, then submit B with v1. Assert the status sequence is success followed by 412, and assert the final server state contains A's value. Also test a missing condition if the API promises to require one.
Then challenge the boundary. A retry after 412 must fetch the latest document and reconcile deliberately; blindly resending the same stale payload with the new ETag simply converts a safe conflict into an overwrite. A field-level merge is possible, but it needs conflict rules and validation of the merged state. HTTP conditional requests
Two clients read before either write; the stale write cannot silently win.
A fresh ETag permits the next valid update and returns a replacement validator.
A conflict response preserves current state and gives the client a recovery path.
Explain tradeoffs and recovery
Optimistic concurrency does not prevent edits from conflicting; it makes the conflict observable. That is a good fit when collisions are uncommon and a user can review the newer version. It can create extra round trips and requires clients to handle conflicts. A server that accepts last-write-wins may be simpler, but should do so as an explicit product decision rather than an accidental consequence of request timing.
In an interview, narrate the invariant before coding, ask whether writes replace the whole resource or only selected fields, and explain what the client should do after 412. Mention that conditional validation must be enforced at the storage boundary. These details show whether a generated patch satisfies the contract instead of merely compiling.
Common questions about If-Match
Does If-Match merge concurrent edits? No. It lets a server reject an update based on a stale validator. The client or product must decide how to reconcile the two versions. HTTP conditional requests
Is an ETag a sequential database ID? It is an opaque entity tag identifying a representation version. The client should preserve and return it; the service chooses how to generate and validate it. ETag header
Should every endpoint return 428 when a header is absent? That is an API policy. 428 communicates that a precondition is required, while a mismatched supplied precondition is commonly reported as 412. Confirm the endpoint contract before choosing behavior. 428 Precondition Required
Sources & editorial notes
This article was authored with AI using primary-source research and an automated technical review. It has not been reviewed by a human. Code examples are illustrative; run and verify them in your own environment.
- HTTP conditional requests
Explains conditional writes, optimistic locking, lost updates, and the 412 response for a stale If-Match validator.
- ETag header
Defines ETag validators and illustrates returning a tag with a write that checks If-Match.
- 428 Precondition Required
Documents 428 when a server requires a conditional request and the distinction from a failed precondition.
Put the reading into practice
Work through an existing repository, review AI suggestions, and test the decisions behind your changes.
Explore practice problems