How does a Cloud Function instance handles multiple requests?

Viewed 768

I trying to wrap my head around Cloud Function's instances and how they work.

I'm asking about an example of an HTTP function, but I think the concept applies to any kind of function.

  • Let's say I have this cloud function that handles SSR for my app, named ssrApp.
  • And let's assume that it takes 1 second to complete every time it gets a request.
  • When Cloud Function receives the 1st request, it will spin up an instance to respond it.

QUESTION

How does that instance behave when multiple requests are coming?

From: https://cloud.google.com/functions/docs/concepts/exec

Each instance of a function handles only one concurrent request at a time. This means that while your code is processing one request, there is no possibility of a second request being routed to the same instance. Thus the original request can use the full amount of resources (CPU and memory) that you requested.

Does it mean that during that 1 second when my ssrApp function is running, if somebody hits my app URL, it is guaranteed that Cloud Function will spin up another instance for that second request? Does it matter if the function does only sync calls or some async calls in its execution? What I mean is, could an async call free the instance to respond to another request in parallel?

2 Answers

Does it mean that during that 1 second when my ssrApp function is running, if somebody hits my app URL, it is guaranteed that Cloud Function will spin up another instance for that second request?

That's the general behavior, although there are no guarantees around scheduling.

Does it matter if the function does only sync calls or some async calls in its execution? What I mean is, could an async call free the instance to respond to another request in parallel?

No, that makes no difference. If the container is waiting for an async call, it it still considered to be in-use.

Related