NibblyPig
NibblyPig

Reputation: 52952

Can I cache a single value in Azure Functions without any negative effects?

I have an Azure Function on a timer that activates every minute, which calls an API which returns an integer value. I store this value in SQL.

I then have another Azure Function that can be queried by the user to retrieve the integer value. This query could in theory be as high as hundreds or thousands of times per second.

Rather than have the second Azure Function query SQL every single time it gets a request, I would like it to cache the value in memory. If the cache were perfect there would be no need for SQL at all, but because Functions can scale up and also seem to lose their cache periodically there has to be some persistent storage.

Is it just a case of a static variable within the function to cache the value, and another with the last date retrieved? Or is there another type of caching that I can use within the function?

I understand there are solutions such as Redis but they seem pretty overkill to spin up just for a single integer value. I'm also not even sure if Azure SQL itself would cache the value when it's requested.

My question is, would a static variable work (if it's null/reset then we'd just do a quick SQL query to get the value) and actually persist? Or does an alternative like redis or similar exist that wouldn't be overkill for this application? And finally, is there actually any harm (performance problems) in hammering SQL over and over to retrieve a single value (i.e. is it clever enough to cache already so there's not a significant performance hit vs. querying a variable in memory)?

Upvotes: 3

Views: 4590

Answers (2)

Erndob
Erndob

Reputation: 2612

Really depends. If you understand the limitations of using in-memory cache in an azure function, and your business case is fine with those limitations, you should use it.

The main thing is you can't invalidate cache. So for example, if your number changes, it can be not usable for you. You will have cases where a container for your azure is spinning, and it has an old value. The same user could get different values on each request because who knows which instance he will hit, and what that instance is caching. If you number is something that is set only once and doesn't change, you don't have this issue.

And another important thing is that you still make quite a few requests just to cache it. Every new container will have to cache it for itself, while centralized cache would do it only once. This can be fine for something smaller, but if the thing you're caching really takes significant amount of time, or if the resources of the service are super limited, you would be a lot more efficient with centralized cache.

No matter what, caching in Azure Function level still reduces the load, and there's no reason to make requests when you don't have to.

To answer your sql question, yes, most likely the SQL server will cache it too, but your azure function still needs to establish a connection to sql server, make the request and kill the connection.

Upvotes: 2

Rahul Ruikar
Rahul Ruikar

Reputation: 1096

Azure functions best practices states that functions should be stateless and your state information should be with data. I think Redis is still the better option that SQL.

Upvotes: 0

Related Questions