Prevent Lost Edits With HTTP ETags
Protect concurrent edits with a strong ETag and If-Match, handle stale writes by refreshing the current version, and keep explicit conflict checks.
Contents
- Why HTTP concurrency requires strong entity tags
- Trace a two-client concurrent update sequence
- Implement conflict resolution and state recovery
- FAQ
Why HTTP concurrency requires strong entity tags
When multiple automated agents or human operators edit shared resources over HTTP APIs, concurrent modifications frequently collide. If two clients read version 1 of a record and both submit updates via standard HTTP PUT requests, the second write silently overwrites the changes committed by the first.
While local filesystem tools enforce file integrity using exact string matching as shown in Deterministic File Edits with Text Editor Tool, distributed web services require protocol-level concurrency control. Under RFC 9110 HTTP Semantics, If-Match, servers issue an ETag response header representing the cryptographic digest of the resource state.
Trace a two-client concurrent update sequence
To demonstrate how strong comparison prevents lost updates, trace two independent clients attempting concurrent writes against /api/documents/812:
Client A Server Client B | | | |--- GET /api/documents/812 ------>| | || | | | |--- PUT (If-Match: "v1_hash") --->| | | [Payload: A's edits] | | ||Step 1: Client A and Client B read the document at version 1, receiving ETag: "v1_hash".
Step 2: Client A submits their modification including the header If-Match: "v1_hash". The server validates that the current document digest matches "v1_hash", commits the edit, and emits a new representation with ETag: "v2_hash".
Step 3: Client B sends their update still carrying If-Match: "v1_hash". The server compares "v1_hash" against the live state ("v2_hash"). Because the hashes differ, the server rejects the write with HTTP status 412 Precondition Failed.
Client A's changes are preserved, and Client B is prevented from overwriting the document.
Implement conflict resolution and state recovery
When an agent receives a 412 Precondition Failed error, it must enter an explicit reconciliation workflow rather than failing silently or looping blindly:
import httpxdef update_document_safe(client: httpx.Client, url: str, new_content: str): # Fetch current representation get_resp = client.get(url) get_resp.raise_for_status() current_etag = get_resp.headers["etag"] # Attempt conditional write put_resp = client.put( url, json={"content": new_content}, headers={"If-Match": current_etag} ) if put_resp.status_code == 412: # Conflict detected: refresh document state refreshed = client.get(url).json() raise RuntimeError( f"Write rejected: Document was updated concurrently to ETag {get_resp.headers.get('etag')}. " "Re-fetch latest content and reconcile differences." ) put_resp.raise_for_status() return put_resp.headers["etag"]This guarantees that every committed edit is explicitly anchored to the state the editor inspected.
FAQ
- What changed?
When multiple automated agents or human operators edit shared resources over HTTP APIs, concurrent modifications frequently collide. If two clients read version 1 of a record and both submit updates via standard HTTP PUT requests, the secon
- What should builders take away?
To demonstrate how strong comparison prevents lost updates, trace two independent clients attempting concurrent writes against /api/documents/812:
- Where can I read the primary source?
The full announcement is available at https://labs.zeroshot.studio/ai-workflows/prevent-lost-edits-with-http-etags/opengraph-image.