Skip to main content

Handling Concurrency in Azure Functions (HTTP Triggered)

Concurrency & Isolation

Concurrency is one of the most common requirements when we don't want the resource to be accessed by multiple requests at the same time.

Lets say we have a Database that holds products stock information, then it is very important that any read/write operation that happens on the Database must hold the property of Isolation.

The isolation property ensures that the concurrent execution of transactions results in a system state that would be obtained if transactions were executed sequentially, i.e., one after the other. Providing isolation is the main goal of concurrency control. Depending on the concurrency control method (i.e., if it uses strict – as opposed to relaxed – serializability), the effects of an incomplete transaction might not even be visible to another transaction.

Options Available in Azure Functions

Recently while working on Azure Functions, I discovered a really simple way of handling Concurrency via code. There are ways to control two important aspects of Concurrency in Azure Functions:

  1. Maximum number of Outstanding Requests

The maximum number of outstanding requests that are held at any given time. This limit includes requests that are queued but have not started executing, as well as any in progress executions. Any incoming requests over this limit are rejected with a 429 "Too Busy" response. 

  1. Maximum number of Concurrent Requests

The maximum number of http functions that will be executed in parallel. This allows you to control concurrency, which can help manage resource utilization. For example, you might have an http function that uses a lot of system resources (memory/cpu/sockets) such that it causes issues when concurrency is too high.

Steps 

One of the ways of doing this real quick is using using host.json file in Visual Studio 2017. Lets go over the steps to do so:
  1. Create a New Project in Visual Studio 2017
  2. Select Azure Functions in Project Template (If you are not getting Azure Functions in Project Templates, you need to install Cloud Components using VS Installer)

  1. Select HTTP Trigger in the popup

  1. VS will create a Functions Project for you, and add an Azure Functions File as well.

  1. We will be adding 2 Azure Functions in our Project - 
    1. UpdateStockLevel - will be used to update stock level in the Database
    2. GetStockLevel - will be used to retrieve current stock level from the Database



  1. Assuming that we have a SQL Server DB / Azure Cosmos DB, the above two Azure Functions will be using Stored Procedures / SDK Queries in case of Azure Cosmos DB to perform Update and Get Operations on the Stock Level.
  2. Now coming to to agenda that we've be discussing from the start of this blog - Concurrency.
  3. You might notice that there are two json files already added to the Project in Visual Studio - local.settings.json & host.json
  4. The general format of json file for HTTP triggered function is as below:

1
2
3
4
5
6
7
8
{
  "http": {
    "routePrefix": "api",
    "maxOutstandingRequests": 20,
    "maxConcurrentRequests": 1,
    "dynamicThrottlesEnabled": false
  }
}

  1. Now lets say you want to control Maximum number of Outstanding Requests. That means how many requests should be queued at a given point of time. Currently I have set it to 20, that means at any time, 20 requests will be queued waiting to be processed by the Azure Function.  This is controlled by maxOutstandingRequests key in host.json file.
NOTE - If you don't want to limit Maximum Outstanding Requests, you can set this to -1 that is the default value for this key. This means that the limit will be unbounded and there will be no limitation on how many requests should be outstanding at any given point of time.
Ideally this should be set to -1, unless there is any specific need to limit requests.
  1. Next we will look at Maximum number of Concurrent Requests. This controls how many requests should be executed in parallel by the Azure Function, hence allowing us to control Concurrency. I have set this to 1, since right now I don't want multiple parallel execution for the Azure Function. This is controlled by maxConcurrentRequests key in host.json file. 
NOTE - If you don't want to limit Maximum Concurrent Requests, you can set this to -1 that is the default value for this key. This means that the limit will be unbounded and there will be no limitation on how many requests should be executed in parallel at any given point of time.

References:

https://en.wikipedia.org/wiki/ACID
https://docs.microsoft.com/en-us/azure/azure-functions/functions-best-practices


Comments

  1. Quality Content. Keep doing the good work.

    ReplyDelete
  2. Thank you for spending time delivering this valuable information regarding azure cloud migration services. This concept is a good way to enhance the knowledge about azure migration.
    azure cloud migration services

    ReplyDelete

Post a Comment

Popular posts from this blog

Azure Functions - Retrieve data from Dynamics 365 CRM/CE Online using CRM OData Web API

Introduction: This blog describes how you can retrieve data from Dynamics 365 CRM/CE Online in an Azure Function, using CRM OData Web API. The CRM Web API has a couple of advantages over the traditional SOAP based approach to interact with CRM. These are rest based services, its easy to use and transport over http protocol , and there is no need to use additional web resources (XrmServiceToolkit ) of SDK (SDK.REST). Here is a detailed documentation on CRM OData Wbeb API. I will be using an Azure Function Application in Visual Studio 2017 to retrieve records from Dynamics 365 CRM/CE, by making use of OData API Endpoint provided by CRM. Prerequisites: Dynamics 365 CRM Online Trial https://trials.dynamics.com/ https://signup.microsoft.com/Signup?OfferId=bd569279-37f5-4f5c-99d0-425873bb9a4b&dl=DYN365_ENTERPRISE_PLAN1&Culture=en-us&Country=us&ali=1 Azure Subscription related to CRM Online instance https://portal.azure.com/ Visual Studio 2017 with Cl

Creating Azure Function from Visual Studio 2017 triggered from Azure Service Bus Topic

Recently, I was building an Azure Function from Visual Studio and encountered an issue with the auto generated Function.cs class file. My requirement was to create an Azure Function that would get triggered as soon as we receive a message on an Azure Service Bus Topic. For this I tried to use the Project template available in Visual Studio 2017. Unfortunately, when I tried to provide all the required values, and run the function, I got following error: First you get a warning which says: Warning: Cannot find value named 'Endpoint=sb:......' in local.settings.json that matches 'connection' property set on 'serviceBusTrigger' in '\Source\repos\FunctionApp1\MyFunction\bin\Debug\net461\Function1\function.json'. You can run 'func azure functionapp fetch-app-settings <functionAppName>' or specify a connection string in local.settings.json. This warning was followed by an error: [2/12/2018 9:48:05 AM] A ScriptHost error has occurre