API-33Recommended
Stop lost updates with ETags
Send an ETag with every resource, and require If-Match on updates, so one client can't silently overwrite another's change.
How to do it
Use strong ETags, such as a version number in quotes, because If-Match never matches a weak W/ tag. Answer 412 when the If-Match value no longer matches, as RFC 9110 defines. Answer 428 when an update arrives without If-Match on a resource that requires it, as RFC 6585 defines. The same ETags let readers use conditional requests. The database side is in conditional writes.
How to check
Two clients read the same order, and both send a PATCH with the first ETag. The second gets 412. A PATCH without If-Match gets 428.
Sources
Related rules
- WEB-44 Answer repeat requests with 304 in Website
- DAT-16 Make conditional writes in Data and databases
- APP-58 Never save over a newer version in Web app interface
- APP-21 Save drafts on their own in Web app interface
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/reliability/optimistic-concurrency">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.