Load balancing an Azure function API for high availability using Azure front door

Hey everyone.

I was just exploring of ways to load balance a http API hosted on Azure functions. Hopefully with the solution, this would provide high availability as well. What I’m talking about is can how can we setup our functions in 2 paired regions so that data is distributed amongst them? Also, if one region goes down, I would like all requests to be routed to the remaining function. Obviously this means I need 2 function apps setup in 2 regions. But I do need something else for the load balancing and health probes.

We will make use of Azure front door to do this. You can read more on Azure front door from the official documentation https://azure.microsoft.com/en-in/services/frontdoor/#:~:text=SSL%20offloading%20and%20app%20acceleration,your%20users%20and%20back%20ends.

For some more reading on load balancing you can read https://docs.microsoft.com/en-us/azure/frontdoor/front-door-lb-with-azure-app-delivery-suite

A high level diagram of what we are trying to achieve is shown below.

As a first step, lets create our basic function app. We will use the http triggered template.

Go to the Azure Home page and click on “Create Resource”.

Enter “function” in the search box and click on “Function app” in the drop down list.

Click on Create.

Select your resource group. Enter a function name (I’ve selected apiregion1).

Set the runtime stack to .NET. Select your region (I’ve selected North Europe).

Set the other options as shown below.

Click on “Review + Create” Once validation is complete, click “Create” and wait for the deployment to be completed.

*The function app will automatically be created on consumption plan which is the cheapest plan available.

Now go to your function app and click on “Functions” on the left side pane. Click on “+Add”. As a template click on “HTTP trigger”. Set the name to ‘myapi’ (this can be anything really) and set the Authorization level to “Anonymous”. Click Add.

Once your function is created,click on ‘Code + test’ on the left pane. You should see some default code. This will just return a response when you hit the API endpoint. Let’s change the code a bit to indicate that this response is coming from North Europe. After making the below code change, click on Save. Your function will recompile using the new code.

Next click on Overview on the left pane, and then click on “Get Function Url”. This will give you the url of your function. It will be something like https://apiregion1.azurewebsites.net/api/myapi?. Copy this and store it in your note pad for later use.

Now repeat the entire process and create another function app in another region. We will choose ‘West Europe’. Name your new function app ‘apiregion2’. For simplicity, name your new function the same as the first function i.e. .myapi’.

For this API, we will change the code to indicate that the response is coming from West Europe. When done, click save.

Remember to fetch the url of the api.

At this stage, we have 2 functions in 2 separate regions. Each host an API with the name ‘myapi’.

Now it is time to create our front door.

Go to the Azure Home page and click on “Create Resource”.

Enter “front door” in the search box and click on “Front door” in the drop down list and click ‘Create’. Enter your resource group and then click the Next tab.

In the configuration, click the ‘+’ on the fronends/domains. Then enter a host name, This can really be anything. It will be the 1 API endpoint that will be exposed to the outside world. Click on Add.

Next its time to add our backend pools.

Click the ‘+’ on the backend pools. Then enter a pool name(this can really be anything). Enter the path for your api. This comes from your API urls which we copied earlier on.

Then click on ‘+ Add a backend’.

For the backend, select ‘App service’ as your backend host type. Make sure your backend hostname and backend host header matches that of your 1st API. Click ‘Add’.

Now repeat this process and add another backend for your 2nd API.

Once this setup is done, it should like the below.

Note that the interval is 30s. This indicates how often a health probe is sent to your backends to check if they are functional. This can of course be changed.

Also note, that the weight is 50 for both backends. This means requests will be distributed equally amongst the 2.

Finally create a routing rule as shown below.

Finally, click on Review+Create and after successful validation, click on Create. It will take around 5 minutes for all the settings to come into effect so be patient.

When the deployment is complete, go to your resource and pick up the front end host name.

So the API which we will aim to hit is the front end host name + the api path. So in our case it will be


If we add a name parameter it will be : https://myazurefrondoor.azurefd.net/api/myapi?name=Leonardo

Now comes the time to test if this really works.

Go to your browser and enter the url+ parameter into the search window.

So with this first API request, we are hitting the west Europe region. If you try to keep refreshing the page, you may hit the North Europe region a few times. This is because we have used equal weights for each backend. This means, the requests will be distributed equally amongst the 2 back end.

Now let’s see what happens if we bring one region down.

Go to your first function app and click on ‘STOP’ to forcibly shut down the app.

If you try to immediately hit your API endpoint again, there are changes you will get a 403 message like below.

This is because front door is still routing some messages to the unhealthy backend. We have set the health probe frequency to 30s. So we will attempt to hit our end points after about 30s and see what we get.

After about 30s, if we try and hit the endpoint again, we get the response from the 2nd region (West Europe).

This means Azure Front door has now detected that the North Europe region is down and is now routing all requests to West Europe region.

Hopefully with this demo, you would understand that not only have we distributed the load among 2 function apps but we have also designed for high availability incase one function app comes down.

Drop me a message or comment if you have any questions. I will be happy to help.

IT Solution architect with an interest in cloud computing and anything geeky and techy.