MongoDB Connection Issues

connection-poolingmongodb-3.0replication

We have a MEAN application with a 3-member MongoDB replica set. Somehow the total connection count keeps increasing until the primary is not able to take any new connections and goes down, making the application painfully slow. I have set the ulimit to 30000 and restarted the MongoDB and in no time it got 28000+ connection opened and primary went down.

Any idea why this may be happening? Most of the connections are between the different ports on primary with port 27017 on primary (for instance, primary_server:45002 to primary_server:27017).

Best Answer

  1. Make sure you have right UNIX ulimit Settings.You can see also: https://docs.mongodb.org/manual/reference/ulimit/
  2. Use top command to check if the performance bottleneck is CPU overload.Check the mongodb log file if there is any slow query,and you can use MTools to analyse it.Then you need optimize the slow query.
  3. Use free command to check if not enough physical memory is available.If MongoDB has too many connections,you need to check Stack settings.every connection is a thread,need a Stack,Stack settings usually is too large by default in Linux: shell> ulimit -a | grep stack You can see the real used Stack size,like this: shell> cat /proc/$(pidof mongod)/limits | grep stack | awk -F 'size' '{print int($NF)/1024}' The total mem consumed by connections is so large,I suggest you set it smaller,such as 1024: shell> ulimit -s 1024