Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle REST API timeout in time consuming operations

Tags:

rest

How is possible to handle timeouts in time consuming operations in a REST API. Let's say we have the following scenario as example:

  1. A client service sends a request to insert a resource through a REST API.
  2. Timeout elapses. The client thinks the insertion failed.
  3. REST API keep working and finishes the insertion.
  4. Client do not notify the resource insertion and it status is "Failed".

I can think I a solution with a message broker to send orders to a queue and wait until they are solved.

Any other workaround?

EDIT 1:

  • POST-PUT Pattern as has been suggested in this thread.
  • A Message Broker (add more complexity to the system)
  • Callback or webhook. Pass in the request a return url that the server API can call to let the client know that the work is completed.
like image 492
Esteban S Avatar asked Jul 25 '18 08:07

Esteban S


People also ask

How do you handle API timeout?

Basically you can do it in 2 ways. The first one, is wrapping your REST Call in an action, and in this action you can create an error handler flow: This way you can have a specific message if the default error message or the error code is the one you want to handle.

How do you implement timeout in REST API?

One way we can implement a request timeout on database calls is to take advantage of Spring's @Transactional annotation. It has a timeout property that we can set. The default value for this property is -1, which is equivalent to not having any timeout at all.

How does Web API handle timeout exception?

If you really need to implement a timeout on the API side itself, I would recommend creating a thread to do your work in, and then cancelling it after a certain period. You could for example put it in a Task , create your 'timeout' task using Task. Wait and use Task. WaitAny for the first one to come back.


1 Answers

HTTP offers a set of properties for invoking certain methods. These are primarily safetiness, idempotency and cacheability. While the first one guarantees a client that no data is modified, the 2nd one gives a promise whether a request can be reissued in regards to connection issues and the client not knowing whether the initial request succeeded or not and only the response got lost mid way. PUT i.e. does provide such a property, i.e.

A simple POST request to "insert" some data does not have any of these properties. A server receiving a POST request furthermore processes the payload according to its own semantics. The client does not know beforehand whether a resource will be created or if the server just ignores the request. In case the server created a resource the server will inform the client via the Location HTTP response header pointing to the actual location the client can retrieve information from.

PUT is usually used only to "update" a resource, though according to the spec it can also be used in order to create a new resource if it does not yet exist. As with POST on a successful resource creation the PUT response should include such a Location HTTP response header to inform the client that a resource was created.

The POST-PUT-Creation pattern separates the creation of the URI from the actual persistence of the representation by first firing off POST requests to the server until a response is received containing a Location HTTP response header. This header is used in a PUT request to actually send the payload to the server. As PUT is idempotent the server simply can reissue the request until it receives a valid response from the server.

On sending the initial POST request to the server, a client can't be sure whether the request reached the server and only the response got lost, or the initial request didn't make it to the server. As the request is only used to create a new URI (without any content yet) the client may simply reissue the request and in worst case just create a new URI that points to nothing. The server may have a cleanup routine that frees unused URIs after a certain amount of time.

Once the client receives the URI, it simply can use PUT to reliably send data to the server. As long as the client didn't receive a valid response, it can just reissue the request over and over until it receives a response.

I therefore do not see the need to use a message-oriented middleware (MOM) using brokers and queues in order to guarantee reliable messaging.

like image 104
Roman Vottner Avatar answered Oct 23 '22 17:10

Roman Vottner