Two editors can read the same document and overwrite each other's work even when every request is authenticated and valid. Require an update to identify the version it was based on, then compare that version atomically with the mutation. A separate read followed by an unconditional write leaves the lost-update race intact.
This article follows a synthetic document API using strong entity tags and HTTP preconditions. It explains the protocol and authoritative storage requirement rather than supplying a database integration. The worked exchange is illustrative; no live HTTP service or transaction engine was exercised for it.
Give the client a version it can return
On reading a document, the client receives its representation and a strong entity tag, such as the synthetic tag "doc-7". The tag is an opaque validator. Clients should return it unchanged rather than parsing the number, generating a replacement, or comparing tags lexically to decide which document is newer.
RFC 9110 section 13.1.1 defines If-Match using strong comparison. A weak tag does not satisfy that comparison. The server must make its validator reflect the representation and update contract correctly; an unchanged tag across meaningful edits defeats the intended protection.
This use differs from conditional cache retrieval. The ETag caching guide explains validators for response reuse. Here, the client returns a validator as a condition on a write, and the server must enforce that condition before changing state.
Authorization remains independent. Possessing a current tag is not permission to modify the document. Determine the caller's access to the target and fields through the normal authoritative policy, then evaluate applicable preconditions in the HTTP processing order. Do not reveal document versions to an unauthorized caller merely to provide a more descriptive conflict.
Follow the two-editor failure precisely
In the synthetic schedule, the stored document begins at version 7. Editor A and editor B both read it. A changes the title; B changes the description using the same starting representation. A sends its replacement with If-Match containing "doc-7".
The server accepts A's condition, writes the new representation, and advances its validator. B later sends a replacement still conditioned on "doc-7". That condition is now false. The server rejects the stale update with the appropriate precondition failure and leaves A's committed representation intact.
Without the condition, B's replacement can erase A's title change even though B never intended to edit the title. Partial updates can still conflict when they rely on a shared invariant or when field-level effects overlap. Decide whether the version protects the whole document or a smaller resource; HTTP syntax cannot decide that application boundary.
If the API requires a precondition and the client omits it, RFC 6585 defines status 428, Precondition Required, for that policy. A stale supplied condition instead concerns precondition failure, commonly status 412. Keep missing-policy and stale-version outcomes distinguishable so the client can respond correctly.
Couple comparison and mutation at the storage authority
The tempting implementation first reads version 7, verifies the supplied tag, and then performs an unconditional update. Two requests can both pass that first read before either writes. Moving the check from the browser to the server is necessary but still insufficient if the server separates the check from its authoritative mutation.
Use a supported conditional update, lock-protected transaction, or equivalent mechanism that compares the expected version and writes as one decision. Only one competing update should consume version 7. Define the failure result when no row or resource matches, while preserving the authorization boundary and avoiding information disclosure.
Advance the version for every participating writer, including administrative tools and background jobs. A protected HTTP endpoint cannot prevent another write path from changing state without updating the validator. Test the full writer set rather than assuming that a well-behaved client is the only actor.
Side effects need the same boundary. A stale request must not send a notification, charge an account, or enqueue an authoritative event before it is rejected. Coordinate committed effects through an appropriate transactional mechanism. A precondition failure that occurs after an external effect has already happened is not a no-effect rejection.
Preserve unsaved work when the condition fails
On a stale response, retain the user's local draft. Fetch the current authorized representation, show the differences that matter, and let the user rebase the intended edit or explicitly choose a reviewed replacement. Do not automatically repeat the old body with the newly fetched validator; that merely authorizes the overwrite the precondition was intended to expose.
Define behavior after a connection failure too. The server may have committed an update whose response the client never received. A repeat using the old tag can now fail even when the first request succeeded. Use the API's operation identity or reconciliation mechanism to determine the outcome instead of treating every precondition failure as proof of another user's edit.
The RFC permits specific duplicate-success handling when the server can verify that a state-changing request already succeeded. Do not generalize that allowance into accepting any stale write. The evidence must identify the duplicate outcome according to the protocol and the application's own effect semantics.
Before deployment, run a two-client integration test with a barrier after both reads. Assert exactly one accepted mutation, a stale result for the other, preservation of the accepted document, and absence of denied side effects. Then test omission, weak tags, unauthorized access, background writes, and lost responses. Until those integration checks run, the worked schedule establishes the design requirement rather than proving a particular service meets it.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit