'How long are global variables cached in Google Cloud Functions?
The document doesnt state anything about how long will it be cached for.
Solution 1:[1]
From the documentation,
Your function should be still prepared to work without this cache available as there is no guarantee that the next invocation will reach the same function instance
So there's no fixed time for this but I've seen it run around 25-30 minutes.
Solution 2:[2]
There is no "cache", there is only instance runtime configuration. Imagine an instance as your computer. You run your app.
- The question is: "How long the app is kept running on your computer?"
- Answer: "as long as your computer is running"
Same for instance! How long the data in glocal scope are living in the instance? As long as the instance live!
Usually the instances are offloaded after 15 (Cloud Run and Cloud Functions V2) and 30 minutes (Cloud Functions V1), when there is no new request handle by the instance. But that value can change, and it's not guarantee (especially if the underlying physical hardware has an outage, or a maintenance windows, the duration could be shorter).
At the opposite, if you always have a request to your instance (for instance every minute), you can keep the same instance living for hours or days (usually, instances are at least restarted once a week for security/underlying infra patching reason).
In fact, it's serverless, you don't manage the instance lifecycle.
Note: you can force an instance to shut down by exiting (os.exit(0)) or crashing (return exception)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dharmaraj |
| Solution 2 | guillaume blaquiere |
