We're in the process of preparing some general guidelines for RESTful APIs, and are trying to make operations idempotent where possible (including updates i.e. PUT operations).
One apparent wrinkle we've run into is with respect to optimistic locking. If we use the classical optimistic locking pattern of:
1. storing an "update token" with each entity, and returning it along with the entity when that entity is requested
2. validating that the update token included in an update request matches the entity's current update token, before allowing the update to occur
3. mutating the update token if the update goes ahead
we find that PUTs are no longer idempotent.
Has anyone else run into this? Is it (as we believe to be the case) simply that optimistic locking is fundamentally at odds with idempotent updates?
Thanks in advance for any thoughts you have to share!
Peter
Is there a reason you are not using ETags and If-Match (i.e. Conditional PUT) ?
Regardless, the requests are idempotent - the important factor is sender intent. As far as the client is concerned, the request can be repeated an arbitrary amount of times, and the fact that the outcome of the requests can change over time is not of consequence to that. The important point for the client is that they can safely repeat the PUT.
This is same issue as 'non-safety' of GET requests that increment a hit-counter.
Cheers,
Mike
Cheers,
Peter