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:
- 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.
- 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
- Create a New Project in Visual Studio 2017
- 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)
- Select HTTP Trigger in the popup
- VS will create a Functions Project for you, and add an Azure Functions File as well.
- We will be adding 2 Azure Functions in our Project -
- UpdateStockLevel - will be used to update stock level in the Database
- GetStockLevel - will be used to retrieve current stock level from the Database
- 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.
- Now coming to to agenda that we've be discussing from the start of this blog - Concurrency.
- You might notice that there are two json files already added to the Project in Visual Studio - local.settings.json & host.json
- 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 } } |
- 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.
Ideally this should be set to -1, unless there is any specific need to limit requests.
- 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.
References:
https://en.wikipedia.org/wiki/ACID
https://docs.microsoft.com/en-us/azure/azure-functions/functions-best-practices
Quality Content. Keep doing the good work.
ReplyDeleteThank 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.
ReplyDeleteazure cloud migration services