basvo / serverlessazurefriday

Serverless geo-replicated event-based architecture sample for Azure Friday

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless geo-distributed event-based architecture with Azure Cosmos DB and Azure Functions

Serverless geo-distributed event-based architecture sample

Video

This repository was part of an Azure Friday episode, you can view it below:

Watch the Azure Friday episode

This repository contains a sample architecture that can be deployed across multiple regions and leverage Azure Cosmos DB's Multi master and Change feed features to achieve:

  • Local writes on all Azure Function regions
  • Real-time notifications provided by the Change feed
  • Real-time data migration across Cosmos containers.

Local writes

Multi Master enables all Azure Cosmos account regions to be read and write enabled. If you deploy an Azure Function App like the one in the DistributedAPI project, you will notice that the HTTP Trigger Functions are using the PreferredLocations and UseMultipleWriteLocations attributes in the Cosmos DB Output and Input bindings. The first one can define a list of comma-separated region names you want the binding to connect to preferably, and the latter tells the binding that we want to take advantage of the Multi Master feature.

You can see that in both bindings, we are using a placeholder as value as PreferredLocations. That means that we can set this value on an environment variable and the code will pick it up automatically, which also enables us to deploy the same code in multiple regions and make it work locally.

Real-time notifications

The repository contains a sample of how to use the Cosmos DB Trigger and the SignalR Output Binding. This mix lets the Azure Function wake up whenever there are new changes happening in the Azure Cosmos container's Change Feed and send them to Azure SignalR, which will use WebSocket connections to push these events to any connected clients.

Real-time data migration

Finally, mixing the Cosmos DB Trigger with the Cosmos DB Output Binding, allows for online data migration between two Azure Cosmos containers; whenever there are new events on one container, they will be copied to another one, which might even have a different partition key. On this case, it is important the use of LeaseCollectionPrefix on the Trigger, which will enable multiple triggers to share the same leases container and work in parallel.

References

About

Serverless geo-replicated event-based architecture sample for Azure Friday

License:MIT License


Languages

Language:JavaScript 97.9%Language:C# 1.2%Language:CSS 0.7%Language:HTML 0.2%