Performance Tips for Azure Cosmos DB
Optimize your workloads and reduce latency by following these proven strategies.
1️⃣ Choose the Right Consistency Level
Consistency impacts latency and throughput. Use Session for most real‑time scenarios, and Strong only when absolute consistency is required.
// Example: Set session consistency in .NET SDK
var client = new CosmosClient(connectionString,
new CosmosClientOptions{ ConsistencyLevel = ConsistencyLevel.Session });
2️⃣ Partition Your Data Effectively
Select a partition key that provides high cardinality and even distribution. Avoid hot partitions.
- Prefer fields that are frequently used in queries.
- Ensure the key value space is large.
// Example: Defining a container with a partition key
await database.CreateContainerIfNotExistsAsync(
new ContainerProperties("Orders", "/customerId"));
3️⃣ Optimize Indexing Policies
Exclude rarely queried fields to reduce index write costs.
{
"indexingMode": "consistent",
"includedPaths": [{ "path": "/*" }],
"excludedPaths": [{ "path": "/metadata/?" }]
}
4️⃣ Use Bulk Operations
Bulk mode improves throughput when inserting or updating large batches.
// .NET bulk insert example
var bulkOperations = items.Select(item =>
new TransactionalBatchItem(item));
await container.ExecuteTransactionalBatchAsync(bulkOperations);
5️⃣ Leverage Change Feed
Process inserts/updates efficiently without polling.
// Sample Change Feed processor (Node.js)
const { ChangeFeedProcessor } = require("@azure/cosmos");
await processor.start();
6️⃣ Configure Autoscale Throughput
Autoscale adapts RU/s based on traffic, preventing throttling.
// Enable autoscale at container creation
await database.CreateContainerIfNotExistsAsync(
new ContainerProperties("Events", "/eventType"),
throughput: 4000, // Max RU/s
autoscaleSettings: new AutoscaleSettings { MaxThroughput = 10000 });
7️⃣ Use SDK Diagnostics & Metrics
Enable request logging to identify latency hotspots.
// Enable diagnostics in Java SDK
CosmosClientBuilder clientBuilder = new CosmosClientBuilder(connectionString)
.gatewayMode()
.contentResponseOnWriteEnabled(true)
.diagnosticThresholds(new CosmosDiagnosticsThresholds()
.setPointOperationLatencyThreshold(Duration.ofMillis(200)));
Combine these tips to achieve optimal performance tailored to your workload.