The Art of Writing Software

REST-based service design, v2

Series [ REST-Based Service Design ] Tags [ CRUD, REST API ]

I want to revisit the basic “favorite food” service from last post, in light of some further discussion I’ve had with colleagues.

So, PUT can create a resource if it doesn’t exist, and DELETE of a URI means the next GET of it (in the absence of any other operations) will be a 404 NOT FOUND.

One note is that as a client of this API, I want and can make use of the most atomic operation available. For example, to make “spaghetti” a favorite food, I could either:

  1. GET http://host/path/{userid}/favorites
  2. PUT http://host/path/{userid}/favorites (new list with spaghetti added in)

or I could just:

  1. PUT http://host/path/{userid}/favorites/spaghetti

Note that in the first case, I might have an atomicity issue in the presence of concurrent access, so I might need to build in some sort of optimistic locking protocol, where the representation of the favorites list has a version number on it that is checked by the PUT operation. However, if I just use the second method, I don’t have this issue, because the server handles all my concurrency/transactional stuff for me.