Server-Side Latency Metrics

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

Monitoring and Troubleshooting Server-Side Latency in Azure Cosmos DB

Introduction: Why Server-Side Latency Matters

In the world of distributed databases, performance is often measured by how quickly a request completes from the moment it hits the database engine until the response is sent back. In Azure Cosmos DB, this is known as server-side latency. When your applications experience slow response times, it is easy to assume the network or the client application is the culprit. However, in many cases, the bottleneck lies within how the database engine is processing your queries, executing stored procedures, or handling indexing overhead.

Understanding and monitoring server-side latency is critical for maintaining a predictable user experience. If your application relies on low-latency data access—such as a real-time gaming leaderboard, a financial trading dashboard, or a high-traffic e-commerce cart—even a few milliseconds of unexpected latency can lead to cascading failures, timeouts, and a degraded user experience. By mastering the metrics provided by Azure, you can transition from reactive "firefighting" to proactive performance tuning. This lesson will guide you through the intricacies of measuring, analyzing, and optimizing server-side latency within your Cosmos DB environment.


Not read yet

Understanding the Anatomy of Latency

To effectively troubleshoot latency, we must first define what we are measuring. Server-side latency in Cosmos DB represents the time spent by the database engine to process a request. This includes parsing the request, authentication, authorization, data retrieval from the storage engine, index lookups, and the execution of any server-side logic like stored procedures or triggers.

It is important to distinguish between "End-to-End Latency" and "Server-Side Latency." End-to-end latency includes the network round-trip time, client-side serialization, and queuing delays on the client machine. Server-side latency, by contrast, is strictly the time the Cosmos DB engine takes to do its work. When you see high latency in your application, comparing these two metrics is the first step in narrowing down the root cause. If your server-side latency is low but end-to-end latency is high, you should investigate your network configuration or client-side resource constraints rather than the database itself.

Callout: Latency vs. Throughput It is a common mistake to confuse latency with throughput. Throughput (measured in Request Units per second, or RU/s) is a measure of capacity—how much work the system can handle in a given timeframe. Latency is a measure of speed—how long a single unit of work takes to complete. You can have high throughput with high latency (e.g., a batch job) or low throughput with low latency (e.g., a simple point read). Monitoring one without the other gives you an incomplete picture of your system's health.


Not read yet

Key Metrics in Azure Monitor

Azure Cosmos DB exposes several metrics through Azure Monitor that are essential for tracking server-side performance. You can access these in the Azure Portal under the "Metrics" blade of your Cosmos DB account.

1. Total Requests

This metric gives you the volume of requests hitting your database. A sudden spike in requests can often lead to increased latency because the database engine has to manage a larger queue of operations. If you notice latency rising in tandem with request volume, you may be hitting the limits of your provisioned throughput.

2. Average Latency (ms)

This is the primary metric for tracking server-side speed. Azure Monitor provides this data aggregated by operation type (Read, Write, Query, etc.). By segmenting this metric, you can identify if a specific type of operation is causing the slowdown. For instance, if Query latency is spiking but Point Read latency remains stable, you know the issue is related to how you are structuring or indexing your queries, not the underlying hardware performance.

3. Throttled Requests (429s)

When a request exceeds the provisioned throughput, the server returns a 429 "Too Many Requests" status code. While this is technically a throughput issue, it directly impacts latency. When a request is throttled, the client must wait and retry, which significantly increases the perceived latency for the end user.

4. Indexing Progress

If you are performing high-volume writes, the background process of updating the index can impact performance. Monitoring the indexing progress ensures that your write latency isn't being affected by a massive indexing backlog.


Not read yet

Analyzing Latency with Diagnostic Logs

While metrics provide the "what," diagnostic logs provide the "why." To get granular data, you must enable diagnostic logging for your Cosmos DB account. This sends detailed request-level telemetry to a Log Analytics workspace. Once enabled, you can use Kusto Query Language (KQL) to dive deep into the performance of specific requests.

Using KQL to Identify Slow Queries

When you have logs streaming to a Log Analytics workspace, you can execute queries to find the most expensive operations. Below is an example of a KQL query that retrieves the top 10 slowest queries based on server-side duration:

AzureDiagnostics
| where ResourceProvider == "MICROSOFT.DOCUMENTDB"
| where Category == "DataPlaneRequests"
| project TimeGenerated, OperationName, DurationMs, RequestCharge, QueryText
| top 10 by DurationMs desc

Explanation of the query:

  • AzureDiagnostics: The base table where logs are stored.
  • where Category == "DataPlaneRequests": We filter for data plane activity, ignoring control plane operations like scaling or key rotation.
  • project: We select only the columns relevant to performance analysis.
  • top 10 by DurationMs desc: This sorts the results to show you the slowest operations first, allowing you to identify the "low hanging fruit" for optimization.

Note: Enabling full diagnostic logs can generate a significant amount of data, which may increase your Azure bill. Always use filters to limit the logs to the specific databases or containers you are currently troubleshooting.


Not read yet

Common Causes of Increased Server-Side Latency

1. Inefficient Query Patterns

The most common cause of high latency in Cosmos DB is unoptimized queries. A query that performs a full collection scan (a "cross-partition query" that lacks proper filters) forces the engine to look at every single document in the container. As your data grows, these scans become progressively slower.

How to avoid:

  • Always include the partition key in your query filters.
  • Use the query execution plan in the Azure Portal to verify that your query is using the index effectively.
  • Avoid using functions like STARTSWITH, CONTAINS, or UPPER on properties that are not indexed, as these force a scan.

2. Large Documents and Payload Size

Cosmos DB has a limit of 2MB per document. However, even if your documents are well below this limit, retrieving very large documents can increase latency due to the time required for the engine to fetch the data from storage and serialize it for the response.

How to avoid:

  • Use projection (e.g., SELECT c.id, c.name FROM c) instead of SELECT * to retrieve only the fields you need. This reduces the amount of data the engine must process and transmit.

3. Hot Partitions

If you have a poorly chosen partition key—for example, one that results in most of your data landing in a single logical partition—you will experience "hot partitions." All requests for that data will hit the same physical shard, causing a bottleneck. The server-side latency will spike because that specific shard cannot handle the concurrent volume, even if your total account throughput is sufficient.

How to avoid:

  • Choose a partition key with high cardinality (e.g., UserID, OrderID, or a synthetic key).
  • Avoid keys that create a "time-series" bottleneck, such as using Date as a partition key if all current traffic is hitting today's date.

Not read yet

Step-by-Step: Troubleshooting a Latency Spike

If you receive an alert that your application is experiencing high latency, follow this systematic process to identify and resolve the issue:

Step 1: Verify the Scope

Check your Azure Monitor metrics. Is the high latency affecting all operations, or just a specific collection? If it is a single collection, check if there is an ongoing large-scale data ingestion or a batch job running that might be competing for resources.

Step 2: Check for Throttling

Look at the "Total Requests" vs. "Throttled Requests" metrics. If they correlate, your issue is not necessarily "slow" processing, but rather a lack of provisioned throughput. You may need to scale up your RU/s or implement a more robust retry policy on the client side.

Step 3: Analyze Query Performance

Use the query metrics provided in the SDK or the Azure Portal. Look for the indexHitRatio and the totalQueryExecutionTime. If the index hit ratio is low, your query is likely performing a scan. Adjust your indexing policy to include the properties frequently used in WHERE clauses.

Step 4: Review Server-Side Logic

If you are using Stored Procedures, Triggers, or User Defined Functions (UDFs), remember that these execute on the server. A poorly written UDF that performs complex calculations or multiple lookups can dramatically increase server-side latency. Test these scripts in isolation to see if they are the source of the delay.

Step 5: Test and Validate

After making changes (e.g., updating an index or rewriting a query), monitor the metrics for at least one hour. Latency improvements are often visible immediately, but you should ensure the changes don't negatively impact other parts of your application.


Not read yet

Comparison Table: Latency Indicators

Symptom Likely Cause Recommended Action
High Latency, Low Throughput Inefficient indexing or scan Optimize query filters; update index policy
High Latency, High 429s Throughput saturation Increase RU/s or use Autoscale
High Latency, Specific Partition Hot partition Re-evaluate partition key strategy
High Latency, High Payload "Select *" usage Implement field-level projection

Best Practices for Latency Management

1. Optimize Your Indexing Policy

By default, Cosmos DB indexes every property. While this makes development easy, it increases the cost and latency of write operations because the engine must update the index for every write. If your application is write-heavy and you only query by specific fields, customize your indexing policy to exclude unused properties.

2. Use the Correct SDK

Always use the latest version of the Azure Cosmos DB SDK. The SDKs are constantly updated with performance improvements, better connection management, and smarter retry logic. An outdated SDK might be using inefficient connection modes (like Gateway mode instead of Direct mode) which adds unnecessary latency.

3. Leverage Direct Connectivity

In most cases, you should use "Direct Mode" connectivity. In this mode, the client connects directly to the backend nodes. This bypasses the intermediate gateway, reducing the number of network hops and lowering overall latency.

Tip: If you are running your application inside an Azure Virtual Machine or App Service, ensure it is in the same Azure region as your Cosmos DB account. Cross-region traffic is the single biggest contributor to end-to-end latency.

4. Implement Request Timeouts

On the client side, always set a reasonable timeout for your database calls. If a request takes too long, it is often better to fail fast than to hang the application thread. This prevents a slow database operation from causing a "thread pool starvation" issue in your application server.


Not read yet

Common Pitfalls to Avoid

Ignoring the Request Charge

Many developers focus solely on latency and ignore the "Request Charge" metric. A query might run quickly but consume a massive amount of RU/s. Over time, these high-cost queries will lead to throttling, which eventually causes latency spikes. Always optimize for both latency and RU/s.

Relying on Default Partitioning

Never use the default partition key if you don't understand how your data access patterns will look. If you build your application and realize six months later that your partition key is causing hotspots, re-partitioning a container is a non-trivial task that requires moving all your data to a new collection.

Misinterpreting Client-Side Metrics

Do not assume that the time measured by your application code is the same as the server-side latency. Your application might be waiting on a garbage collection cycle, a full thread pool, or a slow DNS lookup. Always use the Diagnostics property provided by the Cosmos DB SDK to see the server-side duration specifically.

// Example of accessing request diagnostics in C#
try {
    ItemResponse<MyDocument> response = await container.ReadItemAsync<MyDocument>("id", new PartitionKey("pk"));
    Console.WriteLine($"Server-side latency: {response.Diagnostics.GetClientElapsedTime()}");
} catch (CosmosException ex) {
    Console.WriteLine($"Error: {ex.Diagnostics}");
}

Not read yet

Advanced Troubleshooting: The Role of Stored Procedures

Stored procedures in Cosmos DB execute inside the database engine, meaning they are bound by the same resource limits as other operations. When a stored procedure runs, it occupies the partition's resources. If a stored procedure is poorly written, it can block other requests, leading to a spike in latency for all users hitting that partition.

When troubleshooting, check the RequestCharge for your stored procedures. If a stored procedure is consistently hitting the RU limit, consider breaking it into smaller, more modular functions or moving the logic to the application tier. The application tier is generally easier to scale and debug than the database tier.


Industry Standards for Performance Monitoring

In high-scale environments, manual monitoring is insufficient. You should implement a structured approach to observability:

  • Baseline Performance: Before deploying to production, run load tests to establish a baseline for "normal" latency. This gives you a reference point when an issue arises.
  • Alerting: Set up alerts in Azure Monitor for P95 and P99 latency. P50 (the median) often hides the latency spikes experienced by your most unlucky users. Monitoring the 99th percentile ensures you are aware of the "long tail" of performance issues.
  • Correlation IDs: Always pass a correlation ID from your application to the database. This allows you to trace a specific user request through your application logs all the way into the Cosmos DB diagnostic logs.

Not read yet

Summary of Key Takeaways

  1. Distinguish between layers: Server-side latency is the time the database engine spends processing; it is distinct from network and client-side delays. Always verify the source of the latency before attempting to tune the database.
  2. Use the right tools: Leverage Azure Monitor for high-level trends and Diagnostic Logs with KQL for deep-dive investigations into specific slow operations.
  3. Optimize query patterns: Always include the partition key in your queries and avoid full collection scans. Use the execution plan to verify that you are hitting the index.
  4. Monitor throughput and throttling: High latency is often a symptom of throughput exhaustion. If you see 429 status codes, address your capacity or retry logic first.
  5. Architect for scale: Avoid hot partitions by choosing a high-cardinality partition key. A well-designed partition strategy is the most effective way to ensure low latency as your data grows.
  6. Prioritize Direct Mode: Ensure your client applications are using the correct SDK and connection mode (Direct) to minimize unnecessary network overhead.
  7. Baseline and Alert: Establish performance baselines and configure alerts based on the 99th percentile (P99) to catch issues before they impact the majority of your user base.

By following these practices, you move beyond simple monitoring and into the realm of true performance engineering. Azure Cosmos DB is a powerful tool, but it requires an understanding of its distributed nature to extract the best possible performance for your applications. Keep your queries lean, your partitions balanced, and your monitoring proactive, and you will maintain the high-performance standards your users expect.

Not read yet

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