Adventures with Azure Cosmos DB: Limit Query RUs

Cosmos DB

If you have worked any with Azure Cosmos DB you are well aware of Request Units, or RUs.

What exactly are RUs and how do they work?

The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). You can think of RUs per second as the currency for throughput. RUs per second is a rate-based currency. It abstracts the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB.

The cost to read a 1 KB item is 1 Request Unit (or 1 RU). A minimum of 10 RU/s is required to store each 1 GB of data. All other database operations are similarly assigned a cost using RUs. Whether the database operation is a write, read, or query, costs are always measured in RUs.

Microsoft Azure – Optimize query cost in Azure Cosmos DB

I was curious if there was a difference in the RUs for a SELECT query for selecting all fields verses fewer fields.

Digging in the documentation I found the following:

The considerations around RUs include: item size, property count, data consistency, indexed properties, indexing, and query patterns.

Microsoft Azure – Optimize reads and writes cost in Azure Cosmos DB

I am thinking that property count refers to the number of fields being returned in the query, but I wanted to know for sure.

So, I created a Cosmos DB database with a single container toDos with a partition key of toDoId.

A sample toDo document looks like the below:

{
    "id": "dacacd8f-c5f8-4832-819e-82505b17bc11",
    "toDoId": "dacacd8f-c5f8-4832-819e-82505b17bc11",
    "status": "In Progress",
    "description": "Lorem ipsum dolor sit amet."
}

I inserted two documents, one with a short description and another with an extremely long description.

Using built-in Data Explore in Azure, I ran two queries SELECT * limit the result with a WHERE clause based on the id value of each document.

The query for the smaller document consumed almost 3 RUs, while the larger document consumed almost 12 RUs.

I then reran the queries limiting the return fields to id and status.

The query for the smaller document consumed the same RUs, about 3 RUs, while the larger document, with the limited properties returned, consumed only 4 RUs.

One thing I found interesting is the Retrieve document size looks like it represents the document size in Cosmos DB, while the Output document size represents the document size of what is returned, so it looks like RUs for queries is “loosely” based on the Output document size.

So why “loosely” based?

Both queries returned the SAME value for Output document size BUT the cost of querying the smaller document differed by 1 RU when compared to the larger document.

Interesting!

The only thing I can confidently say now is that limiting the number of properties returned in Cosmos DB query, especially properties that have a larger payload, will reduce the number of RUs consumed.

Oh, one last thing I found in the documents.

Azure Cosmos DB guarantees that the same query on the same data always costs the same number of RUs on repeated executions.

Microsoft Azure – Request Unit considerations

Apologize for the rambling, but hope it helps someone, this sure provided me some further clarification, along with a little more confusion. 😀

Related Articles

Leave a Reply

Your email address will not be published.