To prevent mid-air collisions caused by multiple users or processes modifying the same item in the database, I am considering using Etags for validation. Here is my proposed approach:
- The client will call a RESTful API to obtain an Etag from the header, which will then be stored in the item object.
- When a user attempts to make changes to the item, the Etag will be sent back to the RESTful API as an If-Match header. The API will validate this against the current record, saving it if valid or rejecting it with code 412 if not.
- If the change is rejected, the client will display an error message instructing the user to refresh the page.
Questions:
Do you think this approach is effective?
How can I efficiently store the Etag value in the item object using fetch? Specifically, how can I merge the Etag received in the header with the data returned from response.json()
?
Thank you!