Introduction
Azure Cosmos DB, a distributed and multi-model database service, offers powerful features to manage large-scale data efficiently. Bulk Operations and Transactional Batch Operations stand out for handling diverse data management needs.
Bulk operations are designed for high-throughput scenarios, enabling parallelism, updates, or deletions across multiple partition keys, making them ideal for large-scale migrations or data processing.
On the other hand, transactional batch operations ensure atomicity within a single partition, allowing multiple operations—such as create, update, or delete—to execute as a single unit. These features, accessible through the Azure Cosmos DB .NET SDK, which empower to optimize performance and maintain data consistency while leveraging modern application
Bulk Operations
Bulk operations are ideal for high-throughput, parallel processing of large datasets across multiple partition keys, making them perfect for data ingestion, migration, and updates. They offer scalability, performance, and flexibility to support massive data processing needs.
Key Features of Bulk Operations
- Operate across multiple partition keys.
- Optimize throughput by internally batching requests.
- Automatically manage retries in case of transient failures.
using Microsoft.Azure.Cosmos;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
public class BulkOperations
{
private readonly Container _container;
public BulkOperations(Container container)
{
_container = container;
}
public async Task ExecuteBulkInsertionAsync(IEnumerable<MyProduct> Products)
{
var tasks = new List<Task>();
foreach (var product in Products)
{
tasks.Add(_container.CreateItemAsync(product, new PartitionKey(product.PartitionKey)));
}
await Task.WhenAll(tasks);
Console.WriteLine("Bulk Operation Completed.");
}
}
public class MyProduct
{
public string Id { get; set; }
public string PartitionKey { get; set; }
public string ProductName { get; set; }
}
Transactional Batch Operations
Transactional batch operations allow multiple operations on items sharing the same partition key to be executed atomically. This ensures that either all operations in the batch succeed or none are applied, preserving data consistency.
Key Features of Transactional Batch Operations
- All operations within the batch share the same partition key.
- It supports creating, reading, updating, and deleting operations in a single batch.
- Adheres to ACID (Atomicity, Consistency, Isolation, Durability) properties of the database.
Reference: https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/transactional-batch?tabs=dotnet
using Microsoft.Azure.Cosmos;
using System;
using System.Threading.Tasks;
public class TransactionalBatchExample
{
private readonly Container _container;
public TransactionalBatchExample(Container container)
{
_container = container;
}
public async Task ExecuteTransactionalBatchAsync()
{
var partitionKey = new PartitionKey("PartitionKeyValue");
TransactionalBatch batch = _container.CreateTransactionalBatch(partitionKey)
.CreateItem(new MyProduct { Id = "1", PartitionKey = "partitionKeyValue", ProductName = "Value1" })
.UpsertItem(new MyProduct { Id = "2", PartitionKey = "partitionKeyValue", ProductName = "Value2" })
.DeleteItem("3");
TransactionalBatchResponse response = await batch.ExecuteAsync();
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Transactional batch executed successfully.");
}
else
{
Console.WriteLine($"Transactional batch failed with status code: {response.StatusCode}");
}
}
}
public class MyProduct
{
public string Id { get; set; }
public string PartitionKey { get; set; }
public string ProductName { get; set; }
}
How does Transactional Batch Work?
When the ExecuteAsync method is called on a TransactionalBatch, all the operations defined in the batch (such as create, update, delete, or read) are grouped together and serialized into a single network payload. This payload includes metadata and the sequence of operations, ensuring that all the necessary information is packed into a single request to minimize network overhead.
Once the payload is sent to the Azure Cosmos DB service, the service processes the entire batch within a transactional scope. This means that all operations in the batch are treated as a single atomic unit. If any operation in the batch fails—such as a constraint violation or a partition key mismatch—the entire batch is rolled back, ensuring that no partial changes are committed. This transactional guarantee adheres to ACID properties (Atomicity, Consistency, Isolation, Durability) within the scope of a single partition key.

Join the conversation! Your thoughts help the community grow.