Lost Updates: Optimistic Concurrency With ETag and If-Match¶
Two people open the same product in your admin app. Ana fixes the price. Ben, a minute later, fixes a typo in the name and saves the product as he loaded it, with the old price. Ana's change is gone, nobody got an error, and nobody will notice until a customer does.
This is a lost update, and every API that lets clients read, change and write back an object has it, unless it does something about it.
Locks or versions¶
There are two ways to stop it.
Pessimistic locking makes Ben wait: whoever opens the object first locks it until they save. It is safe, but over an HTTP API it is fragile. A browser tab left open holds the lock for hours, and a client that crashes never releases it.
Optimistic concurrency control lets everybody read and edit, and checks at write time that nobody changed the
object in between. Conflicts are rare in most apps, so the check almost always passes, and when it doesn't, the client
finds out instead of silently overwriting. HTTP was designed for this: the server tags each version of a resource
with an ETag, and the client sends it back in an If-Match header (RFC 9110).
How it works in the REST API¶
Every ObjectsGrid object has a version, meta.objectVersion, which every read returns as the ETag header:
Send it back with PUT, PATCH or DELETE:
curl -X PATCH https://apis.objectsgrid.com/products/<id> \
-H 'Content-Type: application/json' \
-H 'X-ObjectsGrid-AccessPoint: EU' \
-H "Authorization: Bearer $TOKEN" \
-H 'If-Match: "1763025002419837"' \
-d '{"color": "Green"}'
If the object is still at that version, the write happens and the answer carries the new ETag. If someone changed it
since, the answer is 412 Precondition Failed and nothing is written. In the story above, Ben's save is refused,
and his app can show him Ana's price before he saves again.
And in GraphQL¶
GraphQL has no headers per mutation, so the version travels as an argument. Read meta.objectVersion, and send it
back as expectedVersion:
mutation {
updateProduct(
input: {
id: "<id>"
name: [{ at_language: "en", at_value: "Trail Running Shoe, 2nd edition" }]
}
expectedVersion: "1763025002419837"
) {
id
meta { objectVersion }
}
}
A stale version gets a PRECONDITION_FAILED error, and nothing changes. See the
GraphQL guide.
What to do on a conflict¶
- Read the object again, to get its current state and version.
- Reapply your change to it. For a single field, such as a stock count, that is usually automatic; for a form a person filled in, show them what changed and let them decide.
- Write again with the new version.
Don't retry blindly with the old body: that is the lost update again, one step later.
Even without If-Match¶
Not every client sends a version, so ObjectsGrid protects writes without one too. Each write applies to the version
your access was checked against: if the object changes between the check and the write, for example its tags and so
who may change it, ObjectsGrid checks again. If it keeps changing, the answer is 409 Conflict, and you can retry.
That protects your access control, but it can't know what your client read. To
protect your users' changes from each other, send If-Match.
Versions you can go back to¶
Every write keeps the previous version, and you can read any of them with ?version=. After a conflict, that lets an
app show exactly what the other person changed. See REST API.
Optimistic concurrency costs one header. It is the cheapest way to make sure that what your users save is what they meant to save.