How to call cloud functions asynchronously
Case description
- Implementing an asynchronous call of a cloud function.
- Closing the connection and yielding a response immediately without waiting for the cloud function to complete.
- Keeping a function running for longer than 10 minutes.
Solution
Cloud Functions does not support direct asynchronous calls: as long as a Serverless Containers container or cloud function is running, it cannot return an HTTP response. Then, execution stops as soon as the container or function sends a response.
However, you can implement this workflow using API Gateway and Data Streams as a workaround.
- Create an API Gateway gateway to act as a webhook handler, and connect it to a Data Streams data stream.
- Configure a data stream trigger that will call your container or cloud function.
In this setup, the API Gateway gateway will immediately return an HTTP 200 OK response, while the container or function will process the request asynchronously.
Warning
Note that code execution inside a Serverless Containers container or cloud function, even when triggered via a stream trigger, cannot exceed 10 minutes. For more information, see this guide.
If your workload can be split into multiple stages, you can build an asynchronous pipeline, when the first call performs its computation and then triggers the next stage by writing to the same Data Streams data stream. Keep in mind, however, that subsequent calls may run on different container or function instances.