Hi,
I’m currently running a MongoDB replica set in production, and I’ve noticed that my Go service is routing most of its read requests to only one of the secondary nodes, even though other secondaries are healthy and available. This is causing uneven load and occasional I/O timeout issues.
Here’s my setup and what I’ve observed:
Setup:
- MongoDB Replica Set: 1 primary, 2 secondaries
- Driver:
bongoORM, which internally uses the mgo.v2 MongoDB driver - Connection Config in Go:
config := &bongo.Config{
Database: "core",
DialInfo: &mgo.DialInfo{
AppName: "core-client",
Addrs: []string{"<replica-set-hosts>"},
Timeout: 3 * time.Second,
PoolLimit: 20,
PoolTimeout: 2 * time.Second,
Username: "<user>",
Password: "<pass>",
MinPoolSize: 5,
},
}
- No explicit read preference is set in the code. Based on the
bongosource and default behavior ofmgo, it defaults to:
session.SetMode(mgo.SecondaryPreferred, true)
Observations:
- Almost all read queries go to one secondary
- The other secondary node has very low read load
- Confirmed this from Mongo logs and GCP VM metrics
- Mongo logs show slow queries with
readPreferenceset tosecondaryPreferred
My Goal:
I want to distribute read queries across all secondary nodes in a more balanced way.
What I’ve tried:
- Verified there’s no explicit
readPreferenceoverride in my code - Verified from logs that the queries are indeed going with
secondaryPreferredmode - Checked that all secondaries are healthy and eligible
- Checked that
mgochooses the nearest secondary (lowest ping), and then sticks with it
My Questions:
- Is there any way to force or encourage round-robin or load-balanced behavior across all secondaries using the
mgodriver (orbongo)? - Would setting
mgo.Nearesthelp, and how can I do that when usingbongo? Is it safe to override the session mode after initializing Bongo? - Are there other flags/settings in MongoDB or the driver that influence this selection (like latency thresholds)?
- Could I be misinterpreting the
nearestbehavior - does it actually round-robin among multiple low-latency nodes? - Is switching to the official MongoDB Go Driver the better approach for more advanced control? If yes, then how ?