7

Firebase has announced in September 2021 that it is possible now to configure its cloud function autoscaling in a way, so that a certain number of instances will always be running (https://firebase.google.com/docs/functions/manage-functions#min-max-instances).

I have tried to set this up, but I can not get it to work: At first I have set the number of minimum instances in Google Cloud Console: Cloud Console Screenshot After doing this I expected that one instance for that cloud function would run at any time. The metrics of that function indicate that it instances were still scaled down to 0: Cloud functions "Active Instances Metric"

So to me it looks a bit as if my setting is ignored here. Am I missing anything? Google Cloud Console shows me that the number of minimum instances has been set to 1 so it seems to know about it but to ignore it. Is this feature only available in certain regions?

I have also tried to set the number of minimum instances using the Firebase SDK for Cloud Functions (https://www.npmjs.com/package/firebase-functions). This gave me the same result, my setting is still ignored.

  • 1
    can you redeploy the function and check again or refer to the link :https://www.ayrshare.com/a-firebase-cloud-functions-cold-start-solution/ Is it helpful? – Divyani Yadav Oct 26 '21 at 12:21
  • 1
    Good call, I have tried redeploying the function (I also tried to create a completely new one). In both cases the specified number of min instances is running for a minute or so. Then they are scaled back to 0. I have also checked the functions logs but I could not find any explanation for that so far. – Professor Hackfred Oct 26 '21 at 21:08
  • Posted an answer, Is it helpful? – Divyani Yadav Oct 30 '21 at 06:36
  • 1
    yes absolutely, thanks :) – Professor Hackfred Oct 31 '21 at 18:11

1 Answers1

5

According to the Documentation, the Active Instances metrics shows the number of instances that are currently handling the request.

As stated in the Documentation :

Cloud Functions scales by creating new instances of your function. Each of these instances can handle only one request at a time, so large spikes in request volume often causes longer wait times as new instances are created to handle the demand.

Because functions are stateless, your function sometimes initializes the execution environment from scratch, which is called a cold start. Cold starts can take significant amounts of time to complete, so we recommend setting a minimum number of Cloud Functions instances if your application is latency-sensitive.

You can also refer to the Stackoverflow thread where it has been mentioned that

Setting up minInstances does not mean that there will always be that much number of Active Instances. Minimum instances are kept running idle (without CPU > allocated), so are not counted in Active Instances.

Divyani Yadav
  • 1,030
  • 4
  • 9
  • 1
    Thanks for the answer. I should have looked into the exact definition of these metrics first :) (I was a bit confused, as my runtimes did not really decrease, after setting min-instances. It turned out though that this was about a problem with the firestore sdk). – Professor Hackfred Oct 31 '21 at 18:11
  • @ProfessorHackfred Can you elaborate on the firestore sdk problem? I've got min instances set and it still feels like my functions go through a cold start. The first invocation can take ~7 seconds, then after that it's about 700ms. – Johnny Oshika Jan 22 '22 at 08:29
  • @JohnnyOshika Apparently the Firestore Node.Js SDK provokes some bad cold start times that can not be lowered by making use of the "min instances" setting. There has been a [Github Issue](https://github.com/googleapis/google-cloud-node/issues/2942) (but that has been closed without being fixed). Finally [a ticket](https://issuetracker.google.com/issues/158014637?pli=1) has been created and the Google Support seems to have taken notice (without fixing it). As a workaround, increasing your functions memory can speed up these cold starts (as mentioned in the issue's conversation). – Professor Hackfred Feb 11 '22 at 22:08
  • @ProfessorHackfred Thanks for that link. I had no idea that there were all kinds of problems with the Firestore SDK and Cloud Functions. I'll need to spend time digesting all that info. How did you solve the cold start problem? – Johnny Oshika Feb 14 '22 at 15:38