Skip to main content

Message Queue + Azure Functions + CosmosDB. A perfect trio !!!

Ok !! It's not about SEX, it's about something they name AZURE.

Seriously, I'll asume in this short article, that you have some kind of experience with azure.

Suppose you have a heavy load. Incoming data come from some entity(application) that send, with high frequency, messages to some queue in Azure. Also you need analyze incoming data and apply some logic before insert data in your persistence layer.

It's a complicated task ?

Actually it's a simple task !!!

Step  1. Create storage account. 





Step 2. Create Queue



Step 3.  CosmosDB Resource

Let's asume we already have Data Base and Collection "READY" in CosmosDB


Step 4. Create Azure Function Application




Step 5 Create Azure Function. 

In this step we will see how to bind Message Queue and  CosmosDB collection inside Azure Function.

We will use template for Queue Trigger.



Define name and input bind. Remember this Azure function will listen a queue for message incoming.
Select storage account to create connection. Just click on new link to select from all storage accounts available in our Azure Subscription.






By default, we have this code. 


Step 6.  OUTPUT Binding 

Now we need create OUTPUT binding. In other words, we need create connection to CosmosDB  to insert incoming data after analyze it.


Defining Data Base , collection, connection string and partition key in case you need it.


Now magic is coming !!!


Step 7.  Coding



Done ?
Yes !!! I'll try to resume .

I need to decouple a process. Then we use the queue capability to support high load. Many entities(applications), send simultaneously messages to a queue. There is a Azure Function listening message incoming , analyze it , apply logic and insert into cosmos db collection.

Step 8. Operation results

a.- Simulating an entity that sends a message to the queue.

You can use Microsoft Azure Storage Explorer



b.- Azure function log 


c.- Query in CosmosDB collection 




Bye !!

Comments

Popular posts from this blog

Add more security to an Azure CosmosDB Account.

W e can access programmatically to Azure Cosmos DB resources to create, query, and delete databases, document collections, and documents via REST API. To perform operations on Azure Cosmos DB resources, you can send HTTPS requests with a selected method: GET, POST, PUT, or DELETE to an CosmosDB endpoint.  Frequently we build architecture with centralized access to a persistence layer, then we won't let other applications access directly to this layer. Suppose we have the following hypothetical scenario (see Figure 1): Figure 1. Hypothetical Scenario Only WEB API Service with IP1 is allowed to establish connections with CosmosDB EndPoint. The others elements showed as Application are not allowed. By default we have in our CosmosDB resource the following configuration (see Figure 2) in Firewall and Virtual Networks option. Figure 2. Default Configuration To show you how this feature works, we are going to create a WEP API with a single function (r

Querying CosmosDB. Part 1

Hi CosmosDB Community !!! I'd like to contribute with some posts about how to query Cosmos DB Collection with SQL API. Suppose we have a speaker list of differents events around a whole world. The link above represent speaker list example. Example1.json For all examples we will use Data Explore in Azure Portal. Anyway, you can use any API in any supported programming language to write your queries. We are not using Modeling data techniques or witting same data in different ways to avoid cross partition queries. The main objective of the post is how to use built-in functions and operators in CosmosDB SQL API. I'd like to list all events from Hassan Arteaga Rodríguez  Query SELECT * FROM c where c.speaker = "Hassan Arteaga Rodriguez" Results I need to list all events from MEXICO. Not all fields Query SELECT c.speaker, c.event_name,c.init_date,c.end_date FROM c where c.event_country = "MEXICO" Results

Some architectures with Cosmos DB and Azure Functions

Fig 1. Access Control Fig 2. Documents storage with reference to blob. Fig 3. Automatic storage maintenance of Documents with expiration. Fig 4. Reports.