Batch processing via REST service

Abhijeet Patel picture Abhijeet Patel · Apr 25, 2014 · Viewed 7.6k times · Source

Are there any best practices for performing BATCH operations via REST for POST, PUT, PATCH verbs?

The current paradigm I am following is that the JSON payload is specified in the body for all 3 operations:

a) POST to return the location of the created resource
b) PUT / PATCH return a 201 if the update is successful

For a batch operation, I intend to accept a collection of JSON objects in the payload body but am trying to figure what to return to the client.

While processing the batch, the operation may succeed for some of the items but might fail for others.

Taking this into account, my take is that the best thing to do is to return a collection of objects indicating the Success/Failure status of each item from the payload.

But this deviates from my paradigm outlined in (a) and (b) above.

Instead, does it make sense to return an identifier representing an ID of the Batch operation itself to the client?

The client would then issue a subsequent GET to get the result of the operation it requested.

Does this approach sound reasonable? If so, does it make sense to block the client on the subsequent GET if the operation hasn't completed OR does it make sense to always return the most current state i.e. a collection of responses for each of the items that the client requested to process.

Ideas/Thoughts/Suggestions?

Since REST is an architectural style with no necessarily clear "guidelines" and no mandate on how the actions for HTTP verbs should be implements, clearly there is no right or wrong answer here.

I am looking for a solution that is elegant, natural and intuitive.

Answer

Nicholas Shanks picture Nicholas Shanks · Apr 25, 2014

REST operations are supposed to be atomic as seen from the outside. That is, if one part of the request fails, then the whole state of the server should revert to the pre-request state and a 4xx or 5xx response returned (so, for example, the request could be repeated in whole without ill effects if it failed the first time). This however has nothing to do with batch operations per se—such a request could be any kind of request.

Batch operations violate a different REST constraint, that of the uniform interface (defined by HTTP's methods and their operation upon a resource at the specified URL).

If you want to do batch operations, give up on trying to call your API RESTful, because you have already lost out on the benefits that REST imparts, and are just lying to yourself.

If you want to retain those benefits, give up on batch operations.