Blog

ETag And If-Match: Make Lost Updates A 412, Not Silent Data Loss

clients must prove what they edited
Two clients editing one resource while an ETag version gate rejects the stale copy

Two clients read version 7. Both edit. Client A saves version 8; client B saves its stale copy and erases A’s change.

HTTP already has a protocol for this collision.

Return A Validator

HTTP/1.1 200 OK
ETag: "account-42-v7"
Content-Type: application/json

The client sends that validator with its update:

PUT /accounts/42 HTTP/1.1
If-Match: "account-42-v7"

If current representation no longer matches, the server rejects the method with 412 Precondition Failed instead of overwriting. RFC 9110 describes If-Match specifically as useful for preventing lost updates.

Strong Means Representation Changed

If-Match uses strong comparison. Generate a validator that changes whenever the relevant representation changes. A database version is convenient, but do not leak a weak meaning under strong syntax.

Evaluate the precondition and perform the update atomically. Reading a version in Java and updating later without a guarded WHERE version = ? recreates the race.

Missing Preconditions Need A Policy

For an API where blind replacement is unsafe, require If-Match and reject clients that omit it. Document how clients refresh and merge after 412.

ETags also support cache validation through If-None-Match, but concurrency and caching are different jobs. Same header family, different intent.