Web / API Series

🏷️ Understanding ETags and Conditional Requests From a Top 1% Perspective — the Answer to the "Lost Update" Problem Idempotency Never Solves

Introduction

This article is part of the Top 1% Series Complete Article Guide, and article 3.2 in the Web/API Series.

Prerequisite Knowledge

Getting the Big Picture

Idempotency only guarantees safety for the case of “sending the same request repeatedly.” The “lost update” problem, by contrast, is an entirely different kind of problem. Say two users, A and B, both read the same resource at the same time, each make a different change, and each separately send a PUT. Looked at individually, both PUT requests are perfectly idempotent — but whichever PUT gets sent later silently, completely overwrites the change made by whichever PUT was sent earlier. A’s change gets lost, with no warning at all — this is the lost update problem, something idempotency alone can never prevent. ETags and conditional requests are HTTP’s standard answer to this problem.

sequenceDiagram
    participant A as User A
    participant Server as Server
    participant B as User B
    A->>Server: GET /resource (gets ETag: "v1")
    B->>Server: GET /resource (gets the same ETag: "v1")
    A->>Server: PUT /resource (If-Match: "v1")
    Server->>A: 200 OK (ETag updates to "v2")
    B->>Server: PUT /resource (If-Match: "v1")
    Server->>B: 412 Precondition Failed (rejected — ETag has already changed to "v2")

Deep Dive Into the Fundamentals

What Is an ETag? A Unique Fingerprint for a Resource’s State

An ETag (Entity Tag) is a unique identifier a server issues for a resource’s current state. Even the slightest change to the resource’s content changes the ETag’s value too. The server includes this ETag, as of that point in time, in the response header to a GET request. From there, whenever the client references this resource again, it can send back the ETag from the last time it fetched it, included in a request header.

If-None-Match: a Conditional Request for Caching Efficiency

When a client sends a GET request with an If-None-Match: "v1" header attached, if the current ETag matches the value the client presented, the server never sends the response body at all — it only returns a 304 Not Modified response, with no body. As long as the resource’s content hasn’t changed, the client can keep using the cache it already has, saving both bandwidth and processing cost.

If-Match: a Conditional Request for Detecting a Concurrent Update

The If-Match header is used for the exact opposite purpose of If-None-Match. When a client sends an update or delete request — PUT, PATCH, or DELETE — with an If-Match: "v1" header attached, the server only executes that request if the current ETag exactly matches the value the client presented. If, after the client fetched the resource, someone else changed that resource, and the current ETag has since changed, the server never executes the update — it returns a 412 Precondition Failed error instead. This lets the client itself detect that “I was about to update based on the state I read, but that premise has already fallen apart.”

This Is HTTP's Concrete Implementation of What's Called "Optimistic Locking"

You might remember a design pattern in the database world called “Optimistic Locking” — instead of acquiring a lock ahead of time, you check right before the update whether “the state I last saw still matches the current state,” and reject the update if it doesn’t. The combination of an ETag and If-Match is exactly this optimistic locking, implemented using HTTP’s own standard mechanism. Being able to provide this same safety at the HTTP layer, instead of implementing a separate version-number column on the database side, is a real-world advantage.

What a Pro Sees Here (Top 1% Understanding)

Idempotency and Optimistic Locking Are Separate, Mutually Complementary Safety Measures

Idempotency guarantees “it’s safe to resend a request of the same intent, any number of times,” while optimistic locking (If-Match) guarantees “the premise I intended still holds right now.” These two look similar, but what they each protect against is entirely different — a distinction worth keeping clearly separate. For example, say a PUT’s response never arrives due to a network failure, and the client resends the same PUT request — idempotency itself makes that resend safe. But if a different user changed that resource during the time between the resends, without If-Match, that change gets silently overwritten. This contrast shows that designing a genuinely safe update API in real-world practice needs more than idempotency alone — it needs optimistic locking combined in, as a separate safety axis.

Common Misconceptions and Pitfalls

Troubleshooting Perspective

  1. When multiple people edit the same resource, one person’s change goes missing: Check whether the update API requires a conditional request via If-Match, and whether it’s implemented on the server side.
  2. You attached If-Match, but unexpectedly get a 412: Check whether the ETag the client is holding matches the current latest state, re-fetch it with a fresh GET, and try the update again.
  3. Caching is supposed to be working, but the full response body comes back every single time: Check whether the client is correctly sending the If-None-Match header along with the ETag it already fetched.

Summary

Takeaways to Apply Today

  1. When designing an update API, don’t stop at idempotency alone — consider whether optimistic locking via If-Match is needed too.
  2. When you hit a 412 Precondition Failed, don’t assume it’s a server error — re-fetch the latest state first.

References

Comments

Feel free to leave thoughts, questions, or corrections below (sign in with a GitHub account to comment or react).