Your backend takes too long to answer

One thing I've observed is that people make APIs like they are a ping-pong game. Literally they design all endpoints to respond "as soon as possible". So for every request that arrives to the server, you process it and then reply with the response.

All of this is good most of the times and it works all right for simple requests. But what happens when you need to do a lot of processing before the response? In this scenario, the client is literally waiting for your response, and you and your server are just taking your time to answer and processing whatever you have to, "as fast as possible".

Do you think this is the right approach? Not really, and that's why it exists the pattern of Long Running Operations (LROs).

For example, in a REST API, if your original request was something like POST /process, with LROs you don't hit your resource endpoint specifically. You will have a LRO resource and this one will be in charge of executing what you want. So your system will have a a new resource called LROs which will be kind of abstract because they will be used for all the LROs of your system. It's during your call where you tweak what it should be run, something like:

{
	type: "process"
	metadata: {
		data_specific_for_process
	}
}

Then once that LRO is created, which is fast because you don't have to process anything to create the LRO, you can reply the client with the LRO id, so the client can query the status of it wherever it wants. The LRO will kick in the underneath operation.

You can even make cool stuff like having a LRO cancel endpoint where you specify the LRO id and the long operation gets cancelled and stop processing whatever operation was doing. This of course varies depending on what your system does, you can imagine this is not easy if the long operation is moving data between volumes.

And this is my introduction to LROs. Here is an example from Google for LRO to download files. This pattern is heavily used by them. If you are interested in seeing a real example in place, let me know!

Google LRO example

Your backend takes too long to answer - Javier Guzman