4

The remove operation in RFC 6902 allows you to delete an element from an array in a JSON object, but also changes the array index of all the other elements.

If you retrieve a record using GET, and then delete an element from an array using PATCH, isn't there a risk that someone else could have deleted a different element in that array, which could change the index of the item you want to delete? Couldn't this cause you to accidentally delete the wrong item from the array?

Am I using this incorrectly, or does using PATCH to remove elements from an array require some kind of optimistic locking to function reliably?

Community
  • 1
  • 1
Chris B
  • 737
  • 2
  • 8
  • 21

1 Answers1

5

You are describing a concurrency issue. You should use an optimistic concurrency approach with ETags to perform a conditional PATCH operation.

http://fideloper.com/etags-and-optimistic-concurrency-control

Basically, first the client gets the representation of the resource with an ETag that would be a checksum of the content. When you want to perform a PATCH operation, you attach a If-Match HTTP header with the previous ETag as value. If the data did not change since you read it, the server will accept the operation and return HTTP 200. If otherwise the data changed, the server will return a HTTP 412 Precondition Failed indicating that the data the request is trying to modify has changed since.

vtortola
  • 34,709
  • 29
  • 161
  • 263