开发者

If a REST web service call fails, should a message or event queue be used to retry later?

开发者 https://www.devze.com 2023-01-29 08:51 出处:网络
I\'m building a web service with a RESTful interface (lets call it MY_API). This service relies on another RESTful webservice to handle certain aspects (calling it OTHER_API). I\'d like to determine w

I'm building a web service with a RESTful interface (lets call it MY_API). This service relies on another RESTful webservice to handle certain aspects (calling it OTHER_API). I'd like to determine what types of best practices I should consider using to handle failures of OTHER_API.

Scenario

My UI is a single page javascript application. There are some fairly complex actions a user can take, which can easily take the user a minute or two to complete. When they are done, they click the SAVE button and MY_API is called to save the data.

MY_API has everything it needs to persist the information submitted by the user. However, there is an action that must take place that is handled by OTHER_API. For instance, OTHER_API might handle sending out an emails. Or perhaps it handles adding line items to my user's billing statement. In both cases, these are critical things than must be completed, but they don't have to happen right now, they just need to happen eventually.

If OTHER_API fails, I don't want to simply tell the user their action has failed, as they spent a lot of time doing it and this will make the experience less than optimal.

Questions

  • So should I create some sort of Message or Event Queue that can save these failed REST requests to OTHER_API and process them later?
  • Any advice or suggestions on techniques to go about saving REST requests for delayed processing?
  • Is there a recomme开发者_JAVA技巧nded open source message queue solution that would work for this type of scenario with JSON-based REST web services? Java is preferred as my backend is written in it.
  • Are there other techniques I should consider?


Rather than approach this by focusing on the failure state, it'd be faster and more robust to recognize that these actions should be performed asynchronously and out-of-band from the request by the UI. You should indeed use a message/event/job queue, and just pop those jobs right onto that queue as quickly as possible, and respond to the original request as quickly as possible. Once you've done that, the asynchronous job can be performed independently of the original request, and at its own pace — including with retries as needed.

If you want your API to indicate that there are aspects of the request which have not completed, you can use the HTTP response Status Code 202 (Accepted).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号