Read from secondaries trade-offs in MongoDB - Time & Space Complexity
When reading data from secondary replicas in MongoDB, it's important to understand how the time to get data changes as the data size grows.
We want to see how reading from secondaries affects the speed of queries as more data is involved.
Analyze the time complexity of this MongoDB query reading from a secondary.
db.collection.find({ status: "active" }).readPreference("secondary")
This query fetches documents with status "active" from a secondary replica instead of the primary.
Look at what repeats when this query runs.
- Primary operation: Scanning documents on the secondary to find matches.
- How many times: Once for each document in the collection or index scanned.
As the number of documents grows, the time to find matching documents also grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks |
| 100 | About 100 document checks |
| 1000 | About 1000 document checks |
Pattern observation: The work grows roughly in direct proportion to the number of documents scanned.
Time Complexity: O(n)
This means the time to read matching documents from secondaries grows linearly with the number of documents scanned.
[X] Wrong: "Reading from secondaries is always faster than from primary because it spreads the load."
[OK] Correct: While secondaries can reduce primary load, reading from them can be slower if data is not up-to-date or if indexes are missing, causing more scanning work.
Understanding how reading from secondaries affects query time helps you explain trade-offs in distributed databases clearly and confidently.
What if we added an index on the "status" field? How would the time complexity change when reading from secondaries?