MongoDB Read Preference
What Is Read Preference?
In a replica set, one member is the primary and the others are secondaries that replicate from it. A read preference tells the client which member to read from. The default is primary, which always gives the latest committed writes. Reading from secondaries can spread load or reduce latency, but secondary data may be slightly behind (replication lag).
Why It Matters
| Reason | Benefit |
|---|---|
| Scale read traffic | Analytics or reporting can read from secondaries and leave the primary free for writes. |
| Lower latency | nearest can serve users from the closest data center. |
| Availability options | Preferred modes keep reads working during a primary failover. |
| Control over freshness | primary gives the freshest data; others trade freshness for capacity. |
How To Use It
- Connect to a replica set, for example an Atlas cluster or a local replica set.
- Run rs.status() to identify the primary and secondaries.
- Run a read with the default (primary) mode and note it succeeds.
- Switch the cursor to a secondary with .readPref("secondary").
- Set the mode for a whole connection using readPreference in the URI.
- Consider maxStalenessSeconds when secondaries must be reasonably fresh.
- Decide per feature: critical, read-your-own-write reads use primary; reports can use a secondary.
mongosh Examples
// Which members exist?
rs.status().members.map(m => ({ name: m.name, state: m.stateStr }))
// Default: reads from the primary
db.orders.find({ status: "new" })
// Read from a secondary for this query
db.orders.find({ status: "shipped" }).readPref("secondary")
// Prefer secondaries, fall back to primary
db.orders.find().readPref("secondaryPreferred")
// Lowest latency member
db.orders.find().readPref("nearest")
// As a connection string option
// mongodb://host1,host2,host3/shop?replicaSet=rs0&readPreference=secondaryPreferred&maxStalenessSeconds=90 Edge Cases & Pitfalls
Stale reads from secondaries
A document you just wrote may not yet be on a secondary. Do not read from secondaries right after writes when you need that data.
Does not scale writes
Read preference only moves reads. All writes still go to the primary.
secondary fails without secondaries
With mode secondary and none available, the read errors, unlike secondaryPreferred.
Uneven load
Pointing all reads at one secondary can overload it. Watch lag and resource usage.
Cheat Sheet
| Topic | Detail |
|---|---|
| primary | Default. All reads go to the primary; errors if there is no primary |
| primaryPreferred | Primary if available, otherwise a secondary |
| secondary | Reads only from secondaries |
| secondaryPreferred | Secondary if available, otherwise the primary |
| nearest | Member with the lowest network latency, primary or secondary |
| maxStalenessSeconds | Avoid secondaries that lag more than this many seconds |
| Tag sets | Target members with specific tags, such as a data center |
| cursor.readPref() | Set the mode for a cursor in mongosh |
| URI option | ?readPreference=secondaryPreferred in the connection string |
| Writes | Always go to the primary, regardless of read preference |
Key Takeaways
Read preference decides where a read goes. Read concern decides what data the read may see. Use primary unless you accept possibly stale data.
Frequently Asked Questions
What is the default read preference?
primary. All reads go to the primary member.
Can I read from secondaries?
Yes, by using secondary, secondaryPreferred, or nearest. Data may lag slightly behind the primary.
What is the difference between secondary and secondaryPreferred?
secondary errors if no secondary is available. secondaryPreferred falls back to the primary.
What does nearest do?
It picks the member with the lowest network latency, whether it is the primary or a secondary.
Does read preference affect writes?
No. Writes always go to the primary.
How is read preference different from read concern?
Read preference selects which member serves the read. Read concern sets the consistency and durability level of the data returned.
Next: MongoDB Read Concern
Continue the MongoDB learning path.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful
