- Newest
- Most votes
- Most comments
Hello Sachin,
To manage long-running queries in AWS DocumentDB and prevent CPU spikes, try:
-
Server-Side Timeout Configuration: AWS DocumentDB itself does not provide a built-in server-side timeout setting for queries. You need to handle query timeouts within your application code using the
maxTimeMSparameter provided by MongoDB drivers. -
Using
maxTimeMSfor Query Timeouts: ThemaxTimeMSparameter allows you to specify a time limit for query execution. When the specified time is exceeded, the query will be terminated.Example:
FindIterable<Document> result = collection.find().maxTime(1, TimeUnit.SECONDS); -
Handling Parallel Queries:
maxTimeMSapplies to each query individually. It ensures that no single query runs longer than the specified time, which helps prevent CPU spikes caused by long-running queries. However, it does not directly manage the overall load from parallel queries.To mitigate spikes due to parallel queries:
- Connection Pooling: Tune the connection pool settings to limit the number of concurrent operations.
- Rate Limiting: Implement rate limiting in your application to control the number of queries being sent to DocumentDB.
- Monitoring and Alerts: Use AWS CloudWatch to monitor CPU usage and set up alerts to take action when thresholds are exceeded.
By using maxTimeMS and managing query load through connection pooling and rate limiting, you can effectively control long-running queries and reduce CPU spikes.
I'm here to help!
ref.: https://docs.aws.amazon.com/documentdb/latest/developerguide/functional-differences.html
ref3: https://repost.aws/knowledge-center/documentdb-troubleshoot-high-cpu
Hi Sachin,
Here are some thoughts:
Configuring timeout for long-running queries in Amazon DocumentDB:
Amazon DocumentDB does not have a built-in server-side mechanism to automatically timeout long-running queries.
However, you can use the
maxTimeMS
option when executing queries to set a time limit. This will cause the query to be terminated if it exceeds the specified time limit.
For example, in the MongoDB shell you can use the following command to set a 60-second timeout:
db.collection.find({}).maxTimeMS(60000)
This timeout setting applies to individual queries and does not provide a way to control spikes caused by parallel queries.
Handling spikes caused by parallel queries:
Amazon DocumentDB does not have a built-in mechanism to automatically manage spikes caused by parallel long-running queries.
To handle such spikes, you would need to implement custom monitoring and throttling mechanisms:
Monitor the performance of your Amazon DocumentDB cluster using CloudWatch metrics and logs.
Identify the queries causing performance issues and optimize them.
Consider implementing client-side throttling to limit the number of concurrent queries, especially for long-running or resource-intensive operations.
Explore the use of connection pooling or connection management strategies to better manage the load on your Amazon DocumentDB cluster.
Relevant content
- asked 2 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 6 months ago

Is there a conventional way of estimating the maxTime? Like we have a API exposed which queries document DB, what I think is we can list down the last 6 months P90 for this API, and set maxTime above 20% of what we have found. Or we should set it corresponding to CPU utilization?