mongodb runtime_error ai_generated true

MongoServerError: QueryExceededTimeLimit: query on namespace test.orders exceeded time limit of 5000ms for secondary read

ID: mongodb/query-exceeded-time-limit-for-secondary-read

Also available as: JSON · Markdown · 中文
82%Fix Rate
87%Confidence
1Evidence
2025-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
mongodb-5.0 active
mongodb-6.0 active
mongodb-7.0 active
mongodb-8.0 active

Root Cause

A read operation on a secondary node took longer than the configured maxTimeMS due to high load or missing indexes.

generic

中文

由于高负载或缺少索引,从节点上的读取操作耗时超过配置的 maxTimeMS。

Official Documentation

https://www.mongodb.com/docs/manual/reference/method/cursor.maxTimeMS/

Workarounds

  1. 90% success Add an index that covers the query fields using db.collection.createIndex() to reduce scan time.
    Add an index that covers the query fields using db.collection.createIndex() to reduce scan time.
  2. 80% success Increase maxTimeMS for specific queries using .maxTimeMS(10000) in the application code.
    Increase maxTimeMS for specific queries using .maxTimeMS(10000) in the application code.
  3. 75% success Use a read preference tag to route queries to less loaded secondary nodes.
    Use a read preference tag to route queries to less loaded secondary nodes.

中文步骤

  1. Add an index that covers the query fields using db.collection.createIndex() to reduce scan time.
  2. Increase maxTimeMS for specific queries using .maxTimeMS(10000) in the application code.
  3. Use a read preference tag to route queries to less loaded secondary nodes.

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Setting readPreference to primary only increases load on the primary and does not fix the underlying query performance.

  2. 50% fail

    Increasing maxTimeMS globally can mask performance issues and lead to resource exhaustion.

  3. 50% fail

    Disabling secondary reads entirely reduces read throughput and defeats the purpose of replication.