Today, we're going to cover how to calculate
cosmos db request units (or RUs) for CRUD
and Queries
My name is Maxime Rouiller and I'm a Cloud
Developer Advocate for Microsoft.
What is Cosmos DB?
Azure Cosmos DB is a globally distributed,
multi-model database service.
First, it's globally distributed.
That means having your data replicated worldwide
in multiple data centers without the fear
of losing anything.
Second, it's multi model.
Cosmos DB adapt to your needs.
If you need a document database, there's the
SQL API.
Already using MongoDB?
No need to change your code.
Your data model is more of a graph?
We support it through the Gremlin API
Or maybe you are using Azure Table Storage
and need to globally distribute it.
Finally, if your application uses Cassandra,
Cosmos DB is there for you.
What are Cosmos DB Request Units (RU)?
First, it's important to know that it's a
normalized number.
It is not strictly related to a single operation.
Rather, it represents the CPU, Memory, as
well as the IO Operations required to execute
an operation.
Consider it as reserved compute for processing
operations.
How Much RUs is used for operations?
Based on a single JSON document that I've
created, I've been able to extract those numbers
from the Cosmos DB Capacity Planner.
The link will be provided in the description
as well as in the last slides.
These numbers will change based on the size
of your documents but it gives us a good relative
difference in terms of how expensive an operation
is.
We can see that a single record read is relatively
cheap.
Create and Delete are expensive but not as
much as updating a record which is still less
expensive than deleting and recreating that
record.
I've left queries empty for now because that
number may vary a lot.
So this begs the questions, how much RUs do
I have?
Request Units are per seconds so you don't
really to monitor them that much.
They are automatically refreshed every seconds.
What is important is to know how much RUs
your application is using so that you can
better provision your Cosmos DB account.
The bare minimum of RUs/sec is 400 for fixed
10GB databases.
1000 for database of unlimited size.
You can reserve more RUs in steps of 100.
I will not go into pricing since this changes
more often than not.
What is more important, is figuring out how
many RUs a request is using so that you plan
around it.
So let's do a basic demo and see how we can
manually retrieve those RUs.
So for this demo, we're going to walk through
a simple test to see how much RU some basic
requests are going to consume.
You can already see in my code how i'm initializing
my client, creating my database and collection
if they do not already exist.
That database is already pre-populated with
data so those operation will not do anything
for now but it's good practice to ensure the
storage exist before doing operations against
it.
I've set some random employee id over here
to ensure that we have something to work with.
The first thing we'll do is create an employee
called John.
Then change his name to Paul.
Then we'll retrieve it from Cosmos DB directly.
And finally, we're going to delete that employee.
As you can see over there, we can retrieve
the RUs from each of those individual operations
by accessing the property called RequestCharge.
We can retrieve exactly the same value if
we read from the ResponseHeaders from the
HTTP request directly like so.
However, that query over here will not be
able to retrieve the RU because the way we
use it by just doing ToList() will hide your
RequestCharge from us.
So the best way to retrieve the RequestCharge
from this specific query is to rewrite it
like so.
So, as we can see here we added a few elements
here and we changed the base interface which
allows us to retrieve HasMoreResults and ExecuteNextAsync.This
will allow us to do something like an IEnumerable
where we can enumerate over the different
pages and retrieve the result from each pages.
Also, at the same time, retrieving the request
charge for that specific page.
And all of this allows us to better evaluate
how much our queries impact our performance.
So I hope this demo has been useful to you.
As promised, you can read more about how to
estimate your necessary throughput by going
to cda.ms/kq.
If you want to try out the capacity planner,
you go to cda.ms/kr.
Finally, get your hands on the Quickstart
at cda.ms/ks
If you have any questions about this video,
don't hesitate to reach me on twitter or on
my other profiles online at aka.ms/MaxCDA.
To see a list of all our advocates, go to
aka.ms/advocates
If you want to give feedback to the product
team, please use the following hash tag and
twitter account.
