title: Azure Durable Entity for storing function app state between runs (.net 6)
body: <p>I want to run a simple .net 6 c# consumption azure function app (not durable) every minute, but I need to remember the state from the previous run.</p>
  <p>The state consists of arrays of json serializable objects, and a few access token strings.</p>
  <p>So I create a durable function entity like this:</p>
  <pre><code>[JsonObject(MemberSerialization.OptIn)]
  public class DurableStorageEntity
  {
      [JsonProperty(&quot;simpledata&quot;)]
      public string SimpleData { get; set; }
  
      [JsonProperty(&quot;complicateddata&quot;)]
      public List&lt;ComplicatedObject&gt; ComplicatedData { get; set; }
  
      public void SetSimpleData(string data) =&gt; this.SimpleData = data;
  
  
      public void SetComplicatedObject(List&lt;ComplicatedObject&gt; data) =&gt; this.ComplicatedObject = data;
  
      [FunctionName(nameof(DurableStorageEntity))]
      public static Task Run([EntityTrigger] IDurableEntityContext ctx)
          =&gt; ctx.DispatchAsync&lt;DurableStorageEntity&gt;();
  }
  </code></pre>
  <p>This works great, but I am unsure how efficient this is and how much state data I can store this way. After testing I am also unsure if this is reliable when called from a regular (i.e. non durable) function app.</p>
  <p>I can't find any clear information on this. In the <a href="https://docs.microsoft.com/en-us/azure/azure-functions/durable/durable-functions-entities?tabs=csharp" rel="nofollow noreferrer">MS document on Entity Functions</a> it simply states:</p>
  <blockquote>
  <p>Entity functions define operations for reading and updating small pieces of state, known as durable entities.</p>
  </blockquote>
  <p>And</p>
  <blockquote>
  <p>Entities provide a means for scaling out applications by distributing the work across many entities, each with a modestly sized state.</p>
  </blockquote>
  <p>So this leaves me unsure how much data I can store. I will generally be storing a couple of arrays with a few hundred items each of objects made up of strings, ints and dates. If there is a better way to store state between runs then alternatives are welcome, but I do like using these entity functions so I am hoping it is a viable and reliable option.</p>
  <p><strong>Update 2-december</strong>:</p>
  <p>I have been testing storing lists of objects with an 100 item array of objects (strings, int, datetime).</p>
  <p>I am running this locally on my machine. I might have done something wrong, but occasionally it returns old data I stored previously instead of the latest data stored in the durable entity storage. I am not sure why this is happening. I can get it to happen by storing large amounts of data, but it also happens randomly at other times.</p>
  <p>Generated 100 objects
  SIZE: 6 365 853 bytes
  Read Time: 250ms
  Write Time: 215ms</p>
  <p>Generated 1000 objects
  SIZE: 63 658 851  bytes
  Read Time: 2s 659ms
  Write Time: 1s 811ms</p>
  <p>Generated 10000 objects
  Write seemed to succeed, but on read got previous 1000 objects</p>
  <p>Generated 10 objects
  Read back 10 objects as expected</p>
  <p>Generated 2000 objects
  Error: still gets 10 objects</p>
  <p>Generated 1000 objects
  Error: still gets 10 objects</p>
  <p>Generated 100 objects
  Error: still gets 10 objects</p>
  <p>Generated 1 object
  Finally got 1 object back as expected</p>
  <p>Generated 10 objects
  Error: still gets 1 object for three runs, but then on fourth run of the function app suddenly gets 10 objects as it should.</p>
  <p>This is on my local machine, win10, i7, 32Gb RAM and without debugger in VS2022.</p>