0

I've been facing an annoying issue when calling my firebase cloud functions. They seem to go through cold starts pretty frequently.

For context, my stack is the following:

  • Mobile: Flutter
  • Backend: Cloud Functions

I've configured every function to have the following parameters:

{ 
  timeoutSeconds: 120,
  memory: '512MB',
  minInstances: 3
}

and the functions each live in their own file e.g. ./this-is-an-endpoint.ts which then gets exported in a larger index.ts like the following:

export const sampleEndpoint = functions
  .runWith(PARAMS_ABOVE_GO_HERE)
  .https.onCall(async (data) => {
    return (await import('./this-is-an-endpoint')).default(data);
  });

At the top of each function I have an init() function that gets called at the beginning and looks like:

function init() {
  if (initialized) {
    log instanceIdForLogging as warm start;
    return;
  }
  instanceIdForLogging = generate();
  client = new ApiClient();
  ...more global inits
  initialized = true;
}

After all this (and starting to log unique ids per instance), I'm seeing frequent cold starts. I was under the assumption with min instances, I'd only have 3 unique IDs at most after a deploy but I'm seeing way more than that. I'm only getting a couple requests an hour at most so it's definitely not scaling up to handle more requests.

Been stuck debugging for a while so any thoughts here would be amazing and helpful. Thanks!

  • Do you see any errors or exceptions in the function logs? Are the requests infrequent enough that your function instance is being recycled? That will cause a cold start on the next request even with min instances. How long does your function take to initialize on cold start? Dependencies can dramatically affect this. Edit your question with details. – John Hanley Aug 24 '22 at 06:17
  • @JohnHanley Do errors or exceptions cause the instance to restart? There haven't been any in some of the functions at all. Requests are pretty infrequent for some functions (1-2 an hour) but wouldn't the instance just idle in the warm state based on the docs? Function takes about 2 seconds to initialize on a cold start. I made sure to only import packages needed per function that's why it's split into separate files. – Sujith Vishwajith Aug 24 '22 at 16:31
  • There is a lot of confusion about what idle means. If your instance is not processing requests, it is eligible for termination. Google sometimes calls that recycling. Write your code so that there are zero unhandled exceptions. Just one will force a cold start on the next request. If your function takes 2 seconds to cold start, consider migrating to Cloud Run so that you can build the dependencies into the container. Cloud Run also supports Always On CPU so that an instance is always available. – John Hanley Aug 24 '22 at 19:01

0 Answers0