Up until recently, software development has been revolving around the concept of a server. Provisioning, maintaining, and scaling applications has been something that developers needed to spend a lot of time and effort on.
We are now entering the era of serverless. With serverless architecture, there is a whole new range of applications emerging, in particular serverless event-driven apps. When the event is triggered, your application pops into action, does what it needs to do and disappears again. That’s why we start calling it “severless” because, in essence, your server does not exist until it is needed.
As many of you know, Azure Cosmos DB is the first globally distributed, massively scalable, multi-model database service. The service is designed to allow customers to elastically and horizontally scale both throughput and storage across any number of geographical regions, and it offers guaranteed <10 ms latencies at the 99th percentile, 99.99% high availability and five well defined consistency models to developers to make precise tradeoffs between performance, availability and consistency of data. Azure Cosmos DB is also the first globally distributed database service in the market today to offer comprehensive Service Level Agreements (SLAs) for throughput, latency, availability and consistency.
Today we are announcing the availability of native integration between Azure Cosmos DB and Azure Functions. With this native integration, 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.
Figure: Example of e-commerce serverless event-driven architecture
The beauty of this architecture is that
- It lets you break down your system into smaller and simpler pieces that are easier to manage
- It takes away the pain associated with running and maintaining servers to a point where developers can concentrate on their code instead of infrastructure.
- Simpler architecture is not only a lot more manageable, but is also a lot more economical to run. Each function can be invoked separately when required and then disappear again leaving a much smaller resource footprint.
- Another big advantage is that performance is enhanced due to code reusability.
- Azure Cosmos DB further powers the above architecture by making your massive and diverse data available close to where your users are, worldwide.
Azure Cosmos DB – the database for all serverless apps
Azure Cosmos DB is the database of the future - it is what we believe is the next big thing in the world of massively scalable databases! It is a globally distributed, multi-model database service for building planet scale apps with ease by using the API and data model of your choice. Azure Cosmos DB is the recommended database for all your serverless computing architectures for the following reasons:
- Instant access to all your data: You have fine- granular access to every value stored because Azure Cosmos DB automatically indexes all data by default, and makes those indexes immediately available. This means you are able to constinuously query, update, and add new items to your database and have instant access via Azure Functions.
- Schema-less. Azure Cosmos DB is schema-less, so it's uniquely able to handle any data output from an Azure Function. This "handle anything" approach makes it straightforward to create a variety of Functions that all output to Azure Cosmos DB.
- Scalable throughput. Throughput can be scaled up and down instantly in Azure Cosmos DB. If you have hundreds or thousands of Functions querying and writing to the same collection, you can scale up your RU/s to handle the load. All functions can work in parallel using your allocated RU/s and your data is guaranteed to be consistent.
- Global replication. You can replicate Azure Cosmos DB data around the globe to reduce latency, geo-locating your data closest to where your users are. As with all Azure Cosmos DB queries, data from triggers is read from the Azure Cosmos DB closest to the region.
What you can do with Azure Cosmos DB and Azure Functions Now
The native integration between Azure Cosmos DB and Azure Functions enable you to do the following:
- You can create an Azure Cosmos DB trigger in an Azure Function. This enables to build event-sourced and event-driven serverless apps with incredible ease. This trigger relies on Change Feed streams to monitor your Azure Cosmos DB container for changes. When any changes are made to a container, the change feed stream is sent to the trigger, which invokes the Azure Function. For quickstart instructions, see Create an Azure Cosmos DB trigger in the Azure portal.
- You can bind an Azure Function to an Azure Cosmos DB container (a collection of documents/table/graph) using an input binding. Input bindings read data from a container when a function executes. For quickstart instructions, see Store unstructured data using Azure Functions and Cosmos DB.
- You can bind an Azure Function to an Azure Cosmos DB collection using an output binding. Output bindings write data to a container when a function completes.
Watch the Azure Friday video with Scott Hanselman and Kirill Gavrylyuk introducing Azure Cosmos DB trigger for Azure Functions:
Rich Event-Driven Serverless Apps, from retail to IoT, in minutes
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.
- Retail: 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. You can create an Azure Cosmos DB trigger by listening to the change feed, and be sure there won’t be any delay for critical order processing events in the process.
- Finance: In financial applications, you can invoke a function when a bank account balance falls below a certain threshold. Using a Timer trigger, you can retrieve the bank account balance information stored in an Azure Cosmos DB container using the input bindings. The timer can be set to a daily or weekly cadence, where the user can set a threshold for what would be considered as a low balance, then follow that with an action from the Azure Function.
- IoT: In an IoT application, e.g., a connected car scenario, you can use an Azure Cosmos DB trigger to trigger events related to car alerts, such as the check engine light coming on in a connected car. The trigger is invoked on every data-change to the sensor data collection and if a threshold condition is met, the function can send the sensor data to the warranty department. Another function can be created to send the related owner information to a task that manages mailers related to car maintenance. The output binding on one of the functions can update the car record in another Azure Cosmos DB container to store information about the check engine event.
- Games: In gaming, when a new user is created you can search for other users who might know them by using the Azure Cosmos DB Graph API. You can then write the results to an Azure Cosmos DB database for easy retrieval. Using an Azure Cosmos DB graph database to store all users, you can create a new function with an Azure Cosmos DB trigger. Whenever a new user is inserted, the function is invoked, and then the result is stored using an output binding. The function can query the graph database to search for all the users that are directly related to the new user and return that dataset to the function. This can then be stored in an Azure Cosmos DB Table database as a key-value set of pairs, which can then be easily retrieved by any front-end application that shows the new user their connected friends.
Try Azure Cosmos DB and Azure Functions now
It is extremely easy to try out Azure Cosmos DB and Azure Functions – you can create Azure Cosmos DB triggers from the Azure Cosmos DB portal, or the Azure Functions portal by clicking Add > New Item >Azure Function. The following image shows how to add an Azure Cosmos DB trigger to a function in the Azure Functions portal.
Using Azure Functions and Azure Cosmos DB, you can create and deploy an event driven, serverless app to numerous datacenters across the world. When Azure Functions are combined with a globally-distributed database like Azure Cosmos DB we can provide extremely low-latency access to data for a huge number of customers across the globe. Anyone, from a large enterprise to an individual developer can now build planet-scale serverless apps in a matter of minutes. For more information see, Azure Cosmos DB: Serverless database computing using Azure Functions. You can Try Azure Cosmos DB for free today, no sign up or credit card required. If you need any help or have questions or feedback, please reach out to us on the developer forums on Stack Overflow. Stay up-to-date on the latest Azure Cosmos DB news and features by following us on Twitter #CosmosDB, @AzureCosmosDB.
- Your friends at Azure Cosmos DB