Problem
Users connecting via MongoDB Atlas (SRV) cannot configure Read Preferences directly (they need to provide replica set name).
Currently, the ReadPreference configuration logic in config.go is nested inside a conditional check for ReplicaSet.
if c.ReplicaSet != "" {
query.Set("replicaSet", c.ReplicaSet)
// ... Read Preference logic ...
}
When users connect using mongodb+srv:// (common for MongoDB Atlas), they typically do not provide an explicit ReplicaSet name because the SRV record handles discovery. Consequently, the entire logic block is skipped, and the readPreference query parameter is never added to the connection string.
This forces the driver to use the default primary read preference. Reading from the Primary node is an anti-pattern as it can degrade the performance of the source application by consuming inputs/outputs (IOPS) and CPU intended for transactional writes.
Solution
Decouple the ReadPreference logic from the ReplicaSet check. The read preference should be evaluated and applied to the connection string parameters independently, as it is valid for both SRV-based discovery and manual Replica Set configurations.
Problem
Users connecting via MongoDB Atlas (SRV) cannot configure Read Preferences directly (they need to provide replica set name).
Currently, the
ReadPreferenceconfiguration logic in config.go is nested inside a conditional check forReplicaSet.When users connect using mongodb+srv:// (common for MongoDB Atlas), they typically do not provide an explicit ReplicaSet name because the SRV record handles discovery. Consequently, the entire logic block is skipped, and the readPreference query parameter is never added to the connection string.
This forces the driver to use the default primary read preference. Reading from the Primary node is an anti-pattern as it can degrade the performance of the source application by consuming inputs/outputs (IOPS) and CPU intended for transactional writes.
Solution
Decouple the ReadPreference logic from the ReplicaSet check. The read preference should be evaluated and applied to the connection string parameters independently, as it is valid for both SRV-based discovery and manual Replica Set configurations.