Two edits at once and one is lost? ETag and If-Match stop it

The ETag header gives each version of a resource a tag. With If-None-Match the server answers 304 with no body, and with If-Match it refuses a save with 412 if someone edited first.

Published 3 min read#Web development#Developer tools

Two people opened the same page in the dashboard, each changed something, and they pressed save a minute apart. The second never saw the first edit, and their save wiped it out without a single error message. By the end of this article you will be able to make the server refuse the second save instead of wiping the first, and save a full response every time the browser asks for something that has not changed.

What is the ETag header?

The ETag header is a tag the server sends with a response, naming one specific version of the resource. If the resource changes, the tag changes, and if it does not, the tag stays the same.

According to the ETag page on MDN, the header helps with two things: caching gets cheaper because the server does not need to send the full response again if the content has not changed, and simultaneous edits do not overwrite each other. A tag that starts with W/ is a weak tag: easier to generate, but weaker for comparisons.

ETag: "v7"
ETag: W/"v7"

How does If-None-Match save a full response?

It saves a full response when the client sends back the tag it has in an If-None-Match header, and the server compares it with the current tag. If they match, the server answers 304 with no body.

The same MDN page explains the flow: the client sends the tag when its copy is stale, and if the two values match, the server sends back 304 Not Modified without a body, which means the cached copy is still good to use.

GET /api/posts/42
If-None-Match: "v7"

304 Not Modified
ETag: "v7"

That way a large page or API response is not sent again every time. Keep in mind this is for resources that can be cached in the first place: pages with private data have other rules (see Cache-Control and no-store).

How does If-Match stop two edits from overwriting each other?

The If-Match header makes the save conditional: the client sends the tag it opened the resource with, and if the tag on the server has changed since then, the server refuses the save with 412.

MDN calls this problem a mid-air collision: if the tags do not match, the document was edited in between, and the server returns 412 Precondition Failed. In the example from the start of this article, the second person gets a 412 instead of wiping out the first edit.

PUT /api/posts/42
If-Match: "v7"

412 Precondition Failed

What does the server code look like?

The server code is simple: you compute the tag from something that changes with every edit (like a version column in the table), and compare it with the header before you respond or save.

const etag = `"v${post.version}"`;

// GET: answer 304 when the client's copy is current
if (req.headers["if-none-match"] === etag) {
  res.writeHead(304, { ETag: etag });
  return res.end();
}

// PUT: refuse the save when the post changed since it was opened
if (req.headers["if-match"] !== etag) {
  res.writeHead(412);
  return res.end();
}
  1. Pick the source of the tag: a version column that goes up with every edit, or a hash of the content itself.

  2. Send ETag with every GET response for the resource.

  3. In the frontend, store the tag when you open the resource for editing, and send it in If-Match with the save.

  4. When a 412 comes back, show the user the new version and ask them to review their edit, instead of saving again on your own.

The code above compares a single value directly, which is enough for a frontend you wrote yourself. And in the database itself, make the save conditional on the version number too, so two requests at the same moment do not both get through (a close idea in the ON CONFLICT in PostgreSQL article).

Where do you start today?

Pick one resource in your dashboard that more than one person edits, and add ETag to its response and If-Match to its save, because then the first two simultaneous edits will end in a 412 instead of a lost edit. And if you have a public API, put request limits on it too (see capping API consumption). Which resource of yours gets edited from more than one place?

FAQ

What is the difference between a strong and a weak ETag?

A weak one starts with W/ and says two versions mean the same thing, not that they match byte for byte. It is easier to generate but weaker for comparisons, and a strong one still works with byte range requests.

Can I use the row's version number from my database as the ETag?

Yes. Any value that changes with every edit and stays the same while the resource does not change works, like a version column or a hash of the content.

Does a 304 response have a body?

No. The server sends 304 Not Modified without a body, and the client uses the copy it already has in its cache.

Share

More articles