(gentle upbeat music)
- Welcome back to the Azure Cosmos DB Essentials series.
My name is Mustafa.
I am the Portfolio PMM for Azure Cosmos DB.
And this season, we're joined by Mark Brown,
a PM on the Azure Cosmos DB product team.
This season, we wanna take use cases a step further
and dive into some of the architectures customers are using
to build lightweight applications.
So, today we're gonna talk
about a lightweight order processing architecture built
with Azure Cosmos DB and Azure Functions.
So, some of the people viewing this video
might not be super familiar with Azure Functions.
Wanna explain what Functions is
and what it does before we jump in?
- Yeah, sure, so Azure Functions is a compute PaaS,
or platform-as-a-service,
that can host and run applications within Azure.
Functions can be pre-provisioned and run
on a variety of underlying provisioned compute.
There's also a consumption-based pricing model
for serverless compute as well.
One of the nice features of Azure Functions is it comes
with a number of pre-built triggers and bindings
that can be easily configured to do things
like build event-driven applications quickly and easily.
There are also a number of supported languages,
like C#, Java, Python, Node.JS,
and even PowerShell, which is pretty cool too.
With serverless Functions, we can write some code,
define the triggers and bindings around that code
that control functions execution,
and then Functions will handle everything else
and only charge me for when the code
is actually executing doing its work.
Let's jump into the architecture where I can give an example
and hopefully make things a little more clear.
So today, we're gonna talk about an order processing
for an ecommerce store,
which is another really common use case for Cosmos DB.
Here, we wanna take advantage
of Azure Function's capabilities,
specifically with the Cosmos DB trigger
which can allow us to help build event-driven microservices.
This is especially important for ecommerce workloads
because much of it is event-driven.
What this Cosmos DB trigger does
is it monitors a container in Cosmos DB,
and then when an item is inserted or updated,
the trigger fires and calls a function you define
in your Azure Function.
Behind the scenes,
this is using something called Change Feed.
- So, what is the Change Feed?
Is this something people need to turn on?
- Right, so Change Feed is a change data capture capability
built into Cosmos DB.
It's always on, but you do have to listen to it
in order to use it.
At its core, Change Feed is a feature
that executes some specific piece of code whenever someone
inserts or updates an item within Cosmos DB.
What's great about it is that this allows developers
to easily build event-driven applications
and propagate data written into Cosmos DB
onto other services within your application.
For instance, when you click purchase,
Change Feed could kick off the process
that sends you an email to thank you for your order.
- So, can you tell us more about how we would interact
with the change feed if we're trying to build
something like this?
- Sure, so first, we're gonna have our user-facing web app,
and we might use Azure Functions to implement an endpoint
for the purchase action in our shopping cart
using an HTTP trigger.
That's what we see on the left here.
From there, we'd have a Cosmos DB Trigger
monitoring that container for the new order.
Then from there, Azure Functions gets notified
of the new order and executes some code
for each event that we want to initiate.
Behind the scenes, how Change Feed works is it maintains
a connection to the container it wants to listen to.
It also maintains a connection to a second container
that we call the Leases container.
This container's purpose is to maintain a checkpoint
on the last data processed by the Change Feed.
Every second, Change Feed calls that monitored container
to see is there is any new data?
And if there is, it picks up all that new data,
and then calls the code you've written,
then updates that Leases container to maintain
the checkpoint for the last data processed.
For our use case, every time an order is placed,
we'd need to do things like calculate the tax to charge,
authorize a credit card for the order total and the tax,
send an email thanking them for their order,
and then send a message to the fulfillment center
telling them to pick, pack and ship the order contents.
With Azure Functions using a Cosmos DB Trigger,
we can quickly and easily build this entire chain of events
and even conditionally roll it back
if anything along the way isn't right.
For instance, let's say the credit card
couldn't be authorized.
You could create other Functions to email the user
to provide a new credit card, or cancel the order.
Or more often, an item that was ordered
was not actually in stock.
You could send the customer an email saying,
hey, this item is back-ordered,
then process the rest of the order normally
and then update the amount charged on the credit card.
Anything is possible.
What's important is you can do it with small,
manageable chunks of code,
all hosted in these Azure Functions
that all do one job and can be orchestrated to work together
using a database like Cosmos DB
which can kick off the entire series of events.
- Awesome, well that wraps up the third episode of Season 3.
Join us in the next episode where we're gonna discuss
how to make our apps globally distributed.
We hope you learned something, and see you next time.
(gentle upbeat music)