Read preference for replica sets in MongoDB - Time & Space Complexity
When using read preferences in MongoDB replica sets, it is important to understand how the choice affects the number of operations the database performs.
We want to know how the time to get data changes as the size of the data or number of replicas grows.
Analyze the time complexity of the following MongoDB query using a read preference.
db.collection.find({ status: "active" })
.readPreference("secondaryPreferred")
.toArray()
This code reads documents with status "active" from a replica set, preferring secondary nodes but falling back to primary if needed.
Look for repeated actions that affect performance.
- Primary operation: Scanning documents matching the query on the chosen replica node.
- How many times: Once per query execution on one node; no loops in the client code but the server scans matching documents.
The time to return results grows with the number of matching documents on the chosen node.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 document checks and returns |
| 100 | About 100 document checks and returns |
| 1000 | About 1000 document checks and returns |
Pattern observation: The work grows roughly in direct proportion to the number of matching documents on the node.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of matching documents on the replica node used.
[X] Wrong: "Using read preference to secondary nodes makes queries faster regardless of data size."
[OK] Correct: The query still scans matching documents on the chosen node, so time depends on how many documents match, not just which node is used.
Understanding how read preferences affect query time helps you explain trade-offs in distributed databases clearly and confidently.
"What if we changed the read preference to 'nearest'? How would the time complexity change when querying a large replica set?"