What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
To prevent one client from silently overwriting another client’s status change, make each write conditional on the version the client last read. In an HTTP API, return an ETag and require it in an If-Match header; in a database, update the row only when its version still equals the client’s expected version. Compare and mutation must happen together on the server. If the comparison fails, reject the write as a conflict rather than applying stale data.
What compare-and-swap prevents
A lost update occurs when two clients read the same earlier state, then each submits a change. Without a concurrency check, the later write can overwrite newer work—even if that client never saw it. Compare-and-swap (CAS) makes a write conditional: apply the requested change only if the resource is still at the version the client observed.
As an Amazon Associate I earn from qualifying purchases.
The client’s version is an expectation, not authority. The server remains responsible for current state, authorization, validation, and whether the requested status transition is allowed. CAS detects stale writes; it does not enforce business rules such as whether pending may transition to approved.
Free tools Windows power users keep installed
One-click scans. No signup required.
Implement CAS in an HTTP API with ETags
Read the status and its validator
When the client fetches a status resource, return its representation with an ETag that changes whenever the relevant representation changes. For example:
#1 Best Overall
GET /items/42
HTTP/1.1 200 OK
ETag: "v17"
Content-Type: application/json
{"status":"pending"}
An ETag is useful as a validator only if the server’s generation and comparison rules reliably reflect changes relevant to the update. Treat it as opaque on the client: store the value and send it back rather than deriving or editing it.
Require the observed ETag on the write
For a state-changing request, send the ETag from the read in If-Match:
Rank #2
PATCH /items/42
If-Match: "v17"
Content-Type: application/json
{"status":"approved"}
RFC 9110 specifies strong comparison for If-Match and requires the condition to be evaluated before performing the method. Do not use a weak ETag for this concurrency check. If the current selected representation no longer matches, the server must not perform the requested method; 412 Precondition Failed is the standard response for a failed precondition. See RFC 9110, Section 13.1.1.
Recommended Free Tools
Return the new validator or report the conflict
On success, return the updated representation and its new ETag so the client can make a later conditional change using the latest version. If another write changed the resource first, leave the current state unchanged by this request and return the precondition failure. You may include current state when appropriate, or require the client to fetch it again.
The client should then refresh and decide what the requested change means against current state. It may present the conflict, merge or recompute an intent-based change, or retry a bounded number of times if recomputation is safe. It should not blindly resend a stale whole-resource replacement.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Implement CAS for a database record
For a versioned row, make the version comparison and mutation part of one atomic conditional write. Generic SQL pseudocode:
Rank #4
UPDATE items
SET status = :new_status,
version = version + 1
WHERE id = :id
AND version = :expected_version;
Inspect the affected-row count. One affected row means the expected version matched and the update was applied. Zero means either the record does not exist or its version changed; handle the stale-version case as a conflict, distinguishing a missing record if your API’s information-disclosure policy calls for it. Exact syntax and transaction behavior vary by database.
A check performed in application memory followed by a separate unconditional update is not CAS: another write can slip between the check and mutation. Keep the comparison and change indivisible in a conditional write or transaction.
Best Value
- Careercup, Easy To Read
- Condition : Good
- Compact for travelling
DynamoDB version checks
DynamoDB’s documented pattern uses a version attribute and a ConditionExpression, for example requiring Version = :expected_v as part of the write. A mismatch produces ConditionalCheckFailedException. The application can report a conflict, or fetch current state and recompute a safe update. AWS advises limiting retries because each retry adds a read. See Optimistic locking with version number.
Quick Recap
Handle conflicts without creating new ones
- Validate the transition independently. A matching version does not make an otherwise forbidden status transition valid.
- Separate conflict responses from other errors. Authorization failures, invalid requests, missing resources, and infrastructure failures are not stale-version conflicts.
- Bound retries. Refresh before retrying and recompute from current state. Do not loop indefinitely under contention.
- Protect side effects. If a status update triggers non-idempotent work, such as sending a notification or initiating a payment, a failed or repeated write must not accidentally duplicate that work. Design side effects with suitable transactional, outbox, or idempotency safeguards.
- Choose a clear client recovery path. After a conflict, let the client refresh and then present, merge, or deliberately recompute the change rather than silently overwriting.
Choose the concurrency strategy for the work
| Situation | Approach | Trade-off |
|---|---|---|
| Conflicts are infrequent, retries are inexpensive, and one item is being updated | Optimistic locking with a version and conditional write | Detects a conflict at write time without coordinating a lock in advance. AWS describes optimistic locking as detecting conflicts at write time rather than preventing them: DynamoDB documentation. |
| Several items must change together | Use a database transaction | Provides all-or-nothing semantics for the grouped changes. |
| Long-running critical work or high contention makes retries costly | Evaluate locking or another coordination strategy | Coordination adds complexity but may be preferable to repeated failed writes. |
| DynamoDB global tables receive writes in multiple Regions | Use explicit application-level conflict handling | Global tables reconcile with last-writer-wins, so version-based optimistic locking does not provide the expected cross-region protection. See AWS guidance on concurrent updates. |
Implementation checklist
- Return a validator or version with the state clients read.
- Require the observed value on every relevant write.
- Compare and mutate atomically on the server.
- Advance the version whenever the relevant state changes.
- Reject mismatches without applying the stale request.
- Refresh and resolve conflicts deliberately; retry only when bounded and safe.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




