0

I'm having situation where i need one instance of a object shared between multiple instances of Azure Function. Reason for this is that object internally is saving some state that I need, it is external library so I need to use it, no workaround around it. Is this possible somehow with durable Azure functions? To have one function which would be triggered by queue trigger or http trigger and that function would have that singleton instance, and call regular azure functions? When would that "main" function be shut down then?

Cheers

Bida
  • 439
  • 1
  • 4
  • 13
  • can you not store it in table storage ???? – Thomas Oct 17 '18 at 08:20
  • can we store object instance in table storage? – Bida Oct 17 '18 at 08:22
  • @Thomas wouldn't it suffice to use the redis cache? – Fildor Oct 17 '18 at 08:27
  • For all intents and purposes, different azure functions are actually different *processes*. You can't share instances between them. You can send messages from one to another – Panagiotis Kanavos Oct 17 '18 at 08:35
  • That object would set state correctly at first execution, and then it should keep it and maintain in memory until all executions triggered are done, afterwards, when next execution is planned it can create new instance, set state and maintain it, is this possible with durable functions? – Bida Oct 17 '18 at 08:57
  • I feel redis is an overkill for just one object... Table storage works great and you have less than 10ms latency if your resource are in the same datacenter – Thomas Oct 17 '18 at 20:16

1 Answers1

2

It's impossible to share instances between functions. The functions are ran in different machines, they just can't get a reference to a memory location that is who knows where.

Your tagged azure-durable-function isn't really a solution you need either. It doesn't store real singletons forever, it just stores and restores what your function executed and what it got. The actual data in some singleton would be lost. And the way it stores the state is through table/blog storage.

So essentially, if you have shared state between functions(or even a state you need to keep in the same function), your only way is to use some kind of persistent storage you can depend on. So, blob storage, tables, redis, sql, whatever.

Erndob
  • 2,512
  • 20
  • 32
  • That object would set state correctly at first execution, and then it should keep it and maintain in memory until all executions triggered are done, afterwards, when next execution is planned it can create new instance, set state and maintain it, is this possible with durable functions? – Bida Oct 17 '18 at 08:47
  • It is possible. – Erndob Oct 17 '18 at 10:23
  • Ok, cool, thank you for answer, i just needed guidance to know in which direction to go – Bida Oct 17 '18 at 10:26
  • Would that mean using singleton orchestrator (https://learn.microsoft.com/en-us/azure/azure-functions/durable-functions-singletons) where I can start one orchestrator instance where i can maintain in memory state and pass parameters to other activity functions? – Bida Oct 17 '18 at 10:54
  • @Bida Did you solve issue? I thought about the similar, but I need to keep object instance between subsequent calls the same azure function timer. Is it possible? – gorrch May 23 '19 at 11:38
  • @gorrch Inherently serverless should be stateless. You can't rely on subsequent requests having access to the same object. It will have that access to that object if your request ended up in the same instance, but it might have ended up in another instance with another object. Even the article of the singleton tells you that it has a race condition inside. The more hacks you need to make serverless work for you, the more it's a sign that serverless is not a right option for your usecase. – Erndob May 23 '19 at 11:47
  • @Erndob I needed this instance because it contains queue, which is very comfortable because it contains only 3 records. I need only to push next record on queue each minute. If some condition will be met and count will be bigger than 3, then it release first, so count always is 3. It is really simple in c#. If I had to do it in azure table storage, it demands more work. – gorrch May 23 '19 at 12:18