0
0
AzureConceptBeginner · 3 min read

What is Request Unit in Cosmos DB: Explanation and Usage

A Request Unit (RU) in Azure Cosmos DB is a measure of the cost to perform database operations like reads, writes, and queries. It abstracts the system resources needed, such as CPU, memory, and IOPS, into a single unit to simplify capacity planning and billing.
⚙️

How It Works

Think of a Request Unit (RU) as a universal currency that Cosmos DB uses to measure the work needed to process your database requests. Instead of worrying about CPU time, memory, or disk operations separately, Cosmos DB bundles these resources into one unit. This makes it easier to understand and manage the cost of your database operations.

For example, a simple read of a small item might cost 1 RU, while writing a larger item or running a complex query costs more RUs. You provision a certain number of RUs per second to guarantee your database can handle the workload smoothly, similar to reserving a lane on a highway to avoid traffic jams.

💻

Example

This example shows how to read an item from Cosmos DB and check the RU charge for that operation using the Azure Cosmos DB SDK for JavaScript.

javascript
import { CosmosClient } from "@azure/cosmos";

const endpoint = "https://your-account.documents.azure.com:443/";
const key = "your-primary-key";
const client = new CosmosClient({ endpoint, key });

async function readItem() {
  const database = client.database("SampleDB");
  const container = database.container("Items");

  const { resource, requestCharge } = await container.item("item1", "partitionKey1").read();
  console.log("Item:", resource);
  console.log("Request Units consumed:", requestCharge);
}

readItem();
Output
Item: { id: 'item1', name: 'Sample Item', ... } Request Units consumed: 1.5
🎯

When to Use

Use Request Units to plan and control the performance of your Cosmos DB database. By provisioning enough RUs per second, you ensure your app can handle the expected load without delays or errors. This is especially important for apps with predictable traffic or strict performance needs.

For example, an e-commerce website might provision high RUs during sales to handle many simultaneous reads and writes. A logging system might provision fewer RUs if it only writes data occasionally. Monitoring RU consumption helps you optimize costs and scale your database efficiently.

Key Points

  • Request Units simplify resource measurement by combining CPU, memory, and IOPS into one unit.
  • Every database operation consumes RUs based on its complexity and data size.
  • You provision RUs per second to guarantee throughput and avoid throttling.
  • Monitoring RU usage helps balance performance and cost.

Key Takeaways

Request Units (RUs) measure the cost of database operations in Cosmos DB.
Provisioning enough RUs ensures smooth performance and avoids throttling.
RU consumption depends on operation type and data size.
Use RU metrics to optimize cost and scale your database effectively.