The Azure Cosmos DB Change Feed acts as a persistent transaction log that records document insertions and updates in sorted order per logical partition. It provides an efficient mechanism for triggering asynchronous microservices and maintaining search indexes.
Change Feed Processor vs. Azure Functions Trigger
Developers usually choose between writing custom C# services with the Change Feed Processor library or using the managed Azure Functions Cosmos DB trigger. Azure Functions handles checkpointing, scaling, and partition management automatically behind the scenes.
Implementing an Azure Function Trigger
Below is a production-ready C# Azure Function consuming document updates directly from Cosmos DB:
public static class DocumentProcessorFunction
{
[FunctionName("ProcessContainerChanges")]
public static async Task Run(
[CosmosDBTrigger(
databaseName: "InventoryDb",
containerName: "Products",
Connection = "CosmosDBConnectionString",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true)] IReadOnlyList<MyDocument> documents,
ILogger log)
{
// Check if any documents were updated or inserted in this batch
if (documents != null && documents.Count > 0)
{
log.LogInformation($"Processing batch of {documents.Count} modified documents.");
foreach (var doc in documents)
{
// Process individual item change, e.g., publish to Event Grid
await ProcessItemChangeAsync(doc);
}
}
}
}A dedicated lease container tracks progress across partition leases, enabling multiple function instances to scale horizontally without duplicate processing.
Can you answer this question?
Write Answer0 Answers