poolSize & connection count

We’re trying to get the MongoDB Node Driver to only use a single connection to avoid reaching our Atlas max concurrent connections limit.

However it seems the poolSize:1 & maxPoolSize: 1 options are plainly ignored by the driver.

To reproduce (Mongo Node Driver 3.6.8, Node.js 16.4.0, Atlas 3-node replica set running MongoDB 4.4.6):

  • run in Node
mongodb = require('mongodb')
let MC = new mongodb.MongoClient('mongodb+srv://myreplicaset-vtcmd.mongodb.net/mydb', {
  tls: true,
  auth: {user: 'myuser', password:'mypassword'},
  useNewUrlParser: true,
  useUnifiedTopology: true,
  poolSize: 1,
  maxPoolSize: 1
})
  • run in shell
$ lsof -i tcp:27017
node    8250 user   27u  IPv4 0xe04adf2892fda27d      0t0  TCP 192.168.1.115:52663->ec2-52-47-xx-xx.eu-west-3.compute.amazonaws.com:27017 (ESTABLISHED)
node    8250 user   28u  IPv4 0xe04adf2877cb427d      0t0  TCP 192.168.1.115:52668->ec2-15-236-xx-xx.eu-west-3.compute.amazonaws.com:27017 (ESTABLISHED)
node    8250 user   29u  IPv4 0xe04adf289301f9dd      0t0  TCP 192.168.1.115:52662->ec2-15-188-xx-xx.eu-west-3.compute.amazonaws.com:27017 (ESTABLISHED)
node    8250 user   30u  IPv4 0xe04adf287abf7b1d      0t0  TCP 192.168.1.115:52661->ec2-15-236-xx-xx.eu-west-3.compute.amazonaws.com:27017 (ESTABLISHED)
node    8250 user   31u  IPv4 0xe04adf28933e627d      0t0  TCP 192.168.1.115:52669->ec2-15-188-xx-xx.eu-west-3.compute.amazonaws.com:27017 (ESTABLISHED)
node    8250 user   32u  IPv4 0xe04adf287240058d      0t0  TCP 192.168.1.115:52670->ec2-52-47-xx-xx.eu-west-3.compute.amazonaws.com:27017 (ESTABLISHED)

Shouldn’t there be a single TCP connection here, or am I misunderstanding how poolSize applies to Unified Topology replica sets ?

Hi @Guillaume_Khayat, thanks for the question! The connection pool is on a per-mongod/mongos basis, so when connecting to a 3-member replica there will be three connection pools (one per mongod), each with a maxPoolSize of 1. Additionally, there is a required monitoring connection for each node as well, so you end up with (maxPoolSize+1)*number_of_nodes TCP connections, or (1+1)*3=6 total TCP connections in the case of a 3-member replica set.

Also just to note, maxPoolSize is the correct option to use with the unified topology, you should be able to safely drop poolSize which is only used by the legacy topology.

Please let me know if that answers your questions or if you need any further help.

1 Like

Hi @Eric_Adum

Thanks for the clear answer!

Is there another way to reduce that number while staying on the Unified Topology bandwagon (since this will be the only supported topology going forward) ? The only alternatives i can think of are :

  • using legacy topologies (i remember the legacy replica set topology seemed to only use a single TCP connection per mongod), with the understanding that they will be removed in a later version
  • using a direct connection to a single node, with the many caveats this entails (no failover, no primary/secondary selection, etc)

Sorry about the delayed response! Yes, those do seem like the only available options if you’re trying to limit the TCP connections made by the driver as much as possible. Using direct connection is definitely the more future-proof option given that the legacy topology is going away.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.