title: IoT EventHub with Azure Function App - synchronous multi-threaded
body: <p>I have problem with Azure FunctionApp that is triggered by EventHub (from IotHub). When I start function and there are a lot of historic messages in Event Hub FunctionApp is triggered asynchronous so there are few instance of function in te same time. The problem is that inside function I'm using Redis, so the flow is:
  EventHub Message -&gt; Trigger FunctionApp -&gt; Read data form Redis -&gt; Main function -&gt; Save data to Redis -&gt; Set output messages -&gt; End function. Every time the function is triggered it should end before antother instance of it. So it shoul be synchronous. I tried setting:</p>
  <p><code>WEBSITE_MAX_DYNAMIC_APPLICATION_SCALE_OUT = 1</code></p>
  <p>setting App Scale out to 1 ( was 200),
  but it doesn't work.</p>
  <p>This is crutial for us. After we stop function for even a short time and then starting it again will end with corrupted data.</p>
  <p>Any idea how to solve this?</p>
  <p>EDIT: So it seems that there is only one instance of function at one time - I used <a href="https://docs.microsoft.com/en-us/azure/architecture/serverless/event-hubs-functions/observability#example-application-insights-queries" rel="nofollow noreferrer">Executing Instances and Allocated Instances timechart</a> to check it, and i've got 1 all time.</p>
  <p>But my logs look like this:</p>
  <pre><code>2021-10-27T13:51:50.222 [Information] Json sent sent: MN5 2 10/27/2021 3:50:14 PM
  2021-10-27T13:51:50.222 [Information] {OutputJson}
  2021-10-27T13:51:50.223 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.229 [Information] Executed 'MessageEventFunction' (Succeeded, Id=ef405154-d600-4a54-9e1b-cc9cc7638873, Duration=96ms)
  2021-10-27T13:51:50.230 [Information] Executed 'MessageEventFunction' (Succeeded, Id=ff2d5226-91bf-4a6e-a3f8-9660cac1bdf6, Duration=99ms)
  2021-10-27T13:51:50.237 [Information] Executed 'MessageEventFunction' (Succeeded, Id=32d30205-b689-40b8-98d0-185318af8000, Duration=103ms)
  2021-10-27T13:51:50.238 [Information] Executed 'MessageEventFunction' (Succeeded, Id=238d7703-a352-487a-9a7e-1dff252d6dc9, Duration=103ms)
  2021-10-27T13:51:50.268 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=06e4101d-9cc6-42cc-b3e9-fe4873dabff9)
  2021-10-27T13:51:50.268 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.271 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=5f9b3b11-d756-47d4-9cb2-8ad9993cdb8a)
  2021-10-27T13:51:50.271 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.271 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=8ab7d257-0280-4bbd-afb7-1c8197a43d86)
  2021-10-27T13:51:50.272 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.272 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=cb0b6989-ba36-4be2-9ab4-e848aad341c8)
  2021-10-27T13:51:50.272 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.273 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=00821eeb-762f-4f5e-becd-f69a4514734c)
  2021-10-27T13:51:50.273 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.274 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=7b158eff-2c59-4983-93c0-64fb33a48885)
  2021-10-27T13:51:50.274 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.275 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=1c8688de-e954-466c-95cb-4fcc5a9234ea)
  2021-10-27T13:51:50.275 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.275 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=6c5dbac1-e063-43c6-98c5-c898dc971171)
  2021-10-27T13:51:50.276 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.276 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=d265f8e5-c7a6-480f-b71b-9450deec7fac)
  2021-10-27T13:51:50.277 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.277 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=9b623293-d5bb-4992-b782-dae9f3e3d5bb)
  2021-10-27T13:51:50.278 [Information] Trigger Details: PartionId: 3, Offset: 47273311592, EnqueueTimeUtc: 2021-10-27T13:51:21.8510000Z, SequenceNumber: 220220, Count: 10
  2021-10-27T13:51:50.278 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.278 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.278 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.279 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.279 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.280 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.280 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.280 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.280 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.281 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:50.314 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.322 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.332 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.341 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.348 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.357 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.381 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.390 [Information] Executed 'MessageEventFunction' (Succeeded, Id=1c8688de-e954-466c-95cb-4fcc5a9234ea, Duration=115ms)
  2021-10-27T13:51:50.391 [Information] Executed 'MessageEventFunction' (Succeeded, Id=d265f8e5-c7a6-480f-b71b-9450deec7fac, Duration=115ms)
  2021-10-27T13:51:50.392 [Information] Executed 'MessageEventFunction' (Succeeded, Id=9b623293-d5bb-4992-b782-dae9f3e3d5bb, Duration=114ms)
  2021-10-27T13:51:50.392 [Information] Executed 'MessageEventFunction' (Succeeded, Id=5f9b3b11-d756-47d4-9cb2-8ad9993cdb8a, Duration=122ms)
  2021-10-27T13:51:50.393 [Information] Executed 'MessageEventFunction' (Succeeded, Id=cb0b6989-ba36-4be2-9ab4-e848aad341c8, Duration=120ms)
  2021-10-27T13:51:50.393 [Information] Json sent: Rich 2 10/27/2021 3:50:24 PM
  2021-10-27T13:51:50.394 [Information] {OutputJson}
  2021-10-27T13:51:50.394 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.399 [Information] Json sent sent: P4 2 10/27/2021 3:50:34 PM
  2021-10-27T13:51:50.399 [Information] {OutputJson}
  2021-10-27T13:51:50.400 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.401 [Information] Executed 'MessageEventFunction' (Succeeded, Id=00821eeb-762f-4f5e-becd-f69a4514734c, Duration=127ms)
  2021-10-27T13:51:50.401 [Information] Executed 'MessageEventFunction' (Succeeded, Id=6c5dbac1-e063-43c6-98c5-c898dc971171, Duration=126ms)
  2021-10-27T13:51:50.402 [Information] Json sent sent: MN7 2 10/27/2021 3:50:54 PM
  2021-10-27T13:51:50.402 [Information] {OutputJson}
  2021-10-27T13:51:50.402 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:50.412 [Information] Executed 'MessageEventFunction' (Succeeded, Id=06e4101d-9cc6-42cc-b3e9-fe4873dabff9, Duration=143ms)
  2021-10-27T13:51:50.414 [Information] Executed 'MessageEventFunction' (Succeeded, Id=8ab7d257-0280-4bbd-afb7-1c8197a43d86, Duration=142ms)
  2021-10-27T13:51:50.425 [Information] Executed 'MessageEventFunction' (Succeeded, Id=7b158eff-2c59-4983-93c0-64fb33a48885, Duration=151ms)
  2021-10-27T13:51:51.895 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=726a0425-6959-4f3f-97a4-f722ee78d380)
  2021-10-27T13:51:51.896 [Information] Trigger Details: PartionId: 3, Offset: 47273322136, EnqueueTimeUtc: 2021-10-27T13:51:51.8620000Z, SequenceNumber: 220230, Count: 1
  2021-10-27T13:51:51.896 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:51.903 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:51.919 [Information] Executed 'MessageEventFunction' (Succeeded, Id=726a0425-6959-4f3f-97a4-f722ee78d380, Duration=24ms)
  2021-10-27T13:51:54.968 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=25c647aa-44a3-43f1-b8bf-02ddaae03057)
  2021-10-27T13:51:54.968 [Information] Trigger Details: PartionId: 3, Offset: 47273322576, EnqueueTimeUtc: 2021-10-27T13:51:54.9410000Z, SequenceNumber: 220231, Count: 1
  2021-10-27T13:51:54.969 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:54.997 [Information] JsonSent sent: P4 2 10/27/2021 3:51:39 PM
  2021-10-27T13:51:54.997 [Information] {OutputJson}
  2021-10-27T13:51:54.997 [Information] Activity: CacheIn: 21, CacheOut: 21
  </code></pre>
  <p>So sometimes function work ok, when SequenceNumber is unique and Count = 1. Other time not. Meyby the problem is with something else then instances od FunctionApp?</p>
  <p><code>Activity: CacheIn: 21, CacheOut: 21</code> means thah list of 21 object has been read from and then written to Redis cache</p>
  <p>So it should look like that every time (sending OutputJson is optional, depending on Cache and InputJson):</p>
  <pre><code>2021-10-27T13:51:54.968 [Information] Executing 'MessageEventFunction' (Reason='(null)', Id=25c647aa-44a3-43f1-b8bf-02ddaae03057)
  2021-10-27T13:51:54.968 [Information] Trigger Details: PartionId: 3, Offset: 47273322576, EnqueueTimeUtc: 2021-10-27T13:51:54.9410000Z, SequenceNumber: 220231, Count: 1
  2021-10-27T13:51:54.969 [Information] IoT Hub trigger function processed a message: {InputJson}
  2021-10-27T13:51:54.997 [Information] JsonSent sent: P4 2 10/27/2021 3:51:39 PM
  2021-10-27T13:51:54.997 [Information] {OutputJson}
  2021-10-27T13:51:54.997 [Information] Activity: CacheIn: 21, CacheOut: 21
  2021-10-27T13:51:55.017 [Information] Executed 'MessageEventFunction' (Succeeded, Id=25c647aa-44a3-43f1-b8bf-02ddaae03057, Duration=49ms)
  </code></pre>
  <p>And the problem is where function is stopped for a while and then we're turning it on or when few messages are ariving in almost same time.</p>