This chapter covers the Cosmos DB Change Feed, a powerful feature for streaming data changes from Azure Cosmos DB containers. For the DP-900 exam, understanding the Change Feed is crucial as it appears in questions about real-time data processing, event-driven architectures, and integration with Azure Functions. Approximately 5-10% of exam questions touch on Change Feed concepts, typically focusing on its purpose, how to consume it, and its use cases. You will learn the internal mechanics, configuration options, and common scenarios to confidently answer exam questions.
Jump to a section
Imagine a massive warehouse that receives shipments of products from suppliers continuously. The receiving dock is the Cosmos DB Change Feed. Every time a truck arrives (a document is inserted or updated), the dock worker scans each box and logs it into a clipboard (the change log). The clipboard is ordered by arrival time (commit timestamp). Downstream processes, like inventory management or order fulfillment, can read from the clipboard. They don't need to look at every box in the warehouse; they just check the clipboard for new entries. If a downstream process is slow, it can start from where it left off, using a bookmark (continuation token). The clipboard only records changes, not the full state of each box. If you need the full state, you must query the warehouse shelves (the Cosmos DB container). The Change Feed is like a subscription to the clipboard: you get notified of new entries in real-time, but you decide how often to check (polling interval). In Cosmos DB, the Change Feed is enabled by default and can be read using a push model (Azure Functions trigger) or a pull model (SDK).
What is the Cosmos DB Change Feed?
The Cosmos DB Change Feed is a persistent, ordered log of changes (inserts and updates) to documents in a Cosmos DB container. It provides a way to listen to data changes in real-time without polling the entire container. The Change Feed is enabled by default for all Azure Cosmos DB accounts (SQL API, MongoDB API, Cassandra API, Gremlin API, and Table API) and does not require any additional configuration to activate. However, to consume it, you must use the appropriate SDK or integration.
Why Does It Exist?
In traditional databases, tracking changes often requires triggers, timestamps, or change data capture (CDC) mechanisms. Cosmos DB Change Feed simplifies this by providing a built-in, scalable, and reliable change log. It enables event-driven architectures where downstream systems react to data changes as they happen. For example, you can update a search index, invalidate a cache, or replicate data to another system in near real-time.
How It Works Internally
Cosmos DB stores data in containers, which are partitioned based on a partition key. The Change Feed captures changes per physical partition. Each physical partition maintains its own change log. When a document is inserted or updated, Cosmos DB assigns a monotonically increasing commit timestamp (log sequence number) to the operation. The Change Feed exposes these changes in order of the commit timestamp within each partition.
Key internal components: - Change Feed Log: An internal append-only log stored per physical partition. It records the document ID, the operation type (insert/update), and the new document content (for SQL API) or just the changed fields (for other APIs). - Continuation Token: A token that represents the point in the Change Feed up to which you have read. It is partition-scoped. When you read the Change Feed, you can specify a continuation token to resume reading from where you left off. - Lease Container: For push-based consumption (e.g., Azure Functions trigger), a lease container tracks the state of processing across multiple consumers. It stores lease documents that indicate which partition each consumer is processing and the current continuation token.
Key Components, Values, Defaults, and Timers
Polling Interval: When using the pull model (SDK), you can specify a polling interval via FeedOptions with MaxItemCount and RequestContinuationToken. There is no built-in timer; you control how often you call ReadNextAsync.
Retention Period: The Change Feed retains changes for a limited time. For SQL API, the retention is up to 10 minutes (600 seconds). This means if you don't read the change within 10 minutes of when it occurred, you may lose it. For MongoDB API, the retention is up to 10 minutes as well. For Cassandra API, it is up to 10 minutes. This is a critical exam point.
Throughput Consumption: Reading the Change Feed consumes Request Units (RUs). The cost depends on the number of changes returned and the size of documents. Typically, reading the Change Feed is cheaper than querying the container for the same data.
Ordering: Changes are ordered within a physical partition by commit timestamp. Across partitions, there is no global order.
Multiple Operations per Document: If a document is updated multiple times within the retention period, only the latest version is included in the Change Feed. This is because the Change Feed captures the final state after a series of updates, not each intermediate state. However, if you need each individual update, you must use a different pattern (e.g., append-only container).
Configuration and Verification Commands
To read the Change Feed using the .NET SDK (pull model):
using Microsoft.Azure.Cosmos;
Container container = client.GetContainer("databaseId", "containerId");
FeedIterator<dynamic> iterator = container.GetChangeFeedIterator<dynamic>(
ChangeFeedStartFrom.Beginning(),
ChangeFeedMode.Incremental);
while (iterator.HasMoreResults)
{
FeedResponse<dynamic> response = await iterator.ReadNextAsync();
foreach (var document in response)
{
// Process change
}
}To use Azure Functions trigger (push model):
[FunctionName("CosmosDBTrigger")]
public static void Run(
[CosmosDBTrigger(
databaseName: "ToDoItems",
collectionName: "Items",
ConnectionStringSetting = "CosmosDBConnection",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true)]
IReadOnlyList<Document> documents,
ILogger log)
{
if (documents != null && documents.Count > 0)
{
log.LogInformation($"Documents modified: {documents.Count}");
}
}How It Interacts with Related Technologies
Azure Functions: The most common integration. The Cosmos DB trigger uses the Change Feed to invoke a function when documents are created or updated. The trigger automatically manages leases and scales out across partitions.
Azure Stream Analytics: Can consume the Change Feed as an input source for real-time analytics.
Azure Logic Apps: Can connect to Cosmos DB and use the Change Feed to trigger workflows.
Azure Data Factory: Can use the Change Feed to incrementally load data from Cosmos DB to other stores.
Change Feed Processor Library: A library that provides a robust way to read the Change Feed across multiple consumers, handling lease management and partitioning.
Exam-Relevant Nuances
The Change Feed does not capture deletes by default. To capture deletes, you must implement a soft-delete pattern (e.g., set a 'deleted' flag) and query the Change Feed for that flag.
The Change Feed is not available for the Table API in some older SDKs, but it is now supported.
The Change Feed only records the latest state of a document within the retention window. Multiple updates to the same document within 10 minutes will only yield the final version.
The Change Feed can be consumed in two modes: Incremental (default) and Full Document. Incremental mode returns only the changed properties (for SQL API, it returns the full document). Full Document mode returns the entire document after the change.
The Change Feed does not support filtering by partition key directly. You must read the entire Change Feed and filter client-side, or use multiple consumers each reading a specific partition key range.
Trap Patterns on the Exam
Wrong Answer: Change Feed captures deletes. Many candidates assume that since it captures inserts and updates, it also captures deletes. It does not. The correct approach is to use a soft-delete flag.
Wrong Answer: Change Feed has unlimited retention. The retention is 10 minutes. Candidates may think it's like a log that persists indefinitely. The exam may ask: "How long are changes available in the Change Feed?" Answer: Up to 10 minutes.
Wrong Answer: Change Feed provides global ordering. It provides ordering per physical partition, not globally. The exam may present a scenario requiring global ordering and ask if Change Feed is suitable. The answer is no.
Wrong Answer: Change Feed can be enabled/disabled per container. It is always enabled. You cannot disable it. The exam may ask: "Can you disable the Change Feed?" Answer: No.
Wrong Answer: Change Feed captures every intermediate update. It only captures the final state within the retention window. If you need every change, you must use a different pattern.
Summary of Commands and Defaults
Default retention: 10 minutes (600 seconds).
Default mode: Incremental (returns full document for SQL API).
Default start: From now (if no continuation token provided). You can start from beginning using ChangeFeedStartFrom.Beginning().
Lease collection: Required for push model. Must be created manually or set CreateLeaseCollectionIfNotExists = true.
Throughput: Reading Change Feed costs RUs based on the number and size of changes. Typically 1-2 RU per change.
Real-World Configuration Example
Suppose you have a container with 10,000 RU/s and you want to read the Change Feed every 5 seconds. You would create a feed iterator and call ReadNextAsync in a loop with a 5-second delay. Ensure that the iterator does not time out; the default request timeout is 60 seconds. If you have multiple partitions, the iterator will return results from one partition at a time; you need to handle continuation tokens across partitions.
For production, use the Change Feed Processor library or Azure Functions trigger to manage scaling and checkpoints automatically.
Enable Change Feed (Default)
The Change Feed is automatically enabled for all Cosmos DB containers. You do not need to perform any action to enable it. However, to consume it, you must use the appropriate SDK or integration. The Change Feed logs every insert and update operation on the container. Deletes are not logged. The log is stored internally per physical partition. Each partition maintains an append-only log of changes with a monotonically increasing commit timestamp. The log is retained for up to 10 minutes. After that, the changes are no longer available via the Change Feed.
Choose Consumption Model
You can consume the Change Feed using either a push model or a pull model. The push model uses Azure Functions or the Change Feed Processor library, which automatically listens for changes and invokes a callback. The pull model uses the Cosmos DB SDK to manually read the Change Feed by calling `GetChangeFeedIterator` and `ReadNextAsync`. The push model is simpler for event-driven architectures and scales automatically across partitions using lease documents. The pull model gives you fine-grained control over polling frequency and checkpointing.
Initialize Lease Container (Push)
If using the push model, you must create a lease container. This container stores lease documents that track the state of each consumer. Each lease document corresponds to a partition and contains the continuation token for that partition. The lease container must be created before the trigger starts. It can be in the same Cosmos DB account or a separate one. The lease container should have a partition key of `/id` (the default). It is recommended to provision at least 400 RU/s on the lease container to avoid throttling during high change volumes.
Read Change Feed (Pull Model)
In the pull model, you create a `FeedIterator` by calling `container.GetChangeFeedIterator<dynamic>` with the desired start point (e.g., `ChangeFeedStartFrom.Beginning()` or `ChangeFeedStartFrom.Now()`). You then call `ReadNextAsync` in a loop. Each call returns a batch of changes and a continuation token. You must pass the continuation token back in the next call to resume from where you left off. The iterator will return results from one partition at a time. To read from all partitions, you need to create multiple iterators or use the Change Feed Processor library.
Process Changes and Handle Failures
After reading changes, you process them (e.g., update a cache, send to a queue, etc.). It is critical to handle failures gracefully. If processing fails, you should not advance the continuation token. In the pull model, you can store the continuation token in a persistent store (e.g., a separate Cosmos DB container) and retry processing. In the push model, the lease container automatically tracks the continuation token. If a consumer crashes, another consumer can pick up the lease and resume from the last committed checkpoint. The Change Feed is at-least-once delivery; you may receive duplicate changes if a consumer fails after processing but before checkpointing.
Enterprise Scenario 1: Real-Time Search Index Updates
A large e-commerce company uses Cosmos DB to store product catalog data. They need to update their Azure Cognitive Search index in near real-time whenever a product is added or updated. They deploy an Azure Function with a Cosmos DB trigger that reads the Change Feed and sends the changed documents to the search index. The trigger is configured with a lease collection in the same Cosmos DB account. The function scales out to multiple instances, each processing a subset of partitions. They set MaxItemCount to 100 to control batch size. One issue they encountered: during a large data migration, the Change Feed backlog grew because the function couldn't keep up. They increased the RU/s on the lease container and added more function instances. They also implemented a dead-letter queue for documents that failed to index after 3 retries.
Enterprise Scenario 2: Cache Invalidation
A financial services firm uses Cosmos DB to store customer account balances. They use a Redis cache to serve low-latency balance queries. When a balance changes, they need to invalidate the cached entry. They use the Change Feed to detect changes. They deployed a background service using the Change Feed Processor library that reads the Change Feed and invalidates the corresponding cache keys. They set the polling interval to 1 second to minimize staleness. One challenge: the Change Feed retention of 10 minutes meant that if the cache invalidation service was down for more than 10 minutes, it could miss changes, leading to stale cache entries. They mitigated this by implementing a periodic full refresh from the container. They also configured the lease container with 1000 RU/s to handle peak loads.
Enterprise Scenario 3: Real-Time Analytics Pipeline
A streaming media company uses Cosmos DB to store user activity events (e.g., video starts, pauses). They want to aggregate these events in real-time using Azure Stream Analytics. They configure Stream Analytics to read from the Cosmos DB Change Feed as an input. They set the input alias and specify the database and container. Stream Analytics automatically reads the Change Feed and processes the events. They use a windowed aggregation to count views per minute. One pitfall: the Change Feed does not capture deletes, so if a user deletes their activity, it won't be reflected. They designed their schema with a 'deleted' flag instead of actual deletes. They also had to ensure that the Stream Analytics job had sufficient streaming units to handle the throughput.
Common Misconfigurations
Not creating the lease container: In push model, forgetting to create the lease container or setting CreateLeaseCollectionIfNotExists = false causes the trigger to fail.
Insufficient RU/s on lease container: If the lease container is throttled, the trigger may not be able to checkpoint, leading to reprocessing.
Ignoring partition scaling: When a container has many physical partitions, the Change Feed must be read from each partition. Using a single consumer may not keep up. Use the Change Feed Processor library or multiple Azure Function instances.
Assuming global ordering: If your downstream system requires global ordering (e.g., processing events in exact order across all partitions), the Change Feed is not suitable. You need a different solution like Event Hubs with a single partition.
What DP-900 Tests on This Topic
The DP-900 exam covers the Cosmos DB Change Feed under objective 2.4: "Describe processing of streaming data." Specifically, you need to know:
The purpose of the Change Feed (to capture inserts and updates in real-time).
How to consume it (Azure Functions trigger, SDK pull model, Change Feed Processor).
The retention period (10 minutes).
That deletes are not captured.
That ordering is per partition, not global.
That it is enabled by default.
Common Wrong Answers and Why Candidates Choose Them
Wrong: Change Feed captures deletes. Many candidates confuse Change Feed with change data capture (CDC) in SQL Server, which does capture deletes. They also think that since it captures inserts and updates, it should capture deletes. The reality: Cosmos DB Change Feed intentionally does not capture deletes because deletes remove the document, and the Change Feed only logs changes to existing documents. If you need to capture deletes, you must implement a soft-delete pattern.
Wrong: Change Feed has unlimited retention. Candidates may think the Change Feed persists forever like a transaction log. They might confuse it with the Cosmos DB transaction log which is internal. The exam will ask: "How long are changes available?" The correct answer is 10 minutes. They may also ask: "Can you increase the retention?" No, it is fixed.
Wrong: Change Feed provides global ordering across partitions. Candidates may think that because the Change Feed is ordered by timestamp, it is globally ordered. However, timestamps are only monotonic within a partition. Across partitions, there is no guarantee. The exam may present a scenario requiring global ordering and ask if Change Feed is appropriate. The answer is no.
Wrong: Change Feed must be explicitly enabled. Some candidates think it is a feature that needs to be turned on like indexing. The exam may ask: "What must you do to enable the Change Feed?" The answer is nothing; it is always on.
Specific Numbers and Terms That Appear Verbatim
Retention: 10 minutes (or 600 seconds).
Default mode: Incremental (returns full document for SQL API).
Lease collection: Required for push model (Azure Functions trigger).
Change Feed Processor library: Used for multi-consumer scenarios.
Partition key: Change Feed is scoped to physical partitions.
Continuation token: Used to resume reading.
Edge Cases and Exceptions
Multiple updates to same document: Only the latest state appears. If you need each update, you must use an append-only container or store each change as a new document.
Empty container: If no changes occur, reading the Change Feed returns an empty response with a continuation token that can be used later.
Throughput spikes: If a container has a sudden burst of writes, the Change Feed may have a large backlog. The consumer must be able to handle the load or risk losing changes after 10 minutes.
Change Feed for MongoDB API: Works similarly but returns documents in JSON format. The retention is also 10 minutes.
How to Eliminate Wrong Answers Using the Underlying Mechanism
When you see a question about Change Feed, think about the mechanism:
If the question mentions deletes, the answer cannot be Change Feed alone; you need a workaround.
If the question mentions global ordering, Change Feed is not the answer.
If the question mentions long-term change history (hours or days), Change Feed is not suitable.
If the question mentions real-time triggers for inserts/updates, Change Feed is likely the answer.
If the question asks about enabling the feature, the answer is that it is always enabled.
Use these heuristics to quickly eliminate distractors.
Change Feed captures inserts and updates only; deletes are not captured.
Changes are retained for a maximum of 10 minutes (600 seconds).
Change Feed is enabled by default for all containers.
Ordering is guaranteed only within a physical partition, not globally.
Multiple updates to the same document within the retention window yield only the final state.
Consumption models: push (Azure Functions trigger) and pull (SDK).
Lease container is required for push model to track state across consumers.
Change Feed consumes RUs; cost depends on number and size of changes.
Cannot read the Change Feed for a specific partition key directly; must read all partitions.
Integrates with Azure Functions, Stream Analytics, Logic Apps, and Data Factory.
These come up on the exam all the time. Here's how to tell them apart.
Change Feed (Pull Model)
Manual control over polling interval and checkpointing.
Requires custom code to manage continuation tokens across partitions.
No lease container needed; you store tokens yourself.
More complex to scale across multiple consumers.
Lower latency if you poll frequently, but can miss changes if polling interval exceeds 10 minutes.
Change Feed (Push Model / Azure Functions)
Automatic scaling across partitions using leases.
Built-in checkpointing via lease container.
Requires a lease container with sufficient RU/s.
Simpler to set up for event-driven scenarios.
Azure Functions trigger handles failures and retries automatically.
Mistake
The Change Feed captures document deletes.
Correct
The Change Feed only captures inserts and updates. Deletes are not recorded. To capture deletes, you must use a soft-delete pattern (e.g., set a 'deleted' flag to true) and then detect the update via Change Feed.
Mistake
The Change Feed retains changes indefinitely.
Correct
The Change Feed retains changes for a maximum of 10 minutes (600 seconds). After that, the changes are no longer available. This is a fixed value and cannot be configured.
Mistake
The Change Feed provides globally ordered changes across all partitions.
Correct
Changes are ordered within each physical partition by commit timestamp, but there is no global order across partitions. If you need global ordering, you must use a single partition or another solution like Event Hubs.
Mistake
You must enable the Change Feed per container.
Correct
The Change Feed is enabled by default for all containers. No configuration is required to enable it. You only need to use the appropriate SDK or integration to consume it.
Mistake
The Change Feed captures every intermediate update to a document.
Correct
If a document is updated multiple times within the 10-minute retention window, only the latest version is included. The Change Feed does not capture each individual update; it captures the final state.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
The Change Feed retains changes for up to 10 minutes (600 seconds). After that, the changes are no longer available and cannot be read. This is a fixed retention period that cannot be changed. On the exam, remember that it is 10 minutes, not 1 hour or 24 hours.
No, the Change Feed does not capture deletes. It only captures inserts and updates. If you need to track deletes, you must implement a soft-delete pattern by adding a 'deleted' flag to your documents and updating it when you delete. Then the Change Feed will capture that update.
No, the Change Feed is scoped to the entire container. You cannot filter by partition key when reading the Change Feed. To process changes for a specific partition key, you would need to read all changes and filter them in your application, or use multiple consumers each assigned to a specific partition key range using the Change Feed Processor library.
The push model uses Azure Functions or the Change Feed Processor library to automatically receive changes via a trigger. It manages leases and checkpointing for you. The pull model uses the Cosmos DB SDK to manually read the Change Feed by calling `GetChangeFeedIterator` and `ReadNextAsync`. You control the polling interval and manage continuation tokens yourself. The push model is simpler for event-driven architectures, while the pull model offers more control.
No, the Change Feed is always enabled on all containers. You cannot disable it. However, you can choose not to consume it. The Change Feed does not consume any RU/s unless you read it. So there is no cost associated with having it enabled.
No, the Change Feed only captures the latest state of the document within the 10-minute retention window. If a document is updated multiple times, only the final version after the last update is included. Intermediate updates are not recorded. If you need to capture every change, you must use a different pattern, such as storing each change as a new document in an append-only container.
The Change Feed is designed to scale with your container's partitions. Each physical partition maintains its own change log. When consuming the Change Feed, you can use the Change Feed Processor library or Azure Functions trigger to automatically distribute processing across multiple consumers, each handling a subset of partitions. The lease container tracks which consumer is processing which partition. If a consumer fails, its lease is released and another consumer picks it up. For high throughput, ensure your lease container has sufficient RU/s to avoid throttling.
You've just covered Cosmos DB Change Feed for Streaming — now see how well it sticks with free DP-900 practice questions. Full explanations included, no account needed.
Done with this chapter?