Quick revision
The short answer
The lesson that carries over from ABA is that comparing only a value can miss intermediate changes, while comparing a version that always increases cannot. So updates in our APIs should be conditional on a version: a version column checked in the UPDATE, exposed to clients as an ETag and required with If-Match, so concurrent writers get a conflict instead of silently overwriting each other. Optimistic concurrency works well when conflicts are rare and retries are cheap; under frequent conflicts we serialize updates per key or use a single-writer design. My long-term stance is to make conditional updates a standard, documented and tested part of every service API that allows updates.
What to remember
In this answer, we carried the ABA lesson from compare-and-set into service APIs: a check on values can be fooled by intermediate changes, while a check on a version that always increases cannot. We built conditional updates with a version column in SQL and JPA, exposed the version as an ETag with If-Match, 412 and 428 in HTTP, and noted that optimistic concurrency suits rare conflicts while hot keys need serialization or single writers. The stance is to make conditional updates a standard, documented and tested part of every API that allows updates.
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 lesson that carries over from ABA is that comparing only a value can miss intermediate changes, while comparing a version that always increases cannot. So updates in our APIs should be conditional on a version: a version column checked in the UPDATE, exposed to clients as an ETag and required with If-Match, so concurrent writers get a conflict instead of silently overwriting each other. Optimistic concurrency works well when conflicts are rare and retries are cheap; under frequent conflicts we serialize updates per key or use a single-writer design. My long-term stance is to make conditional updates a standard, documented and tested part of every service API that allows updates.
Why this question is asked
Lost updates are among the most common data bugs in web services, and they rarely look like bugs: a customer's change simply vanishes because another request saved over it a second later. The tools to prevent them are old and well understood, yet many APIs still offer a plain PUT with no precondition. Interviewers ask this at principal level because it links a low-level concept, ABA in compare-and-set, to a design policy across many services. In this answer, we will recap ABA, map it onto lost updates in databases and HTTP, build conditional updates with a version column and ETags, discuss when optimistic concurrency fits and when it does not, and set out the organisational stance.
1A Quick Recap of ABA
In lock-free code, compare-and-set writes a new value only if the current value equals the one we read earlier. ABA is the case where the value went from A to B and back to A while we were not looking, so the CAS succeeds even though the state changed underneath. The cure is to compare something that never repeats, such as a version stamp that increases on every change. AtomicStampedReference does exactly that.
The same structure appears whenever a client reads data, thinks for a while, and then writes back based on what it read. The 'thinking' can be a user editing a form for five minutes, which is a much bigger window than a paused thread.
2Lost Updates and Value-Based Checks
Without any check, two clients that both read a record and both save their edits produce a lost update: the second save overwrites the first, and the first client never learns. Some APIs try to prevent this by comparing values, for example 'update the balance to 80 only if it is still 100', or 'update the address only if it still equals what I read'. That is ABA in a new place. If the balance went from 100 to 150 and back to 100 because of a deposit and a refund, the check passes and the client's decision, based on a history it never saw, is applied. Comparing whole records is also fragile and expensive.
A version number fixes both problems. Every update increments it, so 'version is still 7' means precisely 'nobody changed this record since I read it', however the values moved.
| Step | Balance | Version |
|---|---|---|
| Client X reads | 100 | 7 |
| Deposit of 50 | 150 | 8 |
| Refund of 50 | 100 | 9 |
| X: update if balance is 100 | check passes (ABA) | - |
| X: update if version is 7 | - | check fails: conflict |
3Conditional Updates in the Database
In SQL, the version check is part of the UPDATE statement, so the database performs the check and the write atomically. The application looks at the number of rows updated to tell success from conflict.
With JPA and Hibernate, a field annotated with @Version makes the provider add exactly this condition to every update, and it throws an OptimisticLockException (in Spring, an ObjectOptimisticLockingFailureException) when no row matches. Spring Data's @Version works the same way for JDBC and MongoDB repositories.
4Carrying the Version Over HTTP: ETags and If-Match
HTTP already has the vocabulary. The server returns the version as an ETag header with every GET. A client that wants to update sends it back in an If-Match header. The server performs the conditional update; if the version no longer matches, it answers 412 Precondition Failed, and the client can fetch the current state, show the conflict or merge, and try again. To make the protection mandatory, the server can refuse an update without If-Match with 428 Precondition Required.
8 messages in order. Play them, or step through them one at a time.
Without If-Match, X's PUT would silently overwrite Y's change.
Notice that Spring's ShallowEtagHeaderFilter is not a substitute. It computes an ETag from the response body to help caching of GET responses; it does not check If-Match on writes. The version must come from the data itself, so that the check happens where the write happens.
5When Optimistic Concurrency Fits, and When It Does Not
Optimistic concurrency assumes conflicts are rare. Then almost every update succeeds on the first try, nobody waits for a lock, and the occasional conflict costs one retry or one 'this record changed, please review' message. That describes most business records: customer profiles, orders, settings and documents edited by one person at a time.
It fits badly when many writers update the same record constantly, such as a popular product's stock count or a shared counter. Then most attempts fail and retry, and under load the retries themselves add contention, just like a CAS loop on a hot variable. For those keys we serialize updates instead: route all changes for a key through one queue or Kafka partition with a single consumer, use a single-writer service that owns the key, or let the database apply a commutative change such as SET stock = stock - 1 WHERE stock > 0, which needs no version because it does not depend on what the client read.
6A Long-Term Stance for Service APIs
My stance would be that conditional updates are a standard part of API design, not something each team decides alone. Every resource that can be updated carries a version; GET returns it as an ETag; PUT, PATCH and DELETE accept If-Match, and for resources edited by people, require it with 428. Conflicts return 412 with a body that explains what happened, and the behaviour is documented in the API description so client authors know to handle it.
It also needs tests: a contract test that sends a stale If-Match and expects 412, and a concurrency test that runs two updates in parallel and asserts that one fails rather than both succeeding. Finally, for hot keys identified in design reviews, the documented alternative is single-writer processing, so teams do not respond to frequent 412s by quietly removing the check.
7Follow-up questions
Can the ETag simply be a hash of the content?
It can, and for reads that is fine, but for concurrency a hash of the content has the same weakness as comparing values: content can return to an earlier state, so the hash repeats. A monotonically increasing version, or a timestamp with enough resolution plus a tie-breaker, never repeats and is cheaper to check.
What should a client do after a 412?
Fetch the current representation and its new ETag, then either reapply its change if the change is independent, merge, or show the user the conflict. Blind automatic retry with the new ETag would recreate the lost update, so the client must make a decision based on the new state.
Do events and messages need versions too?
Often yes. A consumer that applies 'customer updated' events out of order can overwrite newer state with older. Including the entity version in the event and ignoring events with a version not greater than the stored one is the same principle applied to messaging.
8Conclusion
In this answer, we carried the ABA lesson from compare-and-set into service APIs: a check on values can be fooled by intermediate changes, while a check on a version that always increases cannot. We built conditional updates with a version column in SQL and JPA, exposed the version as an ETag with If-Match, 412 and 428 in HTTP, and noted that optimistic concurrency suits rare conflicts while hot keys need serialization or single writers. The stance is to make conditional updates a standard, documented and tested part of every API that allows updates.
Open with
The ABA lesson is that comparing a value only tells you the value is the same, not that nothing happened. In a CAS, A can go to B and back to A.
Then, if they want more
In a service, a balance can go from 100 to 150 and back to 100, so an update guarded by 'balance is still 100' is applied on top of history the client never saw. A version that increases on every change can't repeat, so checking the version is reliable.
In practice that's a version column, with the update written as SET fields and version plus one WHERE id and version equals the expected one, and zero rows updated means conflict; JPA's @Version does exactly that.
Over HTTP the version becomes the ETag: GET returns it, the client sends it back in If-Match, and the server answers 412 Precondition Failed if it no longer matches, or 428 if the client didn't send one at all. Optimistic concurrency is the right default when conflicts are rare and a retry is cheap, which covers most business records.
For hot keys, like a stock count that many writers hit constantly, it degrades into retry storms, so there I'd serialize updates per key, through one partition or a single-writer service, or use a commutative update in the database.
My stance for the organisation is to make conditional updates part of the API standard: every updatable resource has a version and an ETag, writes accept If-Match, conflicts are documented, and contract tests check that a stale ETag gets a 412.
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
- java.util.concurrent, package documentation, JLS chapter 17, Threads and Locks
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 "The ABA problem in compare-and-set has a counterpart in distributed systems: optimistic...": https://scrivio-interview-library.netlify.app/java/atomics/effc357c97/