Intermediate LevelREST APIs & Web Services
How do If-Match headers and ETags prevent Lost Update problems during concurrent updates?
❓ Interview Prompt: Explain Lost Update Problem, race conditions in concurrent `PUT`/`PATCH` edits, `If-Match` header, `ETag`, and `412 Precondition Failed` status.
The Lost Update Problem:
Two admins open user profile 42 simultaneously. Admin A changes name to "Alice". Admin B changes email to "bob@example.com". Admin B saves last, overwriting Admin A's changes completely!
Optimistic Concurrency Control Solution:
1
Server includes 'ETag: "v1_hash"' on 'GET /users/42'.
2
Admin A submits 'PUT /users/42' with header 'If-Match: "v1_hash"'. Server updates resource and updates ETag to '"v2_hash"'.
3
Admin B submits 'PUT /users/42' with outdated 'If-Match: "v1_hash"'.
4
Server detects ETag mismatch and rejects Admin B's edit with '412 Precondition Failed'!
Code Implementation Example
http
PUT /api/v1/users/42 HTTP/1.1
Host: api.example.com
If-Match: "v1_hash"
Content-Type: application/json
{ "name": "Alice" }
// If ETag matches: 200 OK (New ETag: "v2_hash")
// If ETag modified: 412 Precondition Failed💡HTTP conditional request using If-Match to prevent concurrent update overwrite bugs.
🎯 Interview Key Takeaway
Prevent lost updates during concurrent edits by requiring If-Match headers containing ETags on PUT and PATCH requests.
#If-Match#ETag#Optimistic Concurrency#Intermediate
Back to all REST APIs & Web Services Questions→