Ama
Ama

Reputation: 1565

Concurrency in Azure Function with .Net 5 (isolated)

With .Net 5, Azure Functions require the host to be instantiated via Program.cs

class Program
{

    static Task Main(string[] args)
    {
        var host = new HostBuilder()
            .ConfigureAppConfiguration(configurationBuilder =>
            {
                configurationBuilder.AddCommandLine(args);
            })
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(services =>
            {
                services.AddLogging();
            })
            .Build();

        return host.RunAsync();
    }

}

If I was to add some global variables in Program.cs (say static) so that they can be accessed by any of the endpoints in the Azure Function project, if the global variable value was changed during the execution of one of these endpoints, is there a chance (even small) that this update propagate into the execution of another endpoint executing just after? I struggle to understand to what extent the Host is concurrent.

These were useful readings, but I did not find what I was looking for:

Upvotes: 1

Views: 702

Answers (1)

Peter Bons
Peter Bons

Reputation: 29780

See Azure Functions as stateless workers. If you want them to have state either use Durable Entities or external storage. Implementations can change, even of the underlying Azure Functions runtime. Design accordingly is my advice.

Static global variables are often a bad idea. Especially in this case you can't reliably predict what will happen. The process can be killed, new instances can be brought up / taken down possibly spanning multiple machines due to dynamic scaling. So different executions can see different values of the static variable.

Again, you should design your functions in such a way that you do not have to worry about the underlying mechanisms, otherwise you will have a very tight coupling between the code and the hosting environment.

Upvotes: 1

Related Questions