GitOrigin-RevId: 35db3811d8f749edd5b79ba910adcbc1ceb54cc4
2.5 KiB
Load balancer support
mongos
has built-in support for connections made via L4 load balancers. However, placing mongos
endpoints behind load balancers requires proper configuration of the load balancers, mongos
, and
any drivers or shells used to connect to the database. Three conditions must be fulfilled for
mongos
to be used behind a load balancer:
mongos
must be configured with the MongoDB Server ParameterloadBalancerPort
whose value can be specified at program start in any of the ways mentioned in the server parameter documentation. This option causesmongos
to open a second port that expects only load balanced connections. All connections made from load balancers must be made over this port, and no regular connections may be made over this port.- The L4 load balancer must be configured to emit a proxy protocol header
at the start of its connection stream.
mongos
supports both version 1 and version 2 of the proxy protocol standard. - The connection string used to establish the
mongos
connection must set theloadBalanced
option, e.g., when connecting to a localmongos
instance, if theloadBalancerPort
server parameter was set to 20100, the connection string must be of the form"mongodb://localhost:20100/?loadBalanced=true"
.
mongos
will emit appropiate error messages on connection attempts if these requirements are not
met.
There are some subtle behavioral differences that these configuration options enable, chief of
which is how mongos
deals with open cursors on client disconnection. Over a normal connection,
mongos
will keep open cursors alive for a short while after client disconnection in case the
client reconnects and continues to request more from the given cursor. Since client reconnections
aren't expected behind a load balancer (as the load balancer will likely redirect a given client
to a different mongos
instance upon reconnection), we eagerly close cursors on load balanced
client disconnects. We also abort any in-progress transactions that were initiated by the load balanced client.