Understanding Azure Cosmos DB Request Units (RUs)

Azure Cosmos DB is a globally distributed, multi-model database service. A core concept in managing the performance and cost of your Cosmos DB database is understanding Request Units (RUs). RUs are a normalized measure of throughput.

What is a Request Unit (RU)?

A Request Unit (RU) is a logical abstraction that represents the computation required to perform various database operations. These operations include:

  • Reading a document
  • Writing a document
  • Querying documents
  • Executing stored procedures

The RU consumption for an operation is determined by factors such as the data size, the complexity of the query, and the consistency level requested.

Why are RUs Important?

Azure Cosmos DB offers a rich set of features and guarantees a high level of performance and availability. To provide these, you provision throughput for your containers (collections, tables, graphs, etc.). Throughput is measured in Request Units per second (RU/s).

  • Cost Management: Provisioned RU/s directly impacts your billing. Understanding RU consumption helps you optimize your provisioned throughput and reduce costs.
  • Performance Tuning: Monitoring RU usage helps identify potential bottlenecks and optimize your application's interaction with Cosmos DB.
  • Scalability: By understanding RU needs, you can effectively scale your database resources up or down.

How are RUs Calculated?

The RU cost of an operation is influenced by several factors:

  • Item Size: Larger items generally consume more RUs for read/write operations.
  • Query Complexity: More complex queries (e.g., those with multiple filters, joins, or aggregations) consume more RUs.
  • Index Usage: Efficiently indexed queries consume fewer RUs than those requiring full scans.
  • Consistency Level: Stronger consistency levels require more RUs for reads.
  • Partition Key Selectivity: Queries that target a specific partition key are more efficient than those that scan across partitions.
Key Takeaway: The simplest read or write of an item with a size of 1 KB at the Strong consistency level consumes 1 RU. All other operations are measured relative to this baseline.

Estimating RU Consumption

You can estimate RU consumption using several methods:

  • Azure Portal: The Azure portal provides metrics for RU usage.
  • SDKs: Cosmos DB SDKs often return the RU charge for each operation.
  • `x-ms-request-charge` Header: All responses from the Cosmos DB API include the x-ms-request-charge header, which indicates the RUs consumed by that specific request.

Example: Reading an item using .NET SDK


// Assume 'client' is an initialized CosmosClient and 'container' is a reference to a container
var options = new ItemRequestOptions { PartitionKey = new PartitionKey("somePartitionKeyValue") };
var response = await container.ReadItemAsync<MyDocument>("documentId", options);

Console.WriteLine($"Request consumed {response.RequestCharge} RUs.");
                    

Provisioned Throughput vs. Autoscale

Azure Cosmos DB offers two primary modes for managing throughput:

  • Manual Throughput: You provision a fixed number of RU/s. This is cost-effective for predictable workloads.
  • Autoscale Throughput: You set a maximum RU/s, and Cosmos DB automatically scales throughput up and down within a defined range (typically 10% of the maximum) based on demand. This is ideal for spiky or unpredictable workloads.

Optimizing RU Consumption

To optimize your RU usage and improve performance:

  • Efficient Queries: Write queries that leverage indexing and avoid full scans.
  • Partitioning Strategy: Choose an appropriate partition key to distribute requests evenly across partitions.
  • Item Size: Keep item sizes manageable.
  • Batching Operations: For multiple writes, consider using transactions or bulk operations where applicable.
  • Appropriate Consistency Level: Use the lowest acceptable consistency level for your application needs.

Conclusion

Understanding Request Units is fundamental to effectively using and managing Azure Cosmos DB. By monitoring RU consumption and implementing optimization strategies, you can ensure your applications are performant, scalable, and cost-efficient.

Request Unit (RU)
A normalized measure of database throughput.
RU/s
Request Units per second, the unit for provisioned throughput.
x-ms-request-charge
HTTP header indicating the RUs consumed by a request.
Manual Throughput
Provisioning a fixed number of RU/s.
Autoscale Throughput
Automatically scaling RU/s based on demand.