Concept
Optimistic locking
Every entity in Codemeta OS has a numeric version field. It starts at
1 and is incremented by one on every successful update. You use it
to detect concurrent writes.
How it works
- You read an entity via
GETand note itsversion. - You send a
PATCHand put the notedversionin the body object. - If the database version still matches, the update goes through and
versionis set to<n+1>. - If not, the API responds with 409 Conflict and you know that another writer got there before you.
Example
# Step 1: read the current state
curl https://os.codemeta.de/api/v1/tickets/<id> \
-H "X-Tenant-Id: $TENANT" \
-b cookies.txt
# → { "id":"…", "version": 7, "title":"…", … }
# Step 2: update with version
curl -X PATCH https://os.codemeta.de/api/v1/tickets/<id> \
-H "X-Tenant-Id: $TENANT" \
-H "Content-Type: application/json" \
-b cookies.txt \
-d '{"version": 7, "priority": "high"}'
If the server version is now 8, you get:
{
"type": "https://codemeta-os.de/probs/version-conflict",
"title": "Version conflict",
"status": 409,
"detail": "The entity has been modified since you last read it.",
"currentVersion": 8
}
Strategy for 409
- Read again. Fetch the current entity, merge your change with
the new fields, and send the update with the new
version. - Don’t blindly retry. Without re-reading, your retry runs into the same 409 response.
- In UI workflows, ask the user. If the other changes collide in substance, an auto-merge strategy is rarely right.