question

AmarnathManchala-5911 avatar image
0 Votes"
AmarnathManchala-5911 asked AmarnathManchala-5911 commented

Azure function Startup class execution

Hi

We have created a Azure function with Startup.cs class.

in the startup class we want to load few configuration parameters from DB and static files and load to some variables.

in the Azure function we are going to use these variables.

I would like to understand th behavior of the Startup.cs class execution.

when app is instantiating startup class is executing and configuration values are being pulled.

Every time when we call Azure function this startup code will execute or it will execute only at the time of Azure app is instantiated?

and when the app is scaled out to new app service plans (we setup the scale out on CPU utilization) the startup class will execute again in the new app??

azure-functions
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

PramodValavala-MSFT avatar image
1 Vote"
PramodValavala-MSFT answered AmarnathManchala-5911 commented

@AmarnathManchala-5911 The startup class is executed at the host level when it first starts up. This is just run once per instance and as your function app is scaled out, its run on each instance while it starts up.

· 11
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Pramod,

Thanks for the update,
based on your response , My understanding is.. if Azure function app scaled to 5 instances then 5 times the Starup class will execute.

0 Votes 0 ·

@AmarnathManchala-5911 Yes. That is correct. Usually, you would just have initialization code here required for each instance. So, the execution itself is scoped to each instance.

0 Votes 0 ·

Hi Pramod,

i am little confused here

when you say each instance, do you mean that each server (App Service plan instance).?

0 Votes 0 ·

@AmarnathManchala-5911 Nope. An App Service Plan Resource is what defines the underlying compute to use. An instance is a server that is allocated to a plan. So, in the case of consumption, as the function app scales it out, it adds more servers/instances to run the same function app.

Think of them as servers/containers allocated to run the same app to scale out. Since they are completely separate, the app is essentially starting up again.

0 Votes 0 ·

Hi Pramod,

We are using the premium app service plan for Azure functions. (we set the maximum scale out to 1 instance for app service plan).

When Azure function app is scaled out to multiple instances the startup class will be executed multiple times?







0 Votes 0 ·

If max scale is set to 1, then your function app will not scale out.

0 Votes 0 ·

In my function app I have one function with Http trigger and i have Startup class also defined to load some configuration data from DB.

We are using the App Service plan model to host our Azure functions.

if we call the http function 50 times , all 50 times the startup class will get executed?









0 Votes 0 ·

No. Not necessarily unless that forces your function app to scale out to multiple instances, in which case startup class will get executed once for each instance (which may not necessarily be 50, depending on your scenario/settings)

0 Votes 0 ·

Hi Pramod,

I have verified the Startup.cs execution, I see Startup.cs is executing for every server (for each app service plan instance).
I have setup Auto scale of the App Services plan on CPU utilization, 3 servers created i see 3 times the Startup.cs class executed.

I called the Azure function more than 500 times but i see only 3 times the App Service plan has been executed. this is because of the App service plan scaled to 3 servers.

Please confirm my understanding is correct or not, In our project this process is crucial for now.

0 Votes 0 ·

Yes. You are correct. The startup class is executed only when the function app starts up within the instance it is running on. In your case, its just 3.

0 Votes 0 ·

Thank you So much.

0 Votes 0 ·