Azure How Many Functions In One Function App

Azure How Many Functions In One Function App

In this Azure tutorial, we will discuss Azure How Many Functions In One Function App. Along with this, we will also discuss a few other topics like Azure Function App Multiple Functions, Multiple Azure Functions In One Class, Azure Functions Best Practices and we will also discuss Azure Function Parallel Execution, Long Running Azure Function.

Azure how many functions in one function app? One Azure Function App can scales out to at max 200 instances as per Microsoft.

Azure How Many Functions In One Function App

Well, here we will discuss how many Azure Functions in One Azure Function App. So As per Microsoft By default, The Azure Function App that is under the Consumption plan will scale out to as many as 200 instances.

Same way, If the Azure Function App is under the Premium plan, then, the Azure Function App can scales out a maximum of 100 instances as per Microsoft.

Again, A single instance inside the Azure Function app can process more than one request, there is no limit set as of now on the number of concurrent executions.

You can set the functionAppScaleLimit property value to set the minimum limit value. You can set the property value to 0 or null to tell it is unrestricted.

You can also specify the functionAppScaleLimit value in between 1 to the max limit as per your Azure Plan like 200 for the Consumption plan and 100 for the Premium plan.

Azure Function App Multiple Functions

In the Azure Function world, An Azure Function App can contain multiple individual Azure Functions. If you will go a little in-depth, Azure Function App provides the execution context for all the individual Azure Functions that exist inside the Azure Function App. In that execution context, actually your Individual function runs.

Note: One important point to note down here is, All the Individual functions inside the Azure Function App must be developed with the same language.

The behavior of the Azure Function App is applied to each individual Azure Functions that are present inside the Azure Function App. Another important thing to note down here is All the individual functions inside an Azure function App share the same resources, per instance as the function App scales. Each function inside the Azure function App is deployed and scaled together.

One more thing to keep in mind is all the functions inside the Function App share the same pricing plan, runtime version, and deployment method, etc. For individual functions, you can define their separate connection string and Application settings for each one.

Multiple Azure Functions In One Class

Yes, You can keep multiple Azure Functions in one class. For example, You can keep the Azure Functions like below inside one class.

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace MyFunctionApp
{
    public static class Vehicle
    {
        [FunctionName("FunctionAUDI")]
        public static async Task<IActionResult> AUDI(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("FunctionAUDI processed a request.");
            //Add the Business Logic
            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This FunctionAUDI executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This FunctionAUDI executed successfully.";

            return new OkObjectResult(responseMessage);
        }

        [FunctionName("FunctionBMW")]
        public static async Task<IActionResult> BMW(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("FunctionBMW processed a request.");
            //Add the Business Logic

            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This FunctionBMW executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This FunctionBMW executed successfully.";

            return new OkObjectResult(responseMessage);
        }
        [FunctionName("FunctionSkoda")]
        public static async Task<IActionResult> Skoda(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("FunctionSkoda processed a request.");

            //Add the Business Logic
            string name = req.Query["name"];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            dynamic data = JsonConvert.DeserializeObject(requestBody);
            name = name ?? data?.name;

            string responseMessage = string.IsNullOrEmpty(name)
                ? "This FunctionSkoda executed successfully. Pass a name in the query string or in the request body for a personalized response."
                : $"Hello, {name}. This FunctionSkoda executed successfully.";

            return new OkObjectResult(responseMessage);
        }
    }
}

You can keep multiple Azure Functions inside one class like above but it is recommended to create the separate Azure Function for each case instead. You might be thinking now what if I have some common code to use across all the Functions, In that case, what you can do is, You can create a common class and then inject that class in all the Azure Functions.

One more benefit of creating separate Azure Functions is you can enable/disable/delete the Azure Function whenever you will need and will not affect the Other one So the risk factor will be less in the case of the Production environment.

In case of any critical issues, it will be easier to analyze the logs for the individual Azure Functions. It will also help to monitor individual Azure Functions.

Another very important factor is, When there will be independent Azure Functions, It will be really helpful in terms of Maintenance activity. If you will have to make any code change, It will not affect the other functions. But when you are keeping all the Functions inside the same Class file then there is a chance that the code change might affect the other functions as well. Especially, in the case of the Production deployment, you should be careful.

Azure Functions Best Practices

Well, here we will discuss Azure Functions Best Practices. There are a few suggestions, you need to concentrate while working with the Azure Functions. Suggestion can be in terms of performance, way of execution, Security, Coding standards need to be followed, Overall Architecture of your Azure Functions, etc.

Build Short Executions Azure Functions

  • This is very important in terms of performance, Always try to create or design a function that takes less time to execute. So try avoiding the long-running functions.
  • The reason behind this is, we know that there is built-in time out for the Azure Function. In the case of long-running applications, there is a chance that it will cross the default time out for the Azure Function, In that case, the Azure Function will throw the time out exception, and you will not get the proper output for your Azure Function.
  • One more risk factor with the long-running Azure Functions is, Since the Azure Function will be executing for such a long time, there is a chance that in between, you might face some network errors or service failures, etc because of the long execution time.
  • So it is always suggested to make your Azure function clean, simple, and short.

Implement Dependency Injection in Your Azure Function Project

As we all know that Dependency Injection is a very good design pattern that you can implement in your Azure Function project. With the help of the Dependency Injection, you can Reduce the dependencies, Your Azure Function code will become more readable, reusable, etc.

Better to use a separate Storage account for each Azure Function

  • While creating an Azure Function, we need a storage account. So the suggestion here is always try to use a separate storage account for each Azure Function. Do not use the same storage account for multiple Azure Functions.
  • If you will use a separate Azure Storage account for the individual Azure Function, then definitely, the performance of your Azure Function will increase simultaneously.
  • With this, you can able to separate your business logic from each other that actually helps you in terms of less execution time and more Performance.

Build Asynchronous Azure Functions

Always try to build your Azure Function with Asynchronous calls that actually helps you to avoid any blocking calls. Avoid using the .wait() method and . Result property if possible.

Implement Logs in side Azure Function

Try to implement logging for the Exceptions or failures where ever possible. I am not talking here to log the information more. Rather, what I am trying to tell here is, use the logs for capturing the exceptions where ever possible that really helps you in case of any critical exception that might occur while executing the Azure Function.

Azure Function Should be Stateless

If Possible, Build your Azure Functions to be stateless. If you will have to add state information, you can add your state information with your data, But make sure to keep the Azure Function remains stateless.

Better To Write Defensive Azure Functions

It is always suggested to write a defensive Azure Function while you are building your Azure Function or Developing your Azure Function. When you are telling defensive, meaning, You should write your Azure Function code in such a way that in case of any exception during the execution, it should be capable enough to continue from a previous fail point during the next successful execution.

The exception might be due to the quota limit reached, network-related issues, etc. So your Azure Function should be able to handle this type of scenario. You need to make your Azure Function capable enough to handle these situations.

I know it might look bit confusing, let’s consider the scenario where you need to insert a set of 500 records to a table with the help of your Azure Function. After inserting maybe 120 items, you got an exception due to a network issue and the Function execution stopped that time.

So, in this scenario, Ideally, your Azure Function should keep these 120 items in a separate queue and you should capable to track the 120 items rather inserting them again from the beginning. You should only insert your remaining 80 items.

Reuse External Resource Connections

Whenever you are working with Azure Functions there is a chance that you will have many connections to the external resources from your Azure Function. Keep this in mind that this is a good practice to reuse the connection to the external resources where ever possible.

Azure Functions Security Best Practices

As we know that security is an important factor to safeguard your Azure Function. Check out the Azure Functions Security Best Practices now.

Concurrency of Azure Function to Be considered

Concurrency needs to be considered while you are working with the Azure Function to distribute the Workload whenever required to avoid running out of resource situations. This is one of the great options to manage the resources.

You can able to configure this using the “maxConcurrentRequests” property in the host.Json file. One more thing to consider here is, When you are setting this Concurrency configuration, This applies to all the Azure Functions inside that particular Azure Function App.

To make you little clear on this, For example, Say, You have configured 30 Concurrent requests for your Azure Function App and you scale out your Azure Function App to 3 instances then you will get all total 3 * 30 = 90 concurrent requests.

So, These are few key best practices you need to keep in mind while working with the Azure Functions.

Azure Function Parallel Execution

In the Scenario, when more than one triggering event occurs faster than a single-threaded function, The runtime may invoke the Azure Function more than one time in parallel. The number of concurrent function invocations depends on the type of triggered being used inside the Azure Function App.

Long Running Azure Function

Long-Running functions are the functions that are running more than the defined time out limit. The problem with the Long-Running function is once it crosses the maximum time out duration, it can stop at any point in time and you will end up with not getting any output. So because of this time out limit, the Azure Function needs to be short and you should avoid writing the long-running Azure Function.

The maximum time out for the Azure Function is 5 minutes when your Azure Function is under the Consumption plan. Though you can able to extend it to 10 minutes max still it won’t be enough for the long-running Azure Function and your Azure function can stop at any point in time once it reached the max 10 minutes.

So to help out with these scenarios, the Function Chaining concept comes into the picture where you need to break up the long-running tasks or processes to smaller units that can be chained together. This is the process of executing a set of functions in a particular order. Here, the output of one Azure Function will be the input for the next Azure Function.

You May also like following the below articles

Wrapping Up

Well, in this article, we discussed Azure How Many Functions In One Function App, Azure Function App Multiple Functions, Multiple Azure Functions In One Class, Azure Functions Best Practices and we also discussed Azure Function Parallel Execution, Long Running Azure Function. Hope You have enjoyed this article !!!