Skip to content

enhancement: Decouple Read Preference from Replica Set configuration for SRV support #682

@vaibhav-datazip

Description

@vaibhav-datazip

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good second issueafter you have solved good first issueimprovementimprovements in existing flows

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions