Serverless database computing with Azure Cosmos DB and Azure Functions (original) (raw)

APPLIES TO: NoSQL

Serverless computing is all about the ability to focus on individual pieces of logic that are repeatable and stateless. These pieces require no infrastructure management and they consume resources only for the seconds, or milliseconds, they run for. At the core of the serverless computing movement are functions, which are made available in the Azure ecosystem by Azure Functions. To learn about other serverless execution environments in Azure see serverless in Azure page.

With the native integration between Azure Cosmos DB and Azure Functions, you can create database triggers, input bindings, and output bindings directly from your Azure Cosmos DB account. Using Azure Functions and Azure Cosmos DB, you can create and deploy event-driven serverless apps with low-latency access to rich data for a global user base.

Azure Functions can create isolated database connections per invocation if the database client is not properly managed, which may impact connection performance under high concurrency, particularly in the Consumption plan. However, when using Cosmos DB bindings or triggers, Azure Functions internally reuses a shared Cosmos DB SDK client, ensuring efficient connection pooling across multiple invocations. Azure Cosmos DB supports both an HTTP-based and a TCP-based connectivity modes. This architecture supports scalable and high-performance workloads that require dynamic data access.

Overview

Azure Cosmos DB and Azure Functions enable you to integrate your databases and serverless apps in the following ways:

Note

Currently, Azure Functions trigger, input bindings, and output bindings for Azure Cosmos DB are supported for use with the API for NoSQL only. For all other Azure Cosmos DB APIs, you should access the database from your function by using the static client for your API.

The following diagram illustrates each of these three integrations:

How Azure Cosmos DB and Azure Functions integrate

The Azure Functions trigger, input binding, and output binding for Azure Cosmos DB can be used in the following combinations:

Use cases

The following use cases demonstrate a few ways you can make the most of your Azure Cosmos DB data - by connecting your data to event-driven Azure Functions.

IoT use case - Azure Functions trigger and output binding for Azure Cosmos DB

In IoT implementations, you can invoke a function when the check engine light is displayed in a connected car.

Implementation: Use an Azure Functions trigger and output binding for Azure Cosmos DB

  1. An Azure Functions trigger for Azure Cosmos DB is used to trigger events related to car alerts, such as the check engine light coming on in a connected car.
  2. When the check engine light comes, the sensor data is sent to Azure Cosmos DB.
  3. Azure Cosmos DB creates or updates new sensor data documents, then those changes are streamed to the Azure Functions trigger for Azure Cosmos DB.
  4. The trigger is invoked on every data-change to the sensor data collection, as all changes are streamed via the change feed.
  5. A threshold condition is used in the function to send the sensor data to the warranty department.
  6. If the temperature is also over a certain value, an alert is also sent to the owner.
  7. The output binding on the function updates the car record in another Azure Cosmos DB container to store information about the check engine event.

The following image shows the code written in the Azure portal for this trigger.

Create an Azure Functions trigger for Azure Cosmos DB in the Azure portal

Financial use case - Timer trigger and input binding

In financial implementations, you can invoke a function when a bank account balance falls under a certain amount.

Implementation: A timer trigger with an Azure Cosmos DB input binding

  1. Using a timer trigger, you can retrieve the bank account balance information stored in an Azure Cosmos DB container at timed intervals using an input binding.
  2. If the balance is below the low balance threshold set by the user, then follow up with an action from the Azure Function.
  3. The output binding can be a SendGrid integration that sends an email from a service account to the email addresses identified for each of the low balance accounts.

The following images show the code in the Azure portal for this scenario.

Index.js file for a Timer trigger for a financial scenario

Run.csx file for a Timer trigger for a financial scenario

Gaming use case - Azure Functions trigger and output binding for Azure Cosmos DB

In gaming, when a new user is created you can search for other users who might know them by using the Azure Cosmos DB for Gremlin. You can then write the results to an Azure Cosmos DB or SQL database for easy retrieval.

Implementation: Use an Azure Functions trigger and output binding for Azure Cosmos DB

  1. Using an Azure Cosmos DB graph database to store all users, you can create a new function with an Azure Functions trigger for Azure Cosmos DB.
  2. Whenever a new user is inserted, the function is invoked, and then the result is stored using an output binding.
  3. The function queries the graph database to search for all the users that are directly related to the new user and returns that dataset to the function.
  4. This data is then stored in Azure Cosmos DB, which can then be easily retrieved by any front-end application that shows the new user their connected friends.

Retail use case - Multiple functions

In retail implementations, when a user adds an item to their basket you now have the flexibility to create and invoke functions for optional business pipeline components.

Implementation: Multiple Azure Functions triggers for Azure Cosmos DB listening to one container

  1. You can create multiple Azure Functions by adding Azure Functions triggers for Azure Cosmos DB to each - all of which listen to the same change feed of shopping cart data. When multiple functions listen to the same change feed, a new lease collection is required for each function. For more information about lease collections, see Understanding the Change Feed Processor library.
  2. Whenever a new item is added to a users shopping cart, each function is independently invoked by the change feed from the shopping cart container.
    • One function may use the contents of the current basket to change the display of other items the user might be interested in.
    • Another function may update inventory totals.
    • Another function may send customer information for certain products to the marketing department, who sends them a promotional mailer.
      Any department can create an Azure Functions for Azure Cosmos DB by listening to the change feed, and be sure they won't delay critical order processing events in the process.

In all of these use cases, because the function has decoupled the app itself, you don't need to spin up new app instances all the time. Instead, Azure Functions spins up individual functions to complete discrete processes as needed.

Native integration between Azure Cosmos DB and Azure Functions is available in the Azure portal and in Visual Studio.

Why choose Azure Functions integration for serverless computing?

Azure Functions provides the ability to create scalable units of work, or concise pieces of logic that can be run on demand, without provisioning or managing infrastructure. By using Azure Functions, you don't have to create a full-blown app to respond to changes in your Azure Cosmos DB database, you can create small reusable functions for specific tasks. In addition, you can also use Azure Cosmos DB data as the input or output to an Azure Function in response to event such as an HTTP requests or a timed trigger.

Azure Cosmos DB is the recommended database for your serverless computing architecture for the following reasons:

If you're looking to integrate with Azure Functions to store data and don't need deep indexing or if you need to store attachments and media files, the Azure Blob Storage trigger may be a better option.

Benefits of Azure Functions:

If you're not sure whether Flow, Logic Apps, Azure Functions, or WebJobs are best for your implementation, see Choose between Flow, Logic Apps, Functions, and WebJobs.

Next steps

Now let's connect Azure Cosmos DB and Azure Functions for real: