Reputation: 391
We have a requirement where we need to store a Object which can be accesses and used by multiple instance of the function, please advise what would be the best way to achieve this.
Upvotes: 6
Views: 4443
Reputation: 329
I've been playing with Azure Functions to keep static data through the invocations. I think as long as function is not scaled out and the underlying process not being disposed by azure infrastructure the static properties persist through the multiple invocations. Even though azure functions are stateless by nature.
So I had few azure functions caching data in static concurrent dictionary, running on different timers - 5/10/20min, and all values were carried through the invocations unless you stop/restart the function. The cache was valid for a long time, e.g. few days till you stop or redeploy the function
Upvotes: 0
Reputation: 15042
Most of the time all your function executions are running in the same process, so you could use static class members to share state across function instances. However, this is not reliable. The process could go down at any time, or your function could be scaled out to multiple processes across multiple VMs, which means some functions will see one copy of the static data while others see a different copy. For that reason, static variables are really only useful as a best-effort transient cache.
If you want to share objects between function instances reliably, you'll either need to serialize your objects to some external data store, like Redis, or serialize your objects to the local file system, which is always available to all instances of your function.
Upvotes: 9