Azure Cosmos DB Change Feed

The Change Feed is a persistent record of changes that have occurred within your Azure Cosmos DB container. It provides ordered, immutable representations of every document that has been modified or deleted within the container. This feature is invaluable for building real-time applications, data synchronization, auditing, and various other scenarios that require reacting to data modifications.

What is the Change Feed?

The Change Feed is an append-only log of changes. Each entry in the Change Feed represents a version of a document at a specific point in time. It includes metadata such as the operation type (create, update, delete) and the timestamp of the change. You can process these changes using various methods, including:

Key Concepts

Common Use Cases

Getting Started with Change Feed Processors

The most robust way to work with the Change Feed is by using the Change Feed Processor library. Here's a simplified conceptual example using C# (the actual implementation might vary based on SDK version and language):


using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.ChangeFeedProcessor;
using System;
using System.Threading.Tasks;

// Assuming you have initialized your CosmosClient and obtained container references
CosmosClient client = new CosmosClient("YOUR_COSMOS_DB_CONNECTION_STRING");
Container sourceContainer = client.GetDatabase("yourDatabase").GetContainer("yourSourceContainer");
Container leaseContainer = client.GetDatabase("yourDatabase").GetContainer("yourLeaseContainer"); // Lease container

async Task StartChangeFeedProcessorAsync()
{
    ChangeFeedProcessor processor = sourceContainer.GetChangeFeedProcessorBuilder(
            processorName: "myChangeFeedProcessor",
            onChangesDelegate: HandleChangesAsync)
        .WithLeaseContainer(leaseContainer)
        .Build();

    await processor.StartAsync();

    Console.WriteLine("Change Feed Processor started. Press Ctrl+C to stop.");
    // Keep the application running to listen for changes
    await Task.Delay(Timeout.Infinite);
}

async Task HandleChangesAsync(
    ChangeFeedProcessorContext context,
    IReadOnlyCollection<dynamic> changes,
    CancellationToken cancellationToken)
{
    Console.WriteLine($"Processing {changes.Count} changes.");
    foreach (var change in changes)
    {
        // Process each change here
        // For example, log the change or send it to another service
        Console.WriteLine($"Operation: {context.OperationKind}, Document ID: {change.id}");
        // Example: Access document properties
        // Console.WriteLine($"Document Content: {change.ToString()}");
    }
    // You can also access context.OwnerResourceId, context.OwnerName etc.
}

// To start the processor:
// await StartChangeFeedProcessorAsync();
            
Important: Ensure your lease container has a partition key defined, and that the partition key values used for leases do not overlap with your source container's data. The recommended partition key for the lease container is often just `id`.

Monitoring the Change Feed

You can monitor the health and performance of your Change Feed Processor through Azure Monitor, including metrics like:

Considerations

By leveraging the Azure Cosmos DB Change Feed, you can build dynamic, responsive, and data-driven applications that react instantaneously to data modifications.