Azure Storage Queues and Azure Functions Queue Triggers
Chains of HTTP calls should be avoided when running software in the cloud.
While on-premises HTTP calls are expected to travel a relatively short distance, on the cloud HTTP calls may have to travel long distances as datacentres distribute over wide pieces of land. This could easily cause timeouts and performance bottlenecks very difficult to diagnose.
One of the possible solutions is using Azure Queues, that are part of Azure Storage. If you have many messages, definitely Azure Queue will be a good option.
From here:
Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world via authenticated calls using HTTP or HTTPS. A single queue message can be up to 64 KB in size, and a queue can contain millions of messages, up to the total capacity limit of a storage account.
Create a Storage Queue Client
Install Client Side Libraries
I will create a simple .NET Core application. I will add bootstrap 4 by using LibMan. This can be done by right-clicking the project, click on “Add” -> “Add Client-Side Library”
I have then followed partially the tutorial that can be found here:
And here:
I will use the same storage account that I used when creating my first Azure Function, detailed in here:
Integrate with an Azure Storage Queue
I will need to take note of my Azure Storage Connection String. This can be found under the “Access Keys” tab in the portal:
I will install “Microsoft.Azure.Storage.Queue” package:
I will add Cloud Storage with Azure Storage as a Connected Service:
I will add a name for the storage:
The tool will prompt if we want to store the connection string in our secret.json file:
I had a look at this topic here:
As a result, the “appsettings.json” file gets populated:
And the “secret.json” file gets populated as well:
Create a Repository
And here is the code of my repository:
Create a Page
Here is the razor code of the page that will capture the message to be sent into the queue:
And its page model:
And here is the result:
Send some messages
The message will stay in the queue until it will be read or for up to 7 days.
Create a Queue Triggered Azure Function
I will add a new function and select “Queue Trigger” as a starting template. I can see I am asked with:
- Connection String Name
- Queue Name
Both the parameters will both be used in order to populate the “QueueTrigger” attribute. They will read from the “local.settings.json” file inside the project.
The function project already comes with its “local.settings.json” populated with the connection string of the Azure Storage Account that comes with it. This connection string is called “AzureWebJobsStorage”.
Once deployed, the result will be visible straight from the console of the Azure Function.
The queue will now be empty:
Conclusions
If your requirements are to have a queue that can store a lot of messages with a maximum size of 64K Azure Storage Queues are perfect.