Home » Optimistic Concurrency in APIs: Preventing Lost Updates with If-Match

Optimistic Concurrency in APIs: Preventing Lost Updates with If-Match

Two editors use v7; the first update creates v8 while a stale If-Match request ends at a 412 rejection.

An API can prevent a stale edit from silently overwriting a newer one by requiring the client to return the resource version it read and applying the write only if that version still matches. HTTP offers ETag and If-Match for this precondition. The comparison and the write must share an atomic boundary; checking a version and then updating unconditionally leaves the race intact.

This is optimistic concurrency: clients can read and prepare changes without holding a lock throughout user interaction. A conflicting write is rejected so the client can reload and reconcile it.

Follow two edits to the same resource

Imagine a note at version 7. Alice and Ben both read it. Alice changes the title and saves first, creating version 8. Ben later submits a body based on version 7. If the server accepts his whole representation unconditionally, Alice's title can disappear.

A conceptual exchange is:

GET /notes/42

HTTP/1.1 200 OK
ETag: "note-42-v7"

Alice sends a write with If-Match: "note-42-v7". After it succeeds, the current validator becomes "note-42-v8". Ben's old validator no longer matches, so this example API returns 412 Precondition Failed and leaves version 8 intact.

The messages are illustrative, not a captured HTTP session. RFC 9110, section 13.1.1 defines If-Match and requires strong entity-tag comparison. A weak validator such as W/"note-42-v7" does not provide the same precondition. If-Match: * tests existence, not whether the client's specific revision is current.

Make the storage operation conditional too

A handler that reads version 7, compares it, and later writes without a condition can still lose an intervening update. Bind the mutation to the expected version in the storage operation:

UPDATE note
SET body = ?, version = version + 1
WHERE id = ? AND version = ?;

This is the core of a proposed implementation, not a complete HTTP handler. The application must map a valid representation validator to the expected version, enforce authorization, interpret the result correctly, and return the new validator under its API contract.

The MDN If-Match reference explains its use against lost updates. The SQL condition above is one way to connect that HTTP intent to a write boundary; the exact transaction and affected-row semantics depend on the database and driver.

Do not assume that every zero-row update proves a stale version. A missing resource, an authorization condition, or another application predicate can also yield zero rows. Define the response policy without revealing objects the caller cannot access. Authentication and authorization still apply before accepting a precondition as permission to write.

What the local exercise established

We executed an in-memory SQLite exercise with one note at version 7. Two sequential attempts used that same expected version, modeling clients that had taken the same snapshot.

Operation Affected rows Stored result
First write using version 7 1 Version 8, first edit
Second write using version 7 0 Version 8 unchanged
Reconciled write using version 8 1 Version 9, reconciled edit

Four assertions passed with Python 3.14.2. The exercise checked row counts and final state. It did not launch an HTTP server, run simultaneous database transactions, or validate a production database's isolation behavior. It demonstrates the conditional mutation for these inputs, not a complete concurrency protocol.

Give the client a reconciliation path

On a stale-edit response, preserve the user's unsaved change, fetch the current representation, and show or compute the difference. Reapplying the old request with the newest validator without reconciliation can overwrite the very update the precondition protected.

For independent fields, the application might offer a merge. For interdependent fields, a human decision may be necessary. An inventory quantity, an account balance, and a document title do not share the same conflict rules. Domain operations can be safer than replacing an entire object, but they still need concurrency semantics appropriate to their invariants.

Use stable error fields to explain what the client can do next, following the principles in designing API errors. A failed precondition describes a version mismatch under this contract; it does not mean the client should retry indefinitely.

Keep retries and representation versions distinct

A request can commit and then lose its response. A retry carrying the old validator can now encounter a changed version even though the first operation succeeded. RFC 9110 permits particular success handling when the server can determine that a state-changing request already succeeded; do not invent that determination from a matching-looking body alone.

Idempotency and operation-status mechanisms address uncertain outcomes. A version validator addresses whether the state being edited is still the state the client read. Neither automatically replaces the other.

Before adoption, test stale, current, missing, malformed, and unauthorized cases; verify that rejected writes have no effect. Cover every representation change that should invalidate the validator. The useful guarantee is that a successful update respected the expected version at the write boundary, with a clear path for conflicts—not that concurrent users never encounter them.

You may also like