Read preference tells MongoDB where to get data from in a group of servers. It helps balance speed and data freshness.
Read preference for replica sets in MongoDB
db.collection.find().readPref('mode') // or when connecting: MongoClient(uri, { readPreference: 'mode' })
'mode' can be primary, secondary, primaryPreferred, secondaryPreferred, or nearest.
Read preference can be set per query or for the whole connection.
db.orders.find().readPref('primary')db.orders.find().readPref('secondary')db.orders.find().readPref('nearest')This example shows how to read from primary and secondary servers in a replica set.
use shop // Read from primary printjson(db.products.find().readPref('primary').limit(2).toArray()) // Read from secondary printjson(db.products.find().readPref('secondary').limit(2).toArray())
Reading from secondaries may return slightly older data because of replication delay.
Use 'primaryPreferred' or 'secondaryPreferred' to allow fallback if preferred servers are unavailable.
Setting read preference helps distribute read load and improve performance.
Read preference controls which replica set member to read from.
It helps balance between data freshness and read performance.
You can set it per query or for the whole connection.
Practice
readPreference setting control in a MongoDB replica set?Solution
Step 1: Understand the role of readPreference
ThereadPreferencesetting tells MongoDB which replica set member to use for read operations.Step 2: Differentiate read and write roles
Writes always go to the primary, but reads can be directed to primary or secondary members based onreadPreference.Final Answer:
Which replica set member to read data from -> Option DQuick Check:
Read preference = controls read source [OK]
- Confusing readPreference with write concern
- Thinking it controls backups
- Assuming it changes replica set size
secondaryPreferred in a MongoDB connection string?Solution
Step 1: Identify correct parameter name
The correct parameter to set read preference in the connection string isreadPreference.Step 2: Match correct value
secondaryPreferredis a valid read preference mode; others likeprimaryOnlyorsecondaryOnlyare invalid or incorrect.Final Answer:
mongodb://host1,host2/?readPreference=secondaryPreferred -> Option AQuick Check:
UsereadPreferencewith valid mode [OK]
- Using 'readPref' instead of 'readPreference'
- Using invalid modes like 'primaryOnly'
- Misspelling 'secondaryPreferred'
secondary:
db.collection.find({ status: "active" }).readPref("secondary")
What will happen if all secondary members are down?Solution
Step 1: Understand 'secondary' read preference behavior
When read preference is set tosecondary, reads must come from secondary members only.Step 2: Consider all secondaries down scenario
If no secondary is available, the query cannot read from primary or fallback; it results in an error.Final Answer:
The query will fail with an error -> Option BQuick Check:
Strict 'secondary' fails if no secondary [OK]
- Assuming fallback to primary happens automatically
- Thinking query waits indefinitely
- Expecting empty results instead of error
db.collection.find().readPref('secondaryPreferred')
But it still reads from the primary. What is the likely cause?Solution
Step 1: Understand 'secondaryPreferred' behavior
This mode reads from secondary if available; otherwise, it falls back to primary.Step 2: Reason why primary is used
If no secondary is available or reachable, the driver reads from primary as fallback.Final Answer:
No secondary members are available, so it falls back to primary -> Option CQuick Check:
'secondaryPreferred' falls back to primary if no secondary [OK]
- Thinking syntax is wrong when it is correct
- Believing read preference needs a filter
- Confusing read preference with write behavior
Solution
Step 1: Understand read preference modes
primaryreads only from primary (fresh but may be slower),secondaryreads only from secondaries (may be stale),secondaryPreferredreads secondaries or falls back to primary,nearestreads from the closest member regardless of type.Step 2: Balance freshness and performance
secondaryPreferredprefers secondaries for read performance optimization but falls back to the primary to ensure data freshness and availability.Final Answer:
secondaryPreferred -> Option AQuick Check:
secondaryPreferred prefers secondary perf + primary freshness fallback [OK]
- Choosing 'secondary' which may return stale data
- Choosing 'primary' which may be slower
- Confusing 'secondaryPreferred' with 'nearest'
Ready to prove it?
Pass the test and this pattern moves unknown
