Change Data Capture in Analytical Store
Complete the full lesson to earn 25 points — 50 with Pro
Work through each section, then tap “Mark as Complete” on the last one.
✦ Skip the page breaks, the wait, and see fewer ads — read each lesson on a single page with Pro
Module: Integrate Azure Cosmos DB Solution
Section: Analytical Workloads
Lesson: Change Data Capture in Analytical Store
Introduction: Why Change Data Capture Matters
In the world of modern data engineering, the ability to react to data changes in real-time is no longer a luxury; it is a fundamental requirement. Azure Cosmos DB is a globally distributed, multi-model database service designed to provide low-latency access to data at any scale. However, as organizations grow, they often find that the operational data stored in Cosmos DB needs to be surfaced in analytical environments—such as data lakes, warehouses, or specialized machine learning pipelines—without impacting the performance of the primary production workload.
This is where the concept of Change Data Capture (CDC) comes into play. CDC is a software design pattern that monitors and captures the changes applied to a database, ensuring that those changes are available for downstream processing. When we combine this with the Azure Cosmos DB Analytical Store, we gain the ability to perform high-performance analytical queries on near-real-time data while keeping the transactional store isolated from heavy compute tasks. Understanding how to orchestrate CDC for the Analytical Store allows you to build systems that are both responsive to user inputs and capable of providing deep, complex insights.
In this lesson, we will explore the mechanisms behind Cosmos DB's change tracking, specifically focusing on how to integrate the Analytical Store into your data architecture. We will move beyond the basics to discuss implementation patterns, performance considerations, and the best ways to keep your analytical insights fresh without compromising your transactional database’s throughput.
Understanding the Architecture: Transactional vs. Analytical Store
Before diving into the mechanics of CDC, it is essential to distinguish between the two stores within Cosmos DB. By default, Cosmos DB uses a row-oriented transactional store, which is optimized for high-speed writes and point lookups. When you enable the Analytical Store, Cosmos DB automatically creates a separate, column-oriented store that is optimized for large-scale analytical queries.
The Analytical Store is automatically synchronized with the transactional store. This synchronization is transparent, managed by the platform, and does not consume any of your provisioned Request Units (RUs). Because the analytical store is column-oriented, it is significantly more efficient for operations like aggregations, filtering, and scanning large datasets, which would be prohibitively expensive in the transactional store.
Callout: The "Sync" Distinction It is important to clarify that the "Change Feed" in Cosmos DB is primarily associated with the transactional store. When we talk about "CDC in the Analytical Store," we are referring to how we move data from the analytical store into other analytical systems (like Synapse or Fabric) or how we monitor changes that have been persisted to that store. The analytical store itself acts as the source of truth for your long-term analytical queries.
Key Differences Table
| Feature | Transactional Store | Analytical Store |
|---|---|---|
| Storage Format | Row-based | Column-based |
| Primary Use Case | OLTP (Reads/Writes) | OLAP (Analytics/Reporting) |
| Query Performance | Fast for single records | Fast for aggregations/scans |
| Consistency | Strong/Bounded Staleness | Eventual (Sync delay) |
| Cost Model | Based on RUs | Based on storage and analytical operations |
Implementing Change Data Capture via Change Feed
While the analytical store is excellent for querying, there are scenarios where you need to trigger external processes based on data modifications. The standard mechanism for this in Cosmos DB is the Change Feed. The Change Feed provides a sorted list of documents within a container in the order in which they were modified.
Even when you have an analytical store enabled, the Change Feed remains the primary mechanism for capturing "events" or "deltas" in your data. You can think of the Change Feed as the "trigger" for your analytical pipeline, while the Analytical Store acts as the "repository" for the actual data analysis.
Step-by-Step: Enabling and Consuming the Change Feed
- Enable the Feed: The Change Feed is enabled by default for all Cosmos DB accounts. You do not need to perform any configuration to turn it on; you simply need to write code to consume it.
- Choose a Processor: The most common way to consume the Change Feed is through the
ChangeFeedProcessorlibrary, which is available in the Azure Cosmos DB .NET, Java, and Python SDKs. - Set up a Lease Container: The processor needs a place to store state information—specifically, the "checkpoints" that track how much of the feed has been processed. You will need to create a dedicated container in your Cosmos DB account to act as this lease store.
Tip: Lease Container Sizing Always create a dedicated container for your leases. Do not use your production container for storing lease documents. The lease container can be very small (400 RUs is usually sufficient) because the state data is minimal.
Code Example: Using the Change Feed Processor (C#)
The following example demonstrates how to initialize a Change Feed processor to listen for changes in your container:
// Define the container and the processor
Container leaseContainer = database.GetContainer("LeaseContainer");
Container monitoredContainer = database.GetContainer("DataContainer");
ChangeFeedProcessor processor = monitoredContainer.GetChangeFeedProcessorBuilder<MyDataModel>(
processorName: "AnalyticalProcessor",
onChangesDelegate: HandleChangesAsync)
.WithInstanceName("hostName")
.WithLeaseContainer(leaseContainer)
.Build();
await processor.StartAsync();
// The delegate method to process changes
static async Task HandleChangesAsync(
IReadOnlyCollection<MyDataModel> changes,
CancellationToken cancellationToken)
{
foreach (var item in changes)
{
// Logic to push to analytical storage or trigger a pipeline
Console.WriteLine($"Detected change in record: {item.Id}");
}
}
In this code, the HandleChangesAsync method is triggered whenever a document is inserted or updated. This allows you to perform real-time data transformations or push notifications to external analytical systems like Azure Data Lake Storage (ADLS).
Leveraging Azure Synapse Link for Analytical Workloads
The most powerful way to perform CDC and analytical work with Cosmos DB is through Azure Synapse Link. Synapse Link provides a cloud-native, hybrid transactional and analytical processing (HTAP) capability. It allows you to run near-real-time analytics over your operational data in Cosmos DB without copying data or impacting your transactional performance.
When you enable Synapse Link, the analytical store is automatically populated. You can then connect your Azure Synapse Analytics workspace or Microsoft Fabric to this store. This effectively eliminates the need for complex ETL pipelines that were traditionally required to move data from the database to a warehouse.
How Synapse Link Handles Changes
Since the Analytical Store is updated automatically by the Cosmos DB engine, any change that hits the transactional store is eventually projected into the analytical store. This is a "push" model managed by the system. If you need to perform CDC in this context, you are essentially querying the "delta" between the current state of the analytical store and the previous state.
- Analytical Querying: Use SQL or Spark in Synapse/Fabric to query the analytical store.
- Filtering by Timestamp: Since every record in the analytical store includes metadata, you can filter your queries by the
_ts(timestamp) field to retrieve only records modified since your last run.
Code Example: Querying the Analytical Store (Spark)
# Reading from the Cosmos DB Analytical Store using Spark
cosmos_df = spark.read.format("cosmos.olap") \
.option("spark.cosmos.container", "YourContainer") \
.load()
# Filtering for recent changes
recent_changes = cosmos_df.filter(cosmos_df._ts > last_processed_timestamp)
# Perform analysis
recent_changes.createOrReplaceTempView("recent_data")
spark.sql("SELECT category, COUNT(*) FROM recent_data GROUP BY category").show()
This approach is highly efficient because the Spark engine pushes down the predicates to the analytical store, ensuring that only the relevant data is processed.
Best Practices for Analytical Workloads
When designing systems that integrate CDC with analytical stores, it is easy to fall into traps that lead to performance degradation or data consistency issues. Follow these industry-standard best practices to maintain a robust system.
1. Decouple Transactional and Analytical Logic
Never attempt to perform complex analytical aggregations within the same function that processes your Change Feed. If you have a high volume of changes, an expensive operation inside your HandleChangesAsync method will cause the processor to lag. Instead, use the Change Feed to move data into an intermediate storage layer, such as an Event Hub or a raw landing zone in ADLS, and let a separate Spark job handle the heavy lifting.
2. Manage the Analytical Store TTL
The analytical store supports Time-to-Live (TTL) settings that are separate from the transactional store. You can keep your transactional data for a short period (e.g., 30 days) while keeping your analytical data for years. Configure these settings based on your compliance and business requirements to optimize storage costs.
3. Monitor "Sync Lag"
While the synchronization between the transactional and analytical store is usually very fast (typically under a minute), it is not instantaneous. If your business logic requires absolute real-time consistency, you must query the transactional store. If you can tolerate a small delay, use the analytical store. Always monitor the AnalyticalStoreSyncLag metric in the Azure Portal to ensure your analytical insights are staying within your required latency SLAs.
Warning: Schema Evolution The analytical store is schema-agnostic, which means it adapts to changes in your JSON structure. However, if your application changes data types (e.g., changing a field from a string to an integer), the analytical store might experience issues projecting that field. Always validate schema changes in a development environment before deploying to production.
Common Pitfalls and How to Avoid Them
Even with the best planning, engineers often run into specific challenges when integrating Cosmos DB with analytical workloads. Let’s address the most common ones.
Pitfall 1: Over-Processing the Change Feed
A common mistake is using the Change Feed to perform secondary writes back into Cosmos DB. This can create "write amplification," where one update triggers another update, which in turn triggers another change feed event. This can quickly consume all of your RUs and lead to a performance bottleneck.
- The Fix: Always use the Change Feed for unidirectional data flow (out of the database into an analytical store or external system).
Pitfall 2: Ignoring Partitioning in Analytical Queries
Just because the analytical store is column-oriented doesn't mean you can ignore partition keys. If you query the analytical store without filtering by partition key, you may trigger a "full table scan" of the analytical store, which is expensive and slow.
- The Fix: Always include the partition key in your
WHEREclauses whenever possible, even in your analytical queries.
Pitfall 3: Assuming Immediate Consistency in Analytics
Some developers assume that because they have updated a record, it is immediately available in the analytical store. This leads to "missing data" bugs in reporting dashboards.
- The Fix: Build your applications to handle "eventual consistency." If a report is generated, inform the user of the "data freshness" (e.g., "Data last updated 2 minutes ago").
Advanced Configuration: Fine-Tuning the Analytical Store
To truly master the analytical store, you must understand how to configure the schema projection. By default, Cosmos DB projects all properties into the analytical store. However, for large datasets, this can lead to bloated storage.
Customizing Schema Projection
You can define a "schema-defined" analytical store. This allows you to explicitly choose which properties are included in the analytical store. This reduces the storage footprint and improves query performance by ignoring unnecessary fields.
- Define the Schema: In your container settings, you can specify an "Analytical Storage Schema" that acts as a filter.
- Impact: Only the fields defined in this schema will be projected. If you add new properties to your documents that are not in the schema, they will be ignored by the analytical store.
Callout: Why Schema Projection Matters In large-scale analytical systems, "storage bloat" is a hidden cost. By projecting only the fields you actually need for your reports (like ID, Timestamp, and specific metrics), you can reduce the amount of data the engine has to scan, leading to faster query times and lower costs.
Comparison: When to use which tool?
Depending on your specific analytical requirements, you might choose different tools to consume the data.
| Tool | Best For | Complexity |
|---|---|---|
| Azure Synapse Link | Ad-hoc analytics, BI reporting | Low |
| Azure Data Factory | Batch ETL, moving data to SQL DW | Medium |
| Change Feed + Azure Functions | Real-time alerts, simple transformations | Medium |
| Spark on Cosmos DB | Complex data science, machine learning | High |
Step-by-Step: Setting Up a Real-Time Analytical Pipeline
If you want to build a robust, end-to-end analytical pipeline, follow these steps:
- Provisioning: Create a Cosmos DB account with both Transactional and Analytical stores enabled. Ensure you have a Synapse Workspace linked to this account.
- Data Ingestion: Use an application to write data into the transactional store.
- Analytical View: In the Synapse Workspace, create a "Linked Service" to the Cosmos DB account.
- Data Exploration: Open a Notebook in Synapse and run a SQL or Spark query against the analytical store.
- CDC Trigger: If you need to trigger a downstream alert, implement a
ChangeFeedProcessorthat sends a message to an Azure Service Bus, which then triggers an Azure Function to send an email or log the event. - Monitoring: Use Azure Monitor to set up alerts on the
AnalyticalStoreSyncLagmetric.
This architecture ensures that your transactional workload remains fast and responsive while your analytical users get the data they need, when they need it, without causing system contention.
Key Takeaways
As we conclude this lesson, remember the following core principles of integrating Cosmos DB into your analytical architecture:
- Separation of Concerns: Always keep your transactional and analytical operations distinct. Use the Transactional Store for application logic and the Analytical Store for reporting and data science.
- The Role of the Change Feed: Use the Change Feed as the primary tool for real-time event-driven tasks, but avoid using it as a primary mechanism for large-scale data movement if Synapse Link can handle the task natively.
- Leverage Synapse Link: This is the industry-standard way to bridge the gap between operational data and analytical insights. It avoids the "ETL nightmare" by keeping the data in place.
- Performance Awareness: Always monitor your RUs for transactional operations and your sync lag for analytical operations. Proactive monitoring prevents performance degradation before it impacts your users.
- Schema Strategy: Be intentional about what data you move into the analytical store. Using schema projection can significantly reduce your costs and increase the speed of your analytical queries.
- Eventual Consistency is OK: Accept that analytical data is not always "real-time" in the transactional sense. Design your reports and dashboards to be transparent about the data's age or freshness.
- Scalability First: Both Cosmos DB and Synapse are built to scale. When designing your pipelines, ensure you are using distributed patterns (like Spark) rather than single-threaded scripts to handle large volumes of data changes.
By mastering these concepts, you are not just managing a database; you are architecting a data ecosystem that is capable of evolving with the needs of your organization. Whether you are building a real-time dashboard or a complex machine learning model, the integration of Cosmos DB's analytical store and change tracking mechanisms will serve as the backbone of your success.
FAQ: Common Questions
Q: Does enabling the Analytical Store cost extra? A: Yes, you pay for the storage used in the analytical store and for the operations performed on that data. However, since it is column-oriented, many analytical queries are significantly cheaper to run in the analytical store than they would be in the transactional store.
Q: Can I use the Change Feed to replicate data to another database? A: Yes, this is a very common pattern. You can use the Change Feed to stream data into an Azure SQL Database or even another Cosmos DB container. Just be sure to handle retries and idempotency in your code.
Q: How do I know if my analytical store is "synced" with my transactional store?
A: Check the AnalyticalStoreSyncLag metric in the Azure Portal. This metric tells you how many milliseconds (or seconds) the analytical store is behind the transactional store.
Q: Is the analytical store limited in size? A: No, the analytical store scales automatically with your data. There is no hard limit on the amount of data you can store in the analytical store.
Q: Can I query the analytical store using the Cosmos DB SDK? A: No, the analytical store is designed for analytical tools like Synapse, Fabric, or Spark. Use the standard Cosmos DB SDK only for accessing the transactional store.
Reach the last section to complete this lesson and earn points — you're on section 1 of 10.
- Introduction to Cosmos DB Data Modeling
- Introduction to Cosmos DB Data Modeling Quiz5q
- Multiple Entity Types in Same Container
- Multiple Entity Types in Same Container Quiz5q
- Storing Related Entities in Same Document
- Storing Related Entities in Same Document Quiz5q
- Denormalizing Data Across Documents
- Denormalizing Data Across Documents Quiz5q
- Referencing Between Documents
- Referencing Between Documents Quiz5q
- Partition Keys and Document IDs
- Partition Keys and Document IDs Quiz5q
- Time to Live (TTL) Configuration
- Time to Live (TTL) Configuration Quiz5q
- Document Versioning Strategies
- Document Versioning Strategies Quiz5q
- Schema Versioning Patterns
- Schema Versioning Patterns Quiz5q
- Choosing Partition Strategies
- Choosing Partition Strategies Quiz5q
- Partition Key Selection Best Practices
- Partition Key Selection Best Practices Quiz5q
- Transactions and Partition Keys
- Transactions and Partition Keys Quiz5q
- Cross-Partition Query Costs
- Cross-Partition Query Costs Quiz5q
- Data Distribution Analysis
- Data Distribution Analysis Quiz5q
- Throughput Distribution Planning
- Throughput Distribution Planning Quiz5q
- Synthetic Partition Keys
- Synthetic Partition Keys Quiz5q
- Hierarchical Partition Keys
- Hierarchical Partition Keys Quiz5q
- Throughput and Storage Requirements
- Throughput and Storage Requirements Quiz5q
- Serverless vs Provisioned Throughput
- Serverless vs Provisioned Throughput Quiz5q
- Database-Level Provisioned Throughput
- Database-Level Provisioned Throughput Quiz5q
- Granular Scale Units
- Granular Scale Units Quiz5q
- Global Distribution Costs
- Global Distribution Costs Quiz5q
- Configuring Throughput in Portal
- Configuring Throughput in Portal Quiz5q
- Gateway vs Direct Connectivity Mode
- Gateway vs Direct Connectivity Mode Quiz5q
- Creating Database Connections
- Creating Database Connections Quiz5q
- Azure Cosmos DB Emulator
- Azure Cosmos DB Emulator Quiz5q
- Connection Error Handling
- Connection Error Handling Quiz5q
- Singleton Pattern for Clients
- Singleton Pattern for Clients Quiz5q
- Global Distribution Regions
- Global Distribution Regions Quiz5q
- Threading and Parallelism
- Threading and Parallelism Quiz5q
- Arrays and Nested Objects Queries
- Arrays and Nested Objects Queries Quiz5q
- Correlated Subqueries
- Correlated Subqueries Quiz5q
- Array and Type-Checking Functions
- Array and Type-Checking Functions Quiz5q
- Mathematical and String Functions
- Mathematical and String Functions Quiz5q
- Date Functions in Queries
- Date Functions in Queries Quiz5q
- Point Operations vs Query Operations
- Point Operations vs Query Operations Quiz5q
- CRUD Point Operations
- CRUD Point Operations Quiz5q
- Patch Operations for Updates
- Patch Operations for Updates Quiz5q
- Transactional Batch Operations
- Transactional Batch Operations Quiz5q
- Bulk Operations with SDK
- Bulk Operations with SDK Quiz5q
- Optimistic Concurrency with ETags
- Optimistic Concurrency with ETags Quiz5q
- Query Pagination and Continuation
- Query Pagination and Continuation Quiz5q
- Cosmos DB Mirroring for Fabric
- Cosmos DB Mirroring for Fabric Quiz5q
- Mirroring vs Spark Connector
- Mirroring vs Spark Connector Quiz5q
- Enabling Analytical Store
- Enabling Analytical Store Quiz5q
- Synapse Spark and SQL Queries
- Synapse Spark and SQL Queries Quiz5q
- Change Data Capture in Analytical Store
- Change Data Capture in Analytical Store Quiz5q
- Azure Functions and Event Hubs Integration
- Azure Functions and Event Hubs Integration Quiz5q
- Denormalization with Change Feed
- Denormalization with Change Feed Quiz5q
- Referential Integrity with Change Feed
- Referential Integrity with Change Feed Quiz5q
- Azure AI Search Integration
- Azure AI Search Integration Quiz5q
- Azure Functions Change Feed Trigger
- Azure Functions Change Feed Trigger Quiz5q
- Consuming Change Feed with SDK
- Consuming Change Feed with SDK Quiz5q
- Change Feed Estimator
- Change Feed Estimator Quiz5q
- Denormalization via Change Feed
- Denormalization via Change Feed Quiz5q
- Aggregation Persistence with Change Feed
- Aggregation Persistence with Change Feed Quiz5q
- Read-Heavy vs Write-Heavy Indexing
- Read-Heavy vs Write-Heavy Indexing Quiz5q
- Index Type Selection
- Index Type Selection Quiz5q
- Custom Indexing Policies
- Custom Indexing Policies Quiz5q
- Composite Index Implementation
- Composite Index Implementation Quiz5q
- Index Performance Optimization
- Index Performance Optimization Quiz5q
- Response Status Codes and Metrics
- Response Status Codes and Metrics Quiz5q
- Normalized RU Consumption Monitoring
- Normalized RU Consumption Monitoring Quiz5q
- Server-Side Latency Metrics
- Server-Side Latency Metrics Quiz5q
- Data Replication Monitoring
- Data Replication Monitoring Quiz5q
- Azure Monitor Alerts Configuration
- Azure Monitor Alerts Configuration Quiz5q
- Resource Logs Implementation
- Resource Logs Implementation Quiz5q
- Partition Throughput Monitoring
- Partition Throughput Monitoring Quiz5q
- Encryption Key Management
- Encryption Key Management Quiz5q
- Network-Level Access Control
- Network-Level Access Control Quiz5q
- Data Encryption Configuration
- Data Encryption Configuration Quiz5q
- Azure RBAC for Control Plane
- Azure RBAC for Control Plane Quiz5q
- Microsoft Entra ID for Data Plane
- Microsoft Entra ID for Data Plane Quiz5q
- CORS Settings Configuration
- CORS Settings Configuration Quiz5q
- Customer-Managed Keys
- Customer-Managed Keys Quiz5q
- Always Encrypted Implementation
- Always Encrypted Implementation Quiz5q
- Data Movement Strategy Selection
- Data Movement Strategy Selection Quiz5q
- SDK Bulk Operations for Data Movement
- SDK Bulk Operations for Data Movement Quiz5q
- Azure Data Factory Pipelines
- Azure Data Factory Pipelines Quiz5q
- Kafka Connector Integration
- Kafka Connector Integration Quiz5q
- Azure Stream Analytics Integration
- Azure Stream Analytics Integration Quiz5q
- Cosmos DB Spark Connector
- Cosmos DB Spark Connector Quiz5q
Enjoying the courses?
Everything stays free. Pro shows fewer ads, doubles the points you earn on every lesson and quiz so you progress twice as fast, unlocks half of every practice exam — plus full case studies — with the Learn & Exam study modes, and lets you read each lesson on one page.
- ✓ Fewer advertisements
- ✓ 2× points per lesson & quiz
- ✓ 50% of every exam unlocked
- ✓ Learn & Exam modes
- ✓ Distraction-free lessons