Cosmos DB: Cost Optimization Strategies

Cosmos DB can be expensive if not configured correctly. Here’s how to optimize costs while maintaining performance.

Understand RU/s Consumption

  • Monitor consumed vs provisioned RU/s
  • Use Azure Monitor metrics
  • Enable per-partition metrics

Right-Size Throughput

  • Autoscale: For variable workloads (10x range)
  • Manual: For predictable workloads
  • Serverless: For dev/test or sporadic usage

Query Optimization

// Check RU charge
var response = await container.ReadItemAsync<Item>(id, pk);
Console.WriteLine($"RU Charge: {response.RequestCharge}");

// Reduce RU with projections
var query = container.GetItemQueryIterator<Item>(
    new QueryDefinition("SELECT c.id, c.name FROM c WHERE c.category = @cat")
        .WithParameter("@cat", category));
// Smaller response = fewer RUs

Other Tips

  • Use TTL for automatic data expiration
  • Composite indexes for complex queries
  • Consider reserved capacity for predictable workloads (65% savings)

References


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.