Change feed

+
~

The Cosmos DB change feed is a persistent record of all committed inserts and updates, in the order that they occurred. These changes can be listened to by consumers and processed asynchronously.

Note

Delete operations are not logged. To get around this you can "soft" delete and set a TTL on the item to eventually actually delete it. This allows the change feed to be aware of the deletion.

Push or pull

Change feed supports both push and pull models. The push model is recommended best practice for convenience. There are some situations where the pull model may be preferred:

Using push

There are two ways to consume the change feed via push:

The Azure functions Cosmos DB trigger uses the change feed processor library under the hood, so they are actually the same, the Function trigger is just a convenience wrapper.

Change feed processor library

Since the function trigger uses this library under the hood, we'll start with that. The change feed processor is made up of four components:

Setup
var processor = cosmosClient
	.GetContainer(databaseName, sourceContainerName) 
	.GetChangeFeedProcessorBuilder<ToDoItem>(
		processorName: "changeFeedSample", 
		onChangesDelegate: HandleChangesAsync
	) 
	.WithInstanceName("consoleHost") 
	.WithLeaseContainer(leaseContainer) 
	.Build();

await processor.StartAsync();

Function trigger

The following code snippet is how a change feed processor can be configured more simply using an Azure function trigger:

[Function("CosmosTrigger")]
public void Run([CosmosDBTrigger(
    databaseName: "ToDoItems",
    containerName:"TriggerItems",
    Connection = "CosmosDBConnection",
    LeaseContainerName = "leases",
    CreateLeaseContainerIfNotExists = true)] IReadOnlyList<ToDoItem> todoItems,
    FunctionContext context)
{
    if (todoItems is not null && todoItems.Any())
    {
        foreach (var doc in todoItems)
        {
            _logger.LogInformation("ToDoItem: {desc}", doc.Description);
        }
    }
}