Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In-memory caching in Azure function

There is a need to cache objects to improve the perf of my Azure function. I tried .NET ObjectCache (System.Runtime.Caching) and it worked well in my testing (tested with upto 10min cache retention period).

In order to take this solution forward, I have few quick questions:

  1. What is the recycling policy of Azure function. Is there any default? Can it be configured?

  2. What is the implication in the cost?

  3. Is my approach right or are there any better solutions?

Any questions that you may know, please help.

Thank you.

like image 940
Javed Akhter Avatar asked Dec 18 '22 14:12

Javed Akhter


2 Answers

Javed,

An out-of-process solution such as Redis (or even using Table storage, depending on the workload) would be recommended.

As a rule of thumb, functions should be stateless, particularly if you're running in the dynamic runtime, where scaling operations (up and down) could happen at any time and your host is not guaranteed to stay up.

If you opt to use the classic hosting, you do have a little more flexibility, as you can enable the "always on" feature, but I'd still recommend the out-of-process approach. Running in the classic mode does have a cost implication as well, since you're no longer taking advantage of the consumption based billing model offered by the dynamic hosting.

I hope this helps!

like image 167
Fabio Cavalcante Avatar answered Feb 22 '23 23:02

Fabio Cavalcante


If you just need a smallish key-value cache, you could use the file system. D:\HOME (also found in the environment variable %HOME%) is shared across all instances. I'm not sure if the capacities are any different for Azure Functions, but for Sites and WebJobs, Free and Shared sites get 1GB of space, Basic sites get 10GB, and Standard sites get 50GB.

Alternatively, you could try running .NET ObjectCache in production. It may survive multiple calls to the same instance (file system or static in-memory property). Note, this will not be shared across instances though so only use it as a best effort cache.

Note, both of these approaches pose problems for multi-tenant products as it could be an avenue for unintended cross-tenant data sharing or even more malicious activities like DNS cache poisoning. You'd want to implement authorization controls for these things just as if they came from a database.

As others have suggested, Functions ideally should be stateless and an out of process solution is probably best. I use DocumentDB because it has time-to-live functionality which is ideal for a cache. Redis is likely to be more performant especially if you don't need persistence across stop/restart.

like image 24
Larry Maccherone Avatar answered Feb 23 '23 00:02

Larry Maccherone