4

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.

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
Javed Akhter
  • 41
  • 1
  • 3
  • 1
    Have you though about using redis ? – Thomas Jul 26 '16 at 23:55
  • Yup, that's a good option in general. However, I am not considering other options unless the .NET caching doesn't work in this scenario. The reason is that my Azure function is individually managed by customers on their subscriptions to run some preprocessing logic on their input files, before the files get processed by the central service. I wanted to keep the function as light weight as possible with limited external dependencies. – Javed Akhter Jul 27 '16 at 17:44

2 Answers2

10

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!

Fabio Cavalcante
  • 12,328
  • 3
  • 35
  • 43
1

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.

Larry Maccherone
  • 9,393
  • 3
  • 27
  • 43