I want to use the Firebase callable functions to run checks on the back-end but I am worried about the cold-start. For instance, I want to check if a user has sufficient credits to download a certain product but I would like to avoid users waiting 10 seconds or more before this promise gets resolved or rejected, and running these checks from the front-end is no option as anyone could bypass them.
Is there a way to do provisioning for a selected group of callable functions on Firebase so that the whole experience doesn't feel slow and frustrating for users? Many users if they have to wait 10 seconds (even if its just for the first time) might give up using this service I want to sell...
CodePudding user response:
Is there a way to do provisioning for a selected group of callable functions on Firebase?
Yes, as explained in the doc, you can set a minimum number of instances for a given Cloud Function with the runWith
parameter, as shown below:
exports.myCallableCloudFunction = functions
.runWith({
// Keep 1 instance warm
minInstances: 1,
})
.https.onCall((data, context) => {
// Cloud Function code
});
You can have more than one instance warm by passing the desired value, e.g. minInstances: 3
.
Note that "a minimum number of instances kept running incur billing costs at idle rates. Typically, to keep one idle function instance warm costs less than $6.00 a month" (excerpt from the doc).
Note also that you need to configure each Cloud Function with this option, AFAIK you cannot apply it to a group of Cloud Functions.