Cosmos DB Mirroring for Fabric
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
Mastering Cosmos DB Mirroring for Microsoft Fabric
Introduction: Bridging the Gap Between Operational and Analytical Data
In the modern data landscape, organizations often struggle with the "data silo" problem. You have your operational data—the information that powers your applications, websites, and services—residing in a high-performance database like Azure Cosmos DB. Simultaneously, you have your analytical data—the information used for business intelligence, machine learning, and trend analysis—residing in a data warehouse or data lake. Historically, moving data between these two environments required complex Extract, Transform, Load (ETL) pipelines that were prone to latency, maintenance overhead, and data consistency issues.
Cosmos DB Mirroring for Microsoft Fabric represents a fundamental shift in how we handle this architecture. Instead of building and managing custom pipelines to move data from your operational NoSQL store into your analytical platform, mirroring creates a near real-time, read-only replica of your Cosmos DB data directly within the Microsoft Fabric OneLake environment. This allows you to perform heavy analytical queries, generate Power BI reports, and train machine learning models on your operational data without ever impacting the performance of your production Cosmos DB instance.
This lesson explores the mechanics of mirroring, how to configure it, the architectural benefits it provides, and the best practices for ensuring your data remains accurate and performant. By mastering this integration, you enable your organization to move from a reactive data strategy—where analytical reports are always hours or days behind—to a proactive strategy where insights are derived from the most current state of your business.
Understanding the Architecture: How Mirroring Works
To understand why mirroring is significant, we must first look at the traditional approach. In a traditional setup, you might use Azure Data Factory or a custom Spark job to pull data from Cosmos DB, flatten the JSON documents, and store them in a Parquet format in an Azure Data Lake Storage (ADLS) Gen2 account. This process adds latency, consumes Request Units (RUs) on your Cosmos DB container, and requires you to manage the schema evolution of your NoSQL documents as they transition into a tabular format.
Mirroring changes this by leveraging the underlying infrastructure of both Azure and Fabric. When you enable mirroring on a Cosmos DB container, the system automatically handles the change feed from the source database. It captures changes in the JSON documents and translates them into a Delta Lake format within your Fabric OneLake. Because this process is managed by the platform, you do not need to write code to handle incremental loads or manage schema drift.
Key Components of the Mirroring Ecosystem
- The Source (Cosmos DB): Your operational NoSQL database. It continues to serve your application traffic with the same performance expectations as before.
- The Change Feed: The mechanism that powers the mirroring. Cosmos DB maintains a persistent record of changes to documents, which the mirroring service consumes.
- Fabric OneLake: The unified, logical data lake for your organization. The mirrored data resides here as Delta tables, making it immediately available to the Fabric compute engines.
- The Compute Engines: Once the data is in OneLake, you can query it using T-SQL via the SQL Analytics Endpoint, or use Spark notebooks to perform complex data science tasks.
Callout: Mirroring vs. Traditional ETL In a traditional ETL process, you are responsible for the code, the scheduling, the monitoring, and the error handling of data movement. You also consume RUs on your Cosmos DB instance every time you perform a full or incremental scan. With mirroring, the synchronization is handled as a platform service. It is designed to be low-impact on your operational database while ensuring that the analytical data remains synchronized with near-zero latency.
Configuring Cosmos DB Mirroring: A Step-by-Step Guide
Setting up mirroring is a straightforward process, but it requires specific prerequisites. Before you begin, ensure you have an Azure Cosmos DB account (specifically the NoSQL API) and a Microsoft Fabric workspace with the appropriate permissions.
Step 1: Prepare the Cosmos DB Account
Mirroring requires that your Cosmos DB account is properly configured for change feed access. While most modern Cosmos DB accounts support this by default, you should verify that your account is not restricted by network policies that might block the Fabric service. Ensure that your firewall settings allow access from the Fabric service, or use Private Links if your organizational security policy requires it.
Step 2: Create the Mirrored Database in Fabric
- Navigate to your Microsoft Fabric workspace.
- Select New and then choose Mirrored Azure Cosmos DB.
- Provide a name for the mirrored database.
- When prompted, select the existing Azure subscription and the specific Cosmos DB account you wish to mirror.
- Select the containers you want to include in the mirroring process.
Step 3: Monitoring the Initial Sync
Once configured, the system initiates an initial synchronization. This is the process of copying the existing data from your Cosmos DB container into the OneLake Delta tables. Depending on the size of your data, this can take anywhere from a few minutes to several hours. You can monitor the progress through the Fabric portal in the "Mirroring" tab of your database object.
Note: The initial synchronization consumes RUs from your Cosmos DB container. If your container is heavily loaded, consider performing the initial sync during off-peak hours to avoid impacting your application performance. Once the initial sync is complete, the ongoing synchronization process uses a significantly smaller amount of resources.
Working with Mirrored Data in Fabric
Once the data has arrived in OneLake, it is transformed into a Delta Lake format. This is the "magic" of the integration. Because the data is now in Delta Lake, you gain all the benefits of the format, including ACID transactions, versioning (time travel), and high-performance indexing.
Querying with SQL Analytics Endpoint
Every mirrored database in Fabric comes with a SQL Analytics Endpoint. This provides a familiar T-SQL interface to query your data. You can treat your mirrored Cosmos DB data just like a standard SQL table.
-- Example: Querying the mirrored data
SELECT
customer_id,
COUNT(order_id) as total_orders,
SUM(order_total) as total_spent
FROM
dbo.Orders
WHERE
order_date >= '2023-01-01'
GROUP BY
customer_id
ORDER BY
total_spent DESC;
This query runs against the Delta tables in OneLake. Because the SQL Analytics Endpoint is optimized for analytical workloads, you can perform joins, aggregations, and window functions that would be prohibitively expensive or impossible to run directly inside a NoSQL environment.
Using Spark for Advanced Analysis
For more complex tasks, such as machine learning or heavy data cleaning, you can use Fabric Spark notebooks. Since the data is in Delta format, you can easily load it into a Spark DataFrame.
# Loading mirrored data into a Spark DataFrame
df = spark.read.format("delta").table("Orders")
# Perform data science operations
from pyspark.sql.functions import col
# Filter and transform
clean_df = df.filter(col("status") == "completed")
# Display the results
display(clean_df.limit(10))
This approach allows data scientists to work in their preferred environment while accessing the most up-to-date information from the operational database.
Best Practices for Performance and Cost Management
While mirroring simplifies architecture, you still need to follow best practices to ensure your solution remains efficient.
1. Optimize Your Source Schema
Cosmos DB is schema-agnostic, meaning you can store documents with different structures in the same container. However, when these documents are mirrored into a tabular format, the system must infer a schema. If your documents are wildly inconsistent, the resulting Delta table might contain many columns with sparse data (many nulls), which can degrade query performance. Try to maintain a consistent schema for your documents wherever possible.
2. Monitor Request Unit Usage
While mirroring is designed to be efficient, it does still consume RUs. Keep an eye on your Cosmos DB metrics to ensure that the background synchronization process isn't causing unexpected throttling. If you notice high RU consumption during the ongoing sync phase, you may need to scale your container or optimize the indexing policy of your Cosmos DB container to reduce the cost of the change feed reads.
3. Use Partitioning Wisely
Mirroring respects the partitioning strategy of your Cosmos DB container. If your container is poorly partitioned (e.g., a "hot partition" where one partition key holds the majority of the data), the mirroring process might also struggle. Ensure your partition key is chosen based on a high-cardinality property that distributes data evenly across physical partitions.
Callout: Schema Evolution One of the most challenging aspects of NoSQL-to-SQL integration is schema evolution. When you add a new field to your JSON documents in Cosmos DB, the mirroring service will automatically detect the change and update the Delta table schema in OneLake. This "automatic evolution" is a major productivity booster, as it removes the need to manually update your analytical tables every time your application code changes.
4. Leverage Time Travel
Because the data is stored in Delta Lake, you have access to "Time Travel." You can query the data as it existed at a specific point in time. This is invaluable for auditing or for comparing the current state of your data with its state a week ago.
-- Querying data as it existed in the past
SELECT * FROM Orders
FOR SYSTEM_TIME AS OF '2023-10-01 12:00:00';
Common Pitfalls and How to Avoid Them
Even with a well-designed system, there are common mistakes that can lead to performance issues or data inconsistencies.
Ignoring Data Types
Cosmos DB handles types differently than SQL. For example, a number might be stored as an integer in one document and a string in another. When mirroring, the system attempts to coerce these into a consistent type. If the data is too messy, the mirroring might fail or produce unexpected results. Always validate your data at the application level before it reaches the database.
Over-Querying the SQL Endpoint
The SQL Analytics Endpoint is not a substitute for an operational database. Do not point your high-traffic, low-latency application queries to the SQL Analytics Endpoint. Those queries should stay in the Cosmos DB SDK. The SQL Analytics Endpoint is intended for analytical queries, reporting, and dashboarding, which typically have different performance profiles.
Neglecting Security and Access Control
Because mirrored data is stored in OneLake, it is subject to Fabric's security model. Ensure that you have properly configured Workspace roles and Item-level permissions. Don't simply grant "Admin" access to everyone. Use the principle of least privilege to ensure that analysts can only see the data they need for their specific reports.
Misunderstanding Latency
While mirroring is "near real-time," it is not "instant." There may be a delay of a few seconds to a few minutes between a change occurring in Cosmos DB and it appearing in the Delta table. If your business requirements demand absolute millisecond consistency for analytical reports, you may need to reconsider your architecture, though for 99% of analytical use cases, this latency is perfectly acceptable.
Comparison: Traditional ETL vs. Fabric Mirroring
| Feature | Traditional ETL (ADF/Spark) | Cosmos DB Mirroring |
|---|---|---|
| Maintenance | High (pipelines, scheduling, error handling) | Low (platform-managed) |
| Latency | Batch-based (hours/days) | Near real-time (seconds/minutes) |
| Performance Impact | High (full/incremental scans) | Low (Change Feed optimized) |
| Schema Handling | Manual mapping/transformation | Automatic inference/evolution |
| Complexity | High (requires code & orchestration) | Low (configuration-based) |
Deep Dive: Handling Complex Data Types
One of the most frequent questions regarding mirroring involves how it handles complex JSON structures, such as nested arrays and objects.
Flattening Nested Structures
When you have a document with a nested array, the mirroring service will typically create a separate table for that array to maintain relational integrity. For example, if you have an Order document that contains a list of Items, you will end up with two tables: Orders and Orders_Items. You can then join these tables using the Order_ID to perform complex analysis.
Handling Large Documents
If you have extremely large JSON documents, ensure that you are not hitting the limits of the Delta Lake format. While Delta can handle large files, extremely large individual rows can lead to performance degradation during query time. If you find your documents are becoming massive, consider normalizing your data model within Cosmos DB before mirroring.
Managing Deleted Data
When a document is deleted in Cosmos DB, it is also removed from the mirrored Delta table. This is the desired behavior for keeping your analytical data in sync with your operational data. However, if you need to keep a history of "deleted" records for compliance reasons, you should implement a "soft delete" strategy in your application—where you set an is_deleted flag instead of physically deleting the document. The mirrored table will then capture that update, and you will retain the record for analytical purposes.
Advanced Scenarios: Integrating with Power BI
The primary goal for many organizations using mirroring is to feed data into Power BI. Because the data is now in a Delta table, you can connect Power BI directly to the Fabric SQL Analytics Endpoint.
- Open Power BI Desktop.
- Select Get Data > Azure > Azure SQL Database (or use the dedicated Fabric connector).
- Enter the SQL connection string provided by the Fabric SQL Analytics Endpoint.
- Choose the "Import" or "DirectQuery" mode.
Tip: Use DirectQuery mode if you have a massive dataset and need to ensure the report always reflects the absolute latest state of the mirrored data. Use Import mode if you need the fastest possible report performance and can tolerate the data being refreshed on a schedule.
By using DirectQuery, you are essentially creating a live connection from your Power BI dashboard all the way to the Delta tables in OneLake. This provides a very fluid experience for end-users who need to drill down into the latest operational data.
Troubleshooting Common Issues
If you find that your data isn't showing up as expected, follow these troubleshooting steps:
- Check the Mirroring Status: Go to the database object in Fabric and check the "Mirroring" tab. It will tell you if the synchronization is "Active," "Paused," or if there is an "Error."
- Verify Permissions: Ensure the service principal or the user account that configured the mirroring has the necessary permissions on the Cosmos DB account.
- Check the Change Feed: If you are comfortable with the Cosmos DB SDK, write a small script to verify that the change feed is indeed emitting events for the container you are mirroring.
- Review Fabric Logs: Use the Fabric monitoring tools to see if there are any system-level errors occurring during the ingestion process.
Key Takeaways
After completing this lesson, you should have a solid understanding of how to leverage Cosmos DB Mirroring for Microsoft Fabric to streamline your analytical workloads. Here are the most critical points to remember:
- Operational-Analytical Synergy: Mirroring removes the complexity of traditional ETL pipelines, allowing you to run analytical queries on operational data without interfering with application performance.
- Near Real-Time Insights: The platform-managed synchronization ensures that your analytical data in OneLake is consistently updated with minimal latency, moving your business away from batch-oriented reporting.
- Delta Lake Benefits: By converting NoSQL documents into Delta tables, you gain access to high-performance SQL querying, ACID transactions, and time-travel capabilities that are not available in the raw NoSQL environment.
- Platform-Managed Schema: The system automatically handles schema evolution, significantly reducing the maintenance burden when your application data structures change over time.
- Cost-Efficient Scaling: Mirroring is designed to be low-impact on your Cosmos DB Request Units, making it a sustainable solution for both small and large-scale data environments.
- Strategic Architecture: Always remember that the SQL Analytics Endpoint is for analytical workloads, not for replacing your application's connection to the Cosmos DB SDK.
- Security First: Use Fabric's robust security model to control access to your mirrored data, ensuring that your organization's sensitive operational data remains protected while being available for analysis.
By implementing these strategies, you are not just setting up a database integration; you are building a modern, responsive data architecture that empowers your organization to make better decisions based on the current state of your operations. This foundational knowledge will serve as a building block for more complex data integration and machine learning projects within the Microsoft Fabric ecosystem.
Reach the last section to complete this lesson and earn points — you're on section 1 of 9.
- 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