Problem

Typically frontend expects response to be immediate (less than few seconds), but sometimes backend might take more time than expected. In these cases, requests would be timed out (AWS API Gateway has 30 seconds timeout).

Solution

One way to handle this to make backend asynchrous and do HTTP long polling from the front end.

1) Make the server async to accept any number of jobs.
2) Offload the time consuming part to a worker.
3) Periodically check from client if worker has finished the request.

More Options

  • Instead of long polling we can also use websocket to create a bidirectional connection and send the result from the server, when it is available. We do have a overhead of maintaing the websocket connection from server.

  • We can also use Server-sent Events. This is not a bidirectional connection. We can send events only from the server. Typical use of this technology is in stock tickers

References