alaa_sayegh
alaa_sayegh

Reputation: 2211

Static code shared between multiple azure funtions?

Lets say I have a function app that contains 5 azure functions. In visual studio I have these functions as classes but they use some static helper methods (to avoid code duplicate). In these helper methods I have kind of lockers to make sure that a specific order gets executed in each function. Now my question is, do all these 5 functions share the same state of these helper static methods? Or does each function run in an isolated context? I want the functions to run in parallel, so i don't want them to get blocked by each other.

The azure functions are not statis in my case, like this:

 [FunctionName("Function1")]
 public async Task Run([BlobTrigger("sample/{name}", Connection = "ConnectinoString")] Stream myBlob, string name, ILogger logger)

Upvotes: 2

Views: 1214

Answers (3)

MXStudios
MXStudios

Reputation: 1

Function Apps scale, not Functions

The unit of scale for Azure Functions is the function app. When the function app is scaled out, additional resources are allocated to run multiple instances of the Azure Functions host. Conversely, as compute demand is reduced, the scale controller removes function host instances.

Upvotes: 0

Simon
Simon

Reputation: 407

Azure functions from the same application run in the same process, just on different threads. So, the answer is the same as if you write the classic multi-threaded application. The only difference is that this process can be restarted (when you don't use function app actively the process is terminated and started again when someone calls one of the functions in app).

If you keep your functions within the same app they access the same static objects. The answer to whether it is safe to call them in parallel from different Azure functions depends really on the code. For example let's assume that your helper function looks like this:

public static int AddTwoNumbers(int a, int b)
{ return a+b; }

It is perfectly safe to be called at the same time from all of your 5 functions. On the other hand if your helper function looks like this:

public static int TransferMoney(int amount)
{
    CallBankAPI("wiretransfer", amount);
    return CallBankAPI("checkbalance");
}

You can not call this in parallel as the results will be unpredictable - there is a race condition here, so you would need to write it like this:

public static int TransferMoney(int amount)
{
    lock(bankAPIlock) {
       CallBankAPI("wiretransfer", amount);
       return CallBankAPI("checkbalance");
    }
}

So without knowing what are your helper functions doing it is hard to tell whether they need lock or not.

Upvotes: 1

Matteo Molino
Matteo Molino

Reputation: 16

When you work with azure functions you have to assume that functions could run in different servers so the static class is not shared. But using azure functions is the right choice to run operations in parallel. I don’t know enough about your use case but you can Try different approaches. The First way is connecting each function To The next One with queues (storage or service bus) Chaining the operation in the correct order. The Second way (maybe the best one) is using durable functions (https://learn.microsoft.com/it-it/azure/azure-functions/durable/durable-functions-overview?tabs=csharp) that allow you to manage the Execution flow by instance context.

Upvotes: 0

Related Questions