Conflict detection

Using a REST API with a single page application, I’ve had some issues with edit conflicts. User A and B both go to edit job number 15. User A saves first, then when B saves he wipes out A’s changes.

As a workaround I’ve been including a “__version” field in the GET response, which is a SHA-256 hash of the data. This is submitted in the PUT request. When the server receives this, it regenerates the hash. If they hashes match, the PUT is allowed. If they don’t match, it is rejected with a 409 error.

This works as a quick fix, although it’s got some issues - mostly that it offers no way to resolve an edit conflict. An alternative would be to always use PATCH instead of PUT, although there’s issues with that as well (e.g. if A and B both change the same field).

It would be really helpful if JSONAPI could suggest a standard way to do this. I’ve seen some apps use the If-Match header instead of a __version field.

A couple things…

If-Match + Etags are designed just for this, so I’d suggest using them if possible (and I can’t imagine a JSON:API recommendation ever contradicting that advice).

Second, there has been some work to standardize something like this for the JSON:API context, but it’s on pause at the moment. See https://github.com/json-api/json-api/pull/824 for details.