Skip to main content

Preventing Lost Updates in REST APIs with ETags, If-Match and 412 Precondition Failed

Use ETags and If-Match to stop concurrent PUT and PATCH requests overwriting each other, make the check atomic, and roll out 412 and 428 in stages.

Detailed macro shot of a honeybee (Apis mellifera) resting on asphalt, showcasing its texture and natural colors.

Underlined words are defined in the KBY Lexicon. Tap or click one to see what it means.

A lost update happens when two clients read the same resource, both change their copy, and the second PUT or PATCH silently replaces the first. already defines the fix. The server returns an ETag with each representation, the client sends that value back in If-Match when it writes, and the server refuses the write with 412 Precondition Failed if the resource has changed since.

The constraint that decides whether it works is atomicity: the ETag comparison and the write must be one operation in the data store, or two requests can both pass the check. Roll it out in two stages. Honour If-Match whenever a client sends it, then, once clients send it, answer unconditional writes with 428 Precondition Required. The behaviour described follows RFC 9110 (HTTP Semantics, June 2022) and RFC 6585 (April 2012), the current specifications on 25 November 2026.

#The problem with last writer wins

RFC 6585 describes the failure precisely: a client GETs a resource, modifies it and PUTs it back, while a third party has changed the state on the server in the meantime. Without a check, the server accepts the stale copy and the other change disappears. Nobody receives an error, so the loss tends to surface later, as a change that seems to have undone itself.

Microsoft’s Azure Storage documentation names the three strategies an can take. Optimistic concurrency checks at write time whether the data changed since it was read. Pessimistic concurrency takes a lock that blocks other writers until it is released. Last writer wins lets every write proceed without checking. Azure Storage applies last writer wins to blobs unless the caller chooses otherwise, and any API that never checks behaves the same way.

Locks suit a single primary that holds data for long periods. They fit poorly with stateless HTTP clients that may read, wait for a person to edit a form, and never come back. Optimistic concurrency costs little until two writers collide, and the Azure documentation calls it the strategy most often used in web applications.

It protects one resource at a time; a change that spans several services still needs a pattern such as compensating sagas for multi-service workflows.

#Under the hood of a conditional write

An ETag is an opaque quoted string, such as "xyzzy", optionally prefixed with W/ to mark it weak. Tags are strong by default. RFC 9110 requires an origin server to mark a tag weak if the way it is generated does not meet the definition of a strong validator.

Clients treat the value as opaque, so the server alone decides how to build it: an internal revision number, a collision-resistant hash of the representation, or a timestamp with sub-second resolution.

The server compares If-Match using the strong comparison function. Two tags match only if neither is weak and the characters are identical. A weak tag therefore never satisfies If-Match, even against itself: W/"1" against W/"1" is “no match” under strong comparison. If-Match: * is true whenever the resource has any current representation, so it guards against writing to something deleted but not against overwriting someone else’s change.

A typical exchange looks like this. The second client’s tag is out of date, so its write is refused and the first client’s change survives.

1GET /v1/orders/42 HTTP/1.1
2
3HTTP/1.1 200 OK
4ETag: "a3f9"
5
6PUT /v1/orders/42 HTTP/1.1
7If-Match: "a3f9"
8
9HTTP/1.1 200 OK
10ETag: "7c1e"
11
12PUT /v1/orders/42 HTTP/1.1
13If-Match: "a3f9"
14
15HTTP/1.1 412 Precondition Failed

Ordering matters. RFC 9110 says preconditions are evaluated after the normal request checks and just before the server processes the content or performs the method. If If-Match is present, it is evaluated first, and If-Unmodified-Since is ignored. A false If-Match means the server must not perform the method. It may answer 412, or a 2xx if it can tell the same change has already been applied.

If-None-Match: * covers the create case. Sent with a PUT, it makes the write fail if any representation already exists, so two clients creating the same resource cannot overwrite each other. Prefer ETags to Last-Modified: HTTP dates have one-second resolution, and Microsoft’s API guidelines note they cannot tell apart updates made less than a second apart.

Rendering diagram...

The atomic step in the middle is where real implementations fail. Reading the current tag, comparing it in application code and then issuing an ordinary update leaves a gap in which a second request can pass the same comparison. The store must do the compare and the write together.

Azure Cosmos DB, for example, gives every item a system-maintained _etag and rejects a replace whose if-match value is no longer current with a 412; Azure Storage does the same for blobs.

A complex network of cables in a data center with a monitor in the foreground.
Photo by panumas nikhomkhai on Pexels

#Rolling it out without breaking existing clients

Turning on enforcement in one step breaks every client that never sent If-Match. Splitting it into stages means each one can be switched back on its own.

  1. Return ETags everywhere. Microsoft’s guidelines recommend an ETag on every operation that returns or updates the resource, and an etag field on each item in a collection. Nothing changes for current clients.
  2. Make the store do the comparison. Use the database’s native conditional write, or an update that only succeeds where the stored version or hash still equals the tag the client sent. Test it with two concurrent writers before going further.
  3. Honour If-Match when present. A mismatched tag gets 412; a request without the header behaves as before. Microsoft’s guidelines treat honouring preconditions as mandatory, because ignoring a header the client relies on is unsafe.
  4. Cover DELETE and PATCH. In Microsoft’s guidelines, a matching delete returns 204 and a mismatched one 412. RFC 5789 says clients applying patch formats that need a known starting point should send a conditional request, such as a strong ETag in If-Match.
  5. Measure adoption. Log, per client identity, the share of writes that carry If-Match, and how many use *.
  6. Require it. Answer unconditional writes with 428. RFC 6585 says the response should explain how to resubmit successfully, and it must not be stored by a cache.

Following along? Tap a step’s number to tick it off.

Put stages 3 and 6 behind separate switches, ideally per client or per endpoint. The precondition for stage 6 is that adoption is close to complete for that client; the rollback is turning the switch off, which returns the API to stage 3.

Client code needs one rule: a 412 is a conflict, not a transient fault. Fetch the resource again, reapply the change to the new state or show the person both versions, then write with the new tag. Never retry the same request with the old tag, and never drop the header to force the write through. Backoff and jitter, as in fixing 429 retry storms with backoff, belong to 429s and server errors.

Watch the response to a successful PUT. RFC 9110 forbids sending an ETag there unless the stored data is exactly what the client sent. Microsoft’s guidelines say the response must include the new ETag. Both hold if the server stores the body unchanged; if it adds fields such as timestamps, the strict reading is to omit the tag and let the client GET the new state.

#Checking it worked with curl

Run these against a test resource in a non-production environment, with a token that may write to it. Record the resource’s state first so it can be restored.

  1. Read the current tag: curl -i https://api.example.test/v1/orders/42. The headers should include a strong tag, such as ETag: "a3f9", with no W/ prefix.
  2. Send a stale write: curl -s -o /dev/null -w '%{http_code}\n' -X PUT -H 'If-Match: "stale"' -H 'Content-Type: application/json' --data-binary @order.json https://api.example.test/v1/orders/42. Expect 412, and a second GET should show the same tag and body as before.
  3. Repeat with the current tag in If-Match. Expect 200 or 204, and a following GET should return a different tag.
  4. Send two writes in parallel with the same tag. Exactly one should succeed and the other should get 412. Two successes mean the comparison is not atomic.
  5. Send If-None-Match: * on a PUT to the existing resource. Expect 412.
  6. Once stage 6 is on, send a write with no conditional header. Expect 428 with a body that names If-Match.

Run the same checks through the public gateway, not only against the service directly. RFC 9110 lets caches and intermediaries ignore If-Match, but a proxy that is not acting as a cache must forward it. A proxy that drops it makes every client look unconditional.

#What goes wrong with ETags in production

  • Every conditional write fails with 412. The server is issuing weak tags, which can never pass the strong comparison. Issue strong tags for resources that accept writes.
  • Spurious 412s with no real conflict. The tag changes on reads, for example because it hashes a field computed per response. Microsoft’s guidelines recommend hashing the entire resource; whatever goes into the hash must be stable between reads.
  • A client’s retry conflicts with its own write. The first write succeeded, the response was lost and the retry carries the old tag. Microsoft’s guidelines prefer content hashes to version numbers for this reason: an identical retry then produces the same tag. RFC 9110 also allows a 2xx when the change has already been applied, but recommends strict 412s for resources such as counters, where similar writes are not interchangeable.
  • Two writers both succeed. The check runs in application code, separate from the update. Move it into the store’s conditional write.
  • Clients see 428 everywhere after enforcement. Adoption was measured on a different path from the one clients use, or a gateway strips the header. Turn stage 6 off and test through the client path.
A bee delicately pollinates chamomile flowers, showcasing nature's beauty and the pollination process.
Photo by Wyxina Tresse on Pexels

#Access control still decides who may write

An ETag is a version marker, not a credential, and it is not secret: any reader can see it. Authorisation must run as it does for any write. RFC 9110 supports the right ordering: a server must ignore preconditions if the request would otherwise have failed or redirected before significant processing. A caller who may not write gets 401 or 403, not 412, and cannot use 412s to probe whether a resource changed.

Keep the 412 response lean. Microsoft’s guidelines suggest returning the representation the client sent, not the server’s current state. A client that needs the current version should GET it through the normal read authorisation, so a write-only integration never receives data it could not read.

The residual risk is If-Match: *. It can satisfy a 428 check that only looks for a conditional header, yet gives last-writer-wins behaviour for an existing resource. Report its use per client, and agree with integration owners which, if any, may send it.

#Backing out enforcement safely

A 412 or 428 changes no data: when the condition is false, the server does not perform the method. Backing out therefore never needs data repair, only a decision about how far to go back.

  • If 428s break a client, turn stage 6 off for that client. Writes without the header are accepted again, and clients that send If-Match keep their protection.
  • Do not back out by ignoring If-Match. A client that sends it expects a 412 on conflict; silently accepting a stale write reintroduces lost updates for exactly the clients that did the work.
  • If the tag scheme changes, for example from a version number to a hash, every tag clients hold becomes stale at once and their next writes get 412. Make the switch at a quiet time and expect one refetch per active client.

#Readiness checks before you require If-Match

  • Two concurrent writes with the same tag produce one success and one 412, through the public gateway.
  • Each client identity sends If-Match on nearly all of its writes, and its 412 rate is stable.
  • Client teams handle 412 by refetching, not by retrying with backoff.
  • The 428 body tells the caller to send If-Match, and the switch for stage 6 can be turned off per client.
  • Use of If-Match: * is reported and agreed.

When all five hold for one endpoint, require If-Match there first, watch 428 and 412 rates for a release cycle, then extend it endpoint by endpoint.

About this page

Credited to

Dates

Published

Sources

This page lists no external sources.

Corrections

No corrections are listed for this page. Some review sweeps corrected pages without naming each one; those are in the corrections log.

Reader Interaction

Comments

Add a thoughtful note on Preventing Lost Updates in REST APIs with ETags, If-Match and 412 Precondition Failed. Comments are checked for spam and held for moderation before appearing.

Loading comments...

More from KBY that goes with this article.

Discover more

Lexicon Definitions

Was this useful?

Reports go to the site owner and are used to correct pages. Corrections are listed at /corrections.

Engineering insights, direct to you.

Receive the latest Systems Engineering tutorials, production guides, Engineering Labs and operational best practices.