Enabling Analytical Store

Earn 25 points (50 with Pro) in two steps

  1. ① Read through the lesson — each section gets a ✓ as you scroll through it.
  2. ② When every section has a ✓, tap Complete lesson.

0 of 10 read · keep scrolling

✦ See fewer ads and earn double points — 50 a lesson instead of 25 — with Pro

Enabling and Managing the Azure Cosmos DB Analytical Store

Introduction to Analytical Workloads in Cosmos DB

In the world of modern data engineering, we often find ourselves caught between two competing needs: the requirement for fast, transactional updates to our data and the necessity to perform deep, complex analytics on that same data. Traditionally, this meant building complex extract-transform-load (ETL) pipelines to move data from a transactional database into a separate data warehouse. This process is not only time-consuming but also creates data latency, where your analytics are always reflecting the state of the world from several hours or even days ago.

Azure Cosmos DB addresses this fundamental challenge through the Analytical Store. The Analytical Store is a fully isolated, column-oriented storage layer that allows you to perform large-scale analytics on your operational data without impacting the performance of your transactional workloads. By enabling this feature, you essentially bridge the gap between operational databases and analytical engines like Azure Synapse Analytics or Azure Databricks. Understanding how to enable and manage this store is critical for any engineer looking to build real-time reporting or machine learning pipelines directly on top of their operational data.

This lesson explores the mechanics of the Analytical Store, how to enable it, how it interacts with the transactional store, and the best practices for ensuring your analytical queries remain performant and cost-effective.

Not read yet

Understanding the Architecture: Transactional vs. Analytical

To understand the Analytical Store, we must first look at the Transactional Store. The Transactional Store is row-oriented and optimized for low-latency CRUD operations—creating, reading, updating, and deleting records. It is designed to handle high concurrency and provide predictable performance for individual record lookups or small batch updates.

The Analytical Store, by contrast, is column-oriented. Columnar storage is highly efficient for analytical queries that process large volumes of data, such as calculating averages, sums, or performing complex joins across thousands or millions of records. Because the data is stored by column rather than by row, the engine only needs to read the specific columns requested in a query, which significantly reduces the amount of I/O required.

Callout: Row-Oriented vs. Column-Oriented Storage The Transactional Store (Row-Oriented) is designed for writing and reading complete records. If you need to update a user's address or fetch a specific order by ID, the row-oriented structure ensures the operation is completed in milliseconds. The Analytical Store (Column-Oriented) is designed for aggregation. If you need to calculate the average sales price across a million transactions, the columnar store allows the system to scan only the 'Price' column, ignoring the user names, addresses, and other metadata, leading to massive performance gains for large-scale analytical tasks.

The Automatic Sync Process

One of the most powerful aspects of the Analytical Store is the automatic synchronization process. When you enable the Analytical Store for a container, Azure Cosmos DB automatically moves data from the transactional store to the analytical store. This happens in near real-time, typically within a minute of the transaction being committed. You do not need to write code to manage this movement; it is a managed service background process that is entirely transparent to your application.

Not read yet

Enabling the Analytical Store

Enabling the Analytical Store is a configuration task performed at the container level. It is important to note that this is a "set it and forget it" configuration; once enabled, all data inserted into that container is automatically replicated to the analytical layer.

Step-by-Step: Enabling via Azure Portal

  1. Navigate to your Azure Cosmos DB account in the Azure portal.
  2. Select Data Explorer from the left-hand menu.
  3. Select your database and the specific container you wish to configure.
  4. Click on Settings in the top menu of the container view.
  5. Look for the Analytical Store section.
  6. Toggle the switch to On.
  7. If you have not yet enabled the Synapse Link for your account, you will be prompted to do so. Synapse Link is the underlying integration that connects Cosmos DB to the analytical engines.
  8. Click Save to apply the changes.

Enabling via Azure CLI

If you prefer infrastructure-as-code or command-line tools, you can enable the Analytical Store during container creation or update an existing container.

# Update an existing container to enable Analytical Store
az cosmosdb sql container update \
    --resource-group MyResourceGroup \
    --account-name MyCosmosAccount \
    --database-name MyDatabase \
    --name MyContainer \
    --analytical-storage-ttl -1

Note: Setting the --analytical-storage-ttl to -1 means the data will be kept in the Analytical Store indefinitely. You can set this to a positive integer to define a time-to-live in seconds, after which the data will be purged from the Analytical Store automatically.

Not read yet

Working with Analytical Data: Synapse Link

Once the Analytical Store is enabled, you need a way to query it. This is where Azure Synapse Link comes into play. Synapse Link allows you to create a "Linked Service" in Azure Synapse Analytics that points to your Cosmos DB container.

Creating a Linked Service

Within your Synapse workspace, you navigate to the Manage tab, select Linked Services, and then click New. Choose Azure Cosmos DB (SQL API) as the source. You will provide your connection string and select the appropriate database. Once the link is established, you can query your data using T-SQL or Spark notebooks.

Querying with T-SQL (Serverless SQL Pool)

Once the link is created, you can write standard SQL queries against your Cosmos DB data. This is incredibly powerful because it allows data analysts who are comfortable with SQL to interact with NoSQL data without needing to learn the Cosmos DB SDK or partition key structures.

SELECT 
    category, 
    SUM(price) as TotalSales
FROM OPENROWSET(
    'CosmosDB',
    'account=my-cosmos-account;database=my-db;container=my-container',
    'SELECT * FROM c'
) AS [data]
GROUP BY category

In this example, the OPENROWSET function acts as the interface to the analytical store. The query engine automatically translates this SQL into an efficient scan of the columnar data, providing results in a fraction of the time it would take to iterate through the transactional store.

Not read yet

Best Practices for Analytical Workloads

While the Analytical Store is easy to enable, getting the most out of it requires understanding how to structure your data and how to manage your storage costs.

1. Data Modeling for Analytics

In the transactional store, we often use denormalization to optimize for read performance. For the analytical store, this remains a good strategy. Because the analytical store is columnar, having wide tables (many columns) is perfectly fine. You should aim to structure your documents so that fields frequently used for filtering or grouping are at the top level of your JSON documents.

2. Monitoring Analytical Storage Costs

The Analytical Store is billed separately from the Transactional Store. You are charged for the storage space used and for the read/write operations performed by the analytical engine. To keep costs down, use the Time-to-Live (TTL) feature to automatically expire data that is no longer needed for analytics. If you only need to run reports on the last 90 days of data, set your analytical TTL to 7,776,000 seconds.

3. Avoiding Common Pitfalls

One of the most common mistakes is attempting to run heavy analytical queries against the Transactional Store using the Cosmos DB SDK. This consumes Request Units (RUs) that are needed for your application's operational traffic. If your application starts experiencing latency or 429 (Too Many Requests) errors, it is a clear sign that you should be offloading those queries to the Analytical Store.

Warning: Never attempt to perform large-scale aggregations on the Transactional Store. Even if you have provisioned high RUs, the row-oriented nature of the store makes it inefficient for scanning millions of rows. Always use the Analytical Store for these types of operations to protect your operational application's performance.

4. Schema Evolution

Cosmos DB is schema-agnostic, which is great for flexibility. However, the Analytical Store needs to infer a schema from your JSON documents. If your documents have inconsistent structures—for example, one document has an integer price and another has a string price—the schema inference process might fail or create data type mismatches in the analytical layer. Ensure your application logic enforces a consistent schema for fields that you intend to analyze.

Not read yet

Comparison: Transactional vs. Analytical Store

The following table summarizes the key differences between the two storage layers to help you decide when to use which.

Feature Transactional Store Analytical Store
Data Format Row-oriented Column-oriented
Primary Use Case Real-time CRUD operations Large-scale aggregations, BI, ML
Performance Low-latency (ms) High-throughput (for scans)
Billing Provisioned RUs Storage + Analytical Read/Write units
Sync Mechanism Immediate Near real-time (background)
Schema Flexible Schema-on-read (inferred)

Implementing Advanced Analytical Patterns

Beyond simple aggregations, the Analytical Store is the backbone for sophisticated data pipelines. Let’s consider a scenario where you are running a retail platform and want to build a real-time dashboard that shows the most popular products in the last hour.

Pattern: The Lambda Architecture Evolution

In the past, you might have used a Kappa or Lambda architecture, involving complex stream processing like Apache Kafka or Azure Stream Analytics. With Cosmos DB Analytical Store, you can simplify this significantly. Your application writes orders to the transactional store. The analytical store automatically picks up these writes. Your Power BI dashboard or Synapse notebook queries the analytical store directly.

Spark Integration

For more advanced data science tasks, you can connect Azure Databricks or Synapse Spark pools to the Analytical Store. Using the Cosmos DB Spark connector, you can load your data into a DataFrame and apply machine learning models.

# Example of reading analytical store into a Spark DataFrame
df = spark.read \
    .format("cosmos.olap") \
    .option("spark.synapse.linkedService", "MyCosmosLink") \
    .option("spark.cosmos.container", "Orders") \
    .load()

# Perform a quick ML aggregation
from pyspark.sql.functions import avg
result = df.groupBy("product_id").agg(avg("price"))
result.show()

This code snippet demonstrates the simplicity of moving from raw operational data to a Spark-based analysis environment. The cosmos.olap format tells the Spark engine to specifically target the columnar analytical store, ensuring that the operation is performant and does not compete with your operational transactions.

Not read yet

Troubleshooting Analytical Sync Issues

Sometimes, you might notice that your analytical queries are not returning the most recent data. While the synchronization is "near real-time," there are factors that can influence this latency.

  • System Load: During periods of extremely high volume, the background sync process may experience a slight delay.
  • Container Configuration: Ensure that the Analytical Store is actually enabled on the container. You can verify this by checking the container settings in the portal.
  • Data Types: As mentioned earlier, inconsistent data types can sometimes cause issues with the analytical schema inference. Check your application logs to ensure that your data is being written in the expected format.
  • Synapse Link Status: Verify that your Synapse Link is healthy and that the credentials used for the Linked Service have not expired or been revoked.

Best Practices for Cost Management

Since Analytical Store billing is based on both storage volume and analytical read/write operations, you should be intentional about how you manage your data.

  1. Use Analytical TTL: As mentioned, this is the single most important setting for cost control. If you have years of historical data but only need to report on the last year, set a TTL of 31,536,000 seconds.
  2. Filter Aggressively: In your analytical queries, always include filters that restrict the scope of the data. For instance, if you are calculating monthly sales, filter by the month column in your SQL query. This reduces the number of Analytical Read units consumed.
  3. Project Only Necessary Columns: When using SELECT *, you may be pulling more data than you need. Explicitly naming the columns in your SELECT statement helps the analytical engine optimize its I/O.
  4. Partitioning Strategy: While the analytical store handles its own partitioning, your choice of partition key in the transactional store still influences how data is distributed. A good partition key that spreads data evenly will also help the analytical store perform more efficiently.

Not read yet

Practical Example: Building an Inventory Alert System

Let's imagine you are building an inventory management system. Your goal is to trigger an alert if the stock level of any product falls below a certain threshold.

  1. Transactional Layer: Your warehouse application updates the stock_level field in the Inventory container whenever an item is sold. This is an O(1) operation that is very fast.
  2. Analytical Layer: You have an analytical query that runs every 5 minutes to check for low stock.
  3. The Query:
    SELECT product_id, stock_level 
    FROM OPENROWSET(...) 
    WHERE stock_level < 10
    
  4. The Outcome: Because this query runs against the Analytical Store, it has zero impact on the warehouse application's ability to process sales. Even if the warehouse is under heavy load, the alert system continues to function smoothly.

This pattern demonstrates the decoupling of operational and analytical concerns, which is the primary benefit of the Analytical Store.

Not read yet

Common Questions and FAQ

Can I enable the Analytical Store on an existing container?

Yes, you can enable the Analytical Store on an existing container at any time. Once enabled, the system will begin the synchronization process for all new data and, depending on the configuration, may backfill existing data.

Does the Analytical Store consume my Provisioned RUs?

No. The Analytical Store is billed separately. Operations performed against the Analytical Store do not consume the Request Units (RUs) provisioned for your transactional container. This is why it is the preferred method for analytical workloads.

Is the Analytical Store available for all Cosmos DB APIs?

Currently, the Analytical Store is primarily supported for the SQL (Core) API and the MongoDB API. Always check the official Azure documentation for the most current list of supported APIs and feature availability.

How does the Analytical Store handle updates to documents?

When a document is updated in the Transactional Store, the change is reflected in the Analytical Store. The system maintains the latest version of the document in the columnar format. If you need to track the history of changes (e.g., for an audit log), you should implement a versioning strategy in your application, such as adding a timestamp and version field to your documents.

Not read yet

Key Takeaways

To conclude this lesson, here are the most important points to remember when working with the Azure Cosmos DB Analytical Store:

  • Isolation is Key: The Analytical Store provides a dedicated columnar storage layer that separates your analytical workloads from your transactional workloads, preventing performance degradation for your operational applications.
  • Columnar Efficiency: By storing data in a column-oriented format, the Analytical Store is significantly more efficient for large-scale aggregations and analytical queries compared to the row-oriented transactional store.
  • Managed Synchronization: The sync process from the Transactional to the Analytical store is fully managed and occurs in near real-time, removing the need for manual ETL processes.
  • Synapse Link Integration: Leverage Azure Synapse Link to connect your Cosmos DB data to powerful analytical engines like Synapse SQL and Spark without moving your data.
  • Cost Management: Always utilize the Analytical TTL feature to manage storage costs, and optimize your queries by filtering and selecting only the necessary columns to reduce Analytical Read unit consumption.
  • Schema Consistency: Ensure that your application writes data with a consistent schema to prevent issues with the automated schema inference process in the Analytical Store.
  • Protect Your RUs: Make it a hard rule that any query involving large-scale data aggregation or scanning must be routed through the Analytical Store rather than the Transactional Store to preserve your Request Unit budget.

By mastering these concepts, you can build sophisticated, data-driven applications that provide real-time insights without compromising the performance or reliability of your transactional systems. The Analytical Store is a powerful tool in the modern data architect's kit, and applying these practices will ensure your implementations are efficient, scalable, and cost-effective.

Not read yet

Each section gets a ✓ as you scroll through it. Tap the button to jump to the next one.