2 min read
Azure Cosmos DB Updates - September 2022
Azure Cosmos DB continues to evolve as Microsoft’s flagship globally distributed, multi-model database service. In September 2022, several important updates have been rolled out that enhance performance, reliability, and developer experience.
Key Updates Overview
Performance Improvements
The latest updates bring significant performance enhancements to Cosmos DB, including optimized query execution and improved throughput management.
using Microsoft.Azure.Cosmos;
// Initialize Cosmos Client with latest SDK
var cosmosClient = new CosmosClient(
connectionString,
new CosmosClientOptions
{
ApplicationName = "MyApp",
ConnectionMode = ConnectionMode.Direct,
ConsistencyLevel = ConsistencyLevel.Session
});
// Create database with autoscale throughput
var database = await cosmosClient.CreateDatabaseIfNotExistsAsync(
"ProductsDB",
ThroughputProperties.CreateAutoscaleThroughput(4000));
// Create container with optimized indexing
var containerProperties = new ContainerProperties("Items", "/categoryId")
{
IndexingPolicy = new IndexingPolicy
{
Automatic = true,
IndexingMode = IndexingMode.Consistent,
IncludedPaths = { new IncludedPath { Path = "/*" } },
ExcludedPaths = { new ExcludedPath { Path = "/largeTextField/?" } }
}
};
var container = await database.CreateContainerIfNotExistsAsync(containerProperties);
SDK Improvements
The .NET SDK has been updated with better diagnostics and retry policies:
// Enhanced diagnostics
var response = await container.ReadItemAsync<Product>(
id: "product-123",
partitionKey: new PartitionKey("electronics"));
// Access detailed diagnostics
Console.WriteLine($"Request charge: {response.RequestCharge} RUs");
Console.WriteLine($"Diagnostics: {response.Diagnostics}");
// Custom retry policy
var options = new CosmosClientOptions
{
MaxRetryAttemptsOnRateLimitedRequests = 9,
MaxRetryWaitTimeOnRateLimitedRequests = TimeSpan.FromSeconds(30)
};
Query Optimization Tips
Take advantage of the new query plan caching:
// Use parameterized queries for better caching
var queryDefinition = new QueryDefinition(
"SELECT * FROM c WHERE c.categoryId = @categoryId AND c.price > @minPrice")
.WithParameter("@categoryId", "electronics")
.WithParameter("@minPrice", 100);
var queryOptions = new QueryRequestOptions
{
MaxItemCount = 100,
PartitionKey = new PartitionKey("electronics")
};
using var resultSetIterator = container.GetItemQueryIterator<Product>(
queryDefinition,
requestOptions: queryOptions);
while (resultSetIterator.HasMoreResults)
{
var response = await resultSetIterator.ReadNextAsync();
foreach (var product in response)
{
Console.WriteLine($"Product: {product.Name}, Price: {product.Price}");
}
}
Best Practices
- Use the latest SDK - Always keep your SDK updated for the latest optimizations
- Monitor RU consumption - Use Azure Monitor to track Request Unit usage
- Optimize partition keys - Choose partition keys that distribute data evenly
- Enable diagnostics - Use built-in diagnostics for troubleshooting
These updates make Cosmos DB an even more powerful choice for globally distributed applications requiring low latency and high availability.