Server-Side Latency Metrics
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
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.
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.
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.
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.
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, orUPPERon 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 ofSELECT *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
Dateas a partition key if all current traffic is hitting today's date.
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.
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.
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}");
}
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.
Summary of Key Takeaways
- 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.
- Use the right tools: Leverage Azure Monitor for high-level trends and Diagnostic Logs with KQL for deep-dive investigations into specific slow operations.
- 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.
- 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.
- 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.
- Prioritize Direct Mode: Ensure your client applications are using the correct SDK and connection mode (Direct) to minimize unnecessary network overhead.
- 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.
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