Quick revision
The short answer
The server returns ETag: "7" on GET, the client sends the change with If-Match: "7", and the server compares that with the current tag using request.evaluatePreconditions, answering 412 Precondition Failed on a mismatch and 428 Precondition Required if the header is missing and we require it. The check must be atomic with the write, through UPDATE ... WHERE id = ? AND version = ? or JPA @Version, because a separate check and update lets two requests pass together, and a successful PUT returns the new ETag. The whole flow is documented in the OpenAPI contract.
What to remember
In this answer, we used ETags to prevent lost updates. GET returns ETag "7", the client sends If-Match "7" on its PUT, and request.evaluatePreconditions gives 412 Precondition Failed on a mismatch, while a missing header gets 428 Precondition Required when we require it. Because two requests can both pass a separate check, the real check is atomic with the write, using UPDATE ... WHERE id = ? AND version = ? or JPA @Version, and a successful update returns the new ETag. The flow, headers and statuses belong in the OpenAPI contract.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The server returns ETag: "7" on GET, the client sends the change with If-Match: "7", and the server compares that with the current tag using request.evaluatePreconditions, answering 412 Precondition Failed on a mismatch and 428 Precondition Required if the header is missing and we require it. The check must be atomic with the write, through UPDATE ... WHERE id = ? AND version = ? or JPA @Version, because a separate check and update lets two requests pass together, and a successful PUT returns the new ETag. The whole flow is documented in the OpenAPI contract.
Why this question is asked
A lost update is the quietest data loss there is. Two people open the same product, both change it, and the second save silently overwrites the first, with no error anywhere. HTTP has a standard answer to this problem: conditional requests with ETags, the same mechanism that powers caching, used the other way round. Interviewers ask for the flow and the server checks because many implementations do the comparison and still lose updates under concurrency. In this answer, we will reproduce a lost update, walk through the request flow, write the server checks, see why the check must be atomic with the write, and then document it for clients.
1Reproducing a Lost Update
Alice and Bob both open product 42, version 7, with price 39.90 and stock 100. Alice changes the price to 34.90 and saves. A minute later Bob, still looking at his old copy, changes the stock to 80 and saves the whole product with PUT.
Nothing failed, which is exactly the problem. Bob's PUT replaced the whole representation with one built from version 7, so it put back the old price. What we want is for Bob's save to be refused, so he can reload and reapply his change on top of Alice's.
2The Request Flow With ETags
The fix uses two headers. The GET response carries the current version as an ETag. The client keeps it and sends it back in If-Match on the update, meaning apply this change only if the resource is still at this version.
12 messages in order. Play them, or step through them one at a time.
Bob's update is refused instead of silently undoing Alice's.
Notice what each status tells the client. 412 Precondition Failed means someone else changed it; reload, merge and try again. 428 Precondition Required, defined in RFC 6585, means you must send If-Match on this endpoint; it stops old or careless clients from overwriting blindly.
3The Server Checks
In JAX-RS, the same evaluatePreconditions method we use for caching also handles If-Match. When the header is present and does not match the current tag, it returns a builder set to 412.
Notice that the early evaluatePreconditions check is only a fast path for a clear mismatch. The real protection is updateIfVersion, because between reading the version and writing, another request can slip in, as the next section shows. Also note that If-Match uses strong comparison, so if our ETags are weak (W/"7"), a match will never succeed; tags used for If-Match must be strong.
4Why the Check Must Be Atomic With the Write
Suppose Alice and Bob send their PUTs at the same instant, both with If-Match: "7". Both requests read version 7, both pass evaluatePreconditions, and both then update. If the update is a plain UPDATE ... WHERE id = 42, both succeed and we are back to a lost update. The check and the write must be one atomic operation, so the database decides who wins.
The database row lock makes the second UPDATE wait for the first, and when it re-evaluates its WHERE clause, version is no longer 7, so it updates nothing. With JPA, @Version on the entity does the same thing: Hibernate adds AND version = ? to the UPDATE and throws OptimisticLockException when no row matches, which we map to 412. The only rule is that the version we check against must be the one from If-Match, not one we re-read inside the transaction; we set it on the entity before merging, or compare it explicitly and let @Version catch the race.
5Returning the New Tag and Documenting the Flow
After a successful update, we return the new ETag in the response, so the client can make its next change without another GET. Then we document the contract in OpenAPI: the GET operation declares the ETag response header, the PUT and PATCH operations declare the If-Match request header as required, and the responses list 412 and 428 with a problem details schema and a description of what the client should do. Generated clients and API consumers then know the flow exists, and contract tests can check it.
6Follow-up questions
Should DELETE also require If-Match?
Often yes. Deleting a resource someone has just changed can be as harmful as overwriting it. The same check applies, and the SQL becomes DELETE ... WHERE id = ? AND version = ?.
What should a client do after a 412?
Fetch the current representation and its new ETag, show the user what changed or merge the change automatically if the fields do not overlap, and resend with the new tag. For a user interface, telling the person that someone else edited the product is usually better than an automatic merge.
Is this the same as optimistic locking?
Yes. The ETag carries the optimistic lock version out to the client and back, so the lock spans the whole edit session, which can last minutes, without holding any database lock during that time.
7Conclusion
In this answer, we used ETags to prevent lost updates. GET returns ETag "7", the client sends If-Match "7" on its PUT, and request.evaluatePreconditions gives 412 Precondition Failed on a mismatch, while a missing header gets 428 Precondition Required when we require it. Because two requests can both pass a separate check, the real check is atomic with the write, using UPDATE ... WHERE id = ? AND version = ? or JPA @Version, and a successful update returns the new ETag. The flow, headers and statuses belong in the OpenAPI contract.
Open with
I would use the ETag as an optimistic lock that travels through the client. The GET returns the product with ETag quote 7 quote, built from its version column.
Then, if they want more
When the client saves, it sends PUT with If-Match quote 7 quote, meaning apply this only if the product is still at version 7. On the server I first require the header, and return 428 Precondition Required if it is missing, so no client can overwrite blindly.
Then I build the current tag and call request.evaluatePreconditions; if it does not match, it gives me a 412 Precondition Failed builder, and I include the current tag so the client can reload. But that check alone is not enough, because two requests with If-Match 7 can both pass it at the same time.
So the actual update is atomic: UPDATE product SET ..., version = version + 1 WHERE id = ? AND version = 7. One request updates one row and wins, the other updates zero rows and also gets 412. With JPA, @Version gives the same behaviour and an OptimisticLockException that I map to 412.
A successful update returns the new ETag, quote 8 quote, so the client can continue. The tags used with If-Match must be strong, because If-Match uses strong comparison. Finally I document the ETag and If-Match headers and the 412 and 428 responses in the OpenAPI contract.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Use ETags to prevent lost updates when two users edit the same product through PUT...": https://scrivio-interview-library.netlify.app/java/jax-rs/3420681f3a/