14

How can I persist a small amount of data between Azure Function executions? Like in a global variable? The function runs on a Timer Trigger.

I need to store the result of one Azure Function execution and use this as input of the next execution of the same function. What is the cheapest (not necessarily simplest) way of storing data between function executions?

(Currently I'm using the free amount of Azure Functions that everyone gets and now I'd like to save state in a similar free or cheap way.)

Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • 1
    What is the initial input to the function? How is your function triggered? – Gaurav Mantri May 25 '17 at 12:28
  • 1
    The function runs on a Timer Trigger. So say every 10 minutes. I add this info to the question. – Heinrich Ulbricht May 25 '17 at 12:29
  • Thanks! So I am assuming your function reads the data from somewhere when triggered. What is the source of that data? Can't you simply overwrite that? – Gaurav Mantri May 25 '17 at 12:33
  • The function consumes data from a read-only third-party REST endpoint. I need to detect differences in the response between runs of the function. (The responses will rarely change. I need to detect when this happens.) – Heinrich Ulbricht May 25 '17 at 12:37
  • 1
    I would think that azure storage would be the easiest way to store the data – 4c74356b41 May 25 '17 at 12:47
  • @4c74356b41 If that is the solution so be it. My hope is/was that there would be a simple solution like `AzureGlobals.Properties["key"] = "value"` (or something easy along these lines) where Azure takes care of the rest, with no additional setup required. And for free of course :) I just don't want to overlook something like this before using a more involved solution. – Heinrich Ulbricht May 25 '17 at 12:58
  • 1
    I'm sure Azure Functions are supposed to be stateless, that's the whole idea – 4c74356b41 May 25 '17 at 13:05
  • How about just writing a file to %temp%? That's d:\local\temp i believe. Just open Kudu and you'll find out on the Environment page. – evilSnobu May 25 '17 at 18:31
  • finally what you have used? I want to do the same please share your idea. thanks – Neo Oct 04 '20 at 13:30

3 Answers3

12

There are a couple options - I'd recommend that you store your state in a blob.

You could use a blob input binding to read global state for every execution, and a blob output binding to update that state.

You could also remove the timer trigger and use queues, with the state stored in the queue message and a visibility timeout on the message to set the schedule (i.e next execution time).

Finally, you could use a file on the file system, as it is shared across the function app.

If you can accept the possibility of data loss and only care at the instance level, you can:

  • maintain a static data structure
  • write to instance local storage
Matt Mason
  • 2,676
  • 9
  • 22
  • 3
    Any docs on using the file system? I was under the impression that functions can at any time migrate from one physical host to the next, or run on several hosts, given enough load and appropriate provisioning. – Cristian Diaconescu Jul 26 '18 at 09:54
6

This is old thread but worth sharing the new way to handle state in Azure function.

Now we have the durable function approach from Microsoft itself where we can maintain the function state very easily and effectively. Please refer the below documentation from MS.

https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-overview

6

Durable entities are now available to handle persistence of state.

https://learn.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-entities?tabs=csharp

Lance
  • 592
  • 2
  • 7
  • 13
  • This worked great when I used it to store an access token, but when I tried storing a bit more data I got unreliable behaviour so sadly I think I will have to use blob storage. See https://stackoverflow.com/questions/70170913/azure-durable-entity-for-storing-function-app-state-between-runs-net-6 – Tarostar Dec 02 '21 at 17:50