Reputation: 20396
It is mentionned in the doc that
Functions are stateless, and the execution environment is often initialized from scratch, which is called a cold start.
This statement implies that * sometimes *, functions are not initialized from scracth and the state can be reused.
Is this a reliable assumption ?
var count = 0; // global variable
functiion mycouldFunction(){
count ++;
return count;
}
If I call myCloudFunction
twice with X amount of time apart, should I expect it to return '1' then '2'
Then a 3rd call after Y amount time, I should expect it to return '1' again ?
If that is true, what is the value of X and Y ?
Upvotes: 1
Views: 616
Reputation: 317352
Cloud Functions will indeed sometimes reuse server instances to handle multiple requests, as an optimization. There is no guarantee if or when this can happen. Unless you are implementing some sort of optimization on your own (memory caching, for example), it's not advisable to store anything in global memory, since your function could be subject to a cold start for any one of its invocations.
Upvotes: 2
Reputation: 3842
No, cloud functions are stateless so every time you call myCloudFunction
it would return 1.
To achieve what you want you would need to store the updated value in Firestore or RTDB or similar and read the updated value on each load.
What "Cold Start" refers to has nothing to do with the above, it's in reference to how functions are served. GCF will "turn off" functions that are infrequently used so they may respond slower than functions that are "turned on" and ready to receive requests.
Upvotes: -1