Change Feed with Azure Functions and Processors
Implement real-time data processing with the Cosmos DB change feed using Azure Functions triggers and the Change Feed Processor library β including lease containers, parallel processing, and feed modes.
What is the change feed?
Think of a security camera recording every change in a store. The change feed is Cosmos DBβs built-in recording of every insert and update. Your application can βwatch the tapeβ in real time and react β send a notification, update a search index, trigger a calculation.
Deletes arenβt recorded in the default mode (youβd use soft-deletes with a TTL). The newer βall versions and deletesβ mode captures everything.
Amaraβs scenario: real-time sensor processing
π‘ Amara at SensorFlow ingests 500M IoT events per day into a Cosmos DB container partitioned by /deviceId. She needs to:
- Trigger alerts when a sensor reading exceeds a threshold
- Update a dashboard materialized view in near-real-time
- Feed analytics by streaming changes to a data warehouse
The change feed is the backbone of all three.
Azure Functions Cosmos DB trigger
The simplest way to consume the change feed β Azure Functions handles the infrastructure:
[FunctionName("SensorAlerts")]
public static void Run(
[CosmosDBTrigger(
databaseName: "sensorflow",
containerName: "readings",
Connection = "CosmosDBConnection",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true
)] IReadOnlyList<SensorReading> changes,
ILogger log)
{
foreach (SensorReading reading in changes)
{
if (reading.Temperature > 85.0)
{
log.LogWarning($"ALERT: Device {reading.DeviceId} at {reading.Temperature}Β°C");
// Send notification, write to alert container, etc.
}
}
}
Key points:
- The trigger creates and manages the lease container automatically
- Changes arrive as a batch β process the entire batch, not item by item
- If the function fails, the batch is retried from the last checkpoint
- The function scales automatically based on the number of physical partitions
Exam tip: lease container purpose
The lease container tracks which changes have been processed and by which consumer. It stores one lease document per physical partition of the monitored container. This enables:
- Checkpointing: If your processor crashes, it resumes from the last checkpoint, not from the beginning.
- Parallel processing: Multiple instances claim different leases, distributing work.
- Load balancing: If an instance dies, its leases are redistributed to surviving instances.
The lease container must be in the same Cosmos DB account as the monitored container.
Change Feed Processor library
For more control than Azure Functions, use the Change Feed Processor directly:
Container leaseContainer = cosmosClient
.GetDatabase("sensorflow")
.GetContainer("leases");
Container monitoredContainer = cosmosClient
.GetDatabase("sensorflow")
.GetContainer("readings");
ChangeFeedProcessor processor = monitoredContainer
.GetChangeFeedProcessorBuilder<SensorReading>(
processorName: "dashboardUpdater",
onChangesDelegate: HandleChangesAsync)
.WithInstanceName("instance-1")
.WithLeaseContainer(leaseContainer)
.WithStartTime(DateTime.MinValue.ToUniversalTime()) // from beginning
.Build();
await processor.StartAsync();
static async Task HandleChangesAsync(
ChangeFeedProcessorContext context,
IReadOnlyCollection<SensorReading> changes,
CancellationToken cancellationToken)
{
foreach (SensorReading reading in changes)
{
Console.WriteLine($"Partition {context.LeaseToken}: {reading.DeviceId}");
}
}
Change feed modes
| Feature | Latest Version (default) | All Versions and Deletes (preview) |
|---|---|---|
| What's captured | Most recent version of inserts/updates | Every version of every change + deletes |
| Deletes | β Not captured (use soft-delete + TTL) | β Captured with metadata |
| Intermediate versions | β Only latest if multiple changes before read | β Every intermediate version |
| Retention | Unlimited | Configured by continuous backup retention window |
| Use case | Event reactions, materialized views | Audit trails, full change history, CDC |
| Prerequisites | None | Continuous backup enabled, NoSQL API only |
Exam tip: soft-delete pattern
In Latest Version mode (default), deletes are NOT in the change feed. The standard workaround is the soft-delete pattern:
- Instead of deleting, set a
deleted: trueflag and a TTL value - The change feed captures this as an update (the flag change)
- Your processor sees the
deleted: trueflag and reacts accordingly - The TTL auto-removes the document after the specified period
This is a very common exam question β know the soft-delete workaround.
Starting position
| Option | Description |
|---|---|
| From beginning | Process all historical changes β useful for rebuilding a materialized view |
| From now | Only process new changes β default for Azure Functions |
| From specific time | Resume from a point-in-time β disaster recovery scenarios |
| From continuation token | Resume from exact position β programmatic checkpointing |
Parallel processing and scaling
The change feed is partitioned by physical partition. Each physical partition can be processed by exactly one consumer instance at a time.
Container with 6 physical partitions:
Instance A: processes partitions 1, 2
Instance B: processes partitions 3, 4
Instance C: processes partitions 5, 6
If Instance B crashes:
Instance A: processes partitions 1, 2, 3 β acquires B's lease
Instance C: processes partitions 4, 5, 6 β acquires B's lease
Scaling rule: You can have at most as many consumer instances as physical partitions. More instances than partitions means some sit idle.
π¬ Video walkthrough
π¬ Video coming soon
Change Feed Processing β DP-420 Module 15
Change Feed Processing β DP-420 Module 15
~16 minFlashcards
Knowledge Check
Amara's SensorFlow container has 8 physical partitions. She deploys 12 Azure Functions instances to process the change feed. How many instances will actively process changes?
Amara needs to detect when sensors are physically removed (deleted from the database). She's using the default change feed mode. What's the best approach?
Amara wants to rebuild a materialized view from scratch by reprocessing all historical changes. How should she configure the change feed processor?
Next up: Analytical Workloads β how Synapse Link and Fabric Mirroring bring analytics to your Cosmos DB data without impacting transactional performance.