readPreference - MongoDB replica set read preference configuration
Overview
readPreference is the MongoDB node-selection strategy for read operations in replica sets. It supports read/write separation by reducing load on the primary node and can help globally distributed deployments read from lower-latency nodes.
Applicable scenarios:
- Replica Set deployment
- Read/write separation to reduce load on the primary node
- Globally distributed deployments that need lower-latency reads
- Analytics/report queries that can tolerate eventual consistency
Restrictions:
config.readPreferencesets the connection-level default.- Individual read operations can pass
readPreferencein their options to override the default for that operation. - The current monSQLize runtime is MongoDB-only; this option maps to MongoDB driver read preference behavior.
- Reads from secondary nodes may observe replication lag, so read-after-write paths should use
primaryorprimaryPreferred. - Reads inside MongoDB transactions should stay on
primary; keep transaction reads on the connection default or override them toprimary. - It has no useful effect in stand-alone mode because there is only one node.
Core Features
5 read preference modes
Connection-level global configuration
Features:
- Configure once and all queries will take effect
- Use query-level options only for operations that need a different read route
- Simplify the default path and reduce repeated configuration
API parameter description
Connection configuration
Optional values:
'primary'- Read only from the primary node (default)'primaryPreferred'- Read from the primary first, then a secondary when the primary is unavailable'secondary'- Read only from secondary nodes'secondaryPreferred'- Read from secondary nodes first, then the primary when no secondary is available'nearest'- Read from the nearest node (low latency)
Usage example
Basic usage
Example 1: Default read preference (primary)
Example 2: secondaryPreferred (prefer secondary nodes)
Applicable scenarios: read-heavy workloads that should reduce load on the primary node.
Example 3: secondary (read only from secondary nodes)
Applicable scenarios: analytics/reporting queries that should isolate primary write load.
Example 4: primaryPreferred (read primary node first)
Applicable scenario: strong consistency is required, but reads should have a fallback when the primary node is unavailable.
Example 5: nearest (nearest read, low latency)
Applicable scenarios: Global distributed deployment, nearby reading to reduce latency
Advanced usage
Example 6: Use with other options
Best Practices
Recommended practices
-
Use
secondaryPreferredfor read-heavy workloads -
Use
primary(default) for strong-consistency reads -
Use
nearestfor globally distributed deployments -
Use
secondaryfor analytics/reporting queries
Notes
-
Replication delay problem
-
Transaction reads
-
Single mode is invalid
-
Replica Set URI Format
-
Cross-database compatibility
Performance impact
Impact of read preference on performance
Performance optimization suggestions
- Read-heavy workloads: Use
secondaryPreferredto move tolerant reads away from the primary. - Globally distributed: Use
nearestwhen lower latency matters more than always reading from the primary. - Analytics/reporting: Use
secondaryto isolate primary write load
FAQ
Q: Does readPreference support query-level configuration?
A: Yes. config.readPreference is the default, and read methods that pass MongoDB driver options can override it per operation:
Use this sparingly. Keep the connection-level default for most reads, and override only read-after-write or analytics queries that need a different route.
Q: How to verify that readPreference is effective?
A:
- Check the MongoDB log/profile to confirm that the read operation hits a secondary node
- Add delay on a secondary node and observe whether query results lag.
- Use
db.currentOp()to view the read preferences of active connections
Q: How long is the replication delay?
A:
- LAN replica set: typically 10-100ms
- Cross-region replica set: maybe 100ms-1s
- When the network jitters: maybe 1s-5s
- It is recommended to monitor
optimeDatedifferences inrs.status()
Q: How to deal with replication delays?
A:
- Read immediately after writing: use
primaryorprimaryPreferred - Acceptable delay: Use
secondaryPreferredorsecondary - Mixed strategy: Use
primaryfor key queries andsecondaryfor analytical queries.
Q: How to test in stand-alone mode?
A:
readPreference is invalid in stand-alone mode. Suggestions:
- Use Docker Compose to build a local replica set
- Use MongoDB Atlas free cluster (M0)
- Use
mongodb-memory-serverto simulate a replica set (configuration required)