The default MongoDB connection limit is surprisingly low, often leading to "too many connections" errors even under moderate load.

Let’s see how it works with a small, self-contained example.

from pymongo import MongoClient

# Connect to a local MongoDB instance
client = MongoClient('mongodb://localhost:27017/')

# Attempt to create 1000 connections
connections = []
try:
    for i in range(1000):
        connections.append(client.get_database('test_db').get_collection('test_collection'))
    print("Successfully created 1000 connections.")
except Exception as e:
    print(f"Failed to create 1000 connections: {e}")

# Clean up (in a real scenario, you'd manage connection pooling)
client.close()

When you run this, if your maxIncomingConnections is set to the default of 100, you’ll see an error like Connection refused or Too many connections. This isn’t about the number of MongoClient instances, but the number of actual TCP connections open to the mongod process.

The core problem MongoDB solves with connection limits is preventing a single client or a misbehaving application from overwhelming the server’s resources (CPU, memory, file descriptors). Each connection consumes these resources. maxIncomingConnections is the gatekeeper, ensuring that mongod doesn’t get buried under too many simultaneous client requests.

Here’s how it breaks down internally:

  • Connection Handshake: When a client connects, mongod performs a handshake. This involves authentication, TLS/SSL negotiation (if applicable), and other initial setup. This process uses server resources.
  • Connection Pool Management: mongod maintains an internal list of active connections. It tracks their state, reads/writes, and associated resources.
  • Resource Allocation: Each connection can lead to threads or processes within mongod that handle its requests. Exceeding the limit means mongod can’t allocate these necessary resources efficiently or at all.
  • maxIncomingConnections: This is the primary configuration setting that dictates the hard limit on concurrent connections. It’s a global setting for the mongod instance.

The key levers you control are:

  • maxIncomingConnections (in mongod.conf): This is the most direct setting. You increase it to allow more connections.
  • Application Connection Pooling: This is crucial. Your application should not open a new connection for every request. Instead, use a connection pool (like the one built into pymongo, mongoose, etc.) to reuse existing connections. This dramatically reduces the actual number of connections to mongod.
  • Resource Limits (OS Level): While maxIncomingConnections is a MongoDB setting, the operating system also has limits on open file descriptors (which sockets are). You might need to adjust ulimit -n on Linux systems.

Here’s how you’d tune maxIncomingConnections:

  1. Diagnose: Monitor your mongod logs for errors like [connXXX] connection accepted, followed by [connXXX] too many connections. Also, use db.serverStatus().connections in the mongo shell to see the current and available connections.

    db.serverStatus().connections
    

    Look for current being close to available.

  2. Tune: Edit your mongod.conf file (e.g., /etc/mongod.conf). Find the net section and add or modify maxIncomingConnections. For example, to allow up to 500 connections:

    net:
      port: 27017
      bindIp: 127.0.0.1
      maxIncomingConnections: 500
    
  3. Restart: Restart the mongod service for the change to take effect.

    sudo systemctl restart mongod
    
  4. Verify: Check db.serverStatus().connections again. You should see available increase.

The default for maxIncomingConnections is 100. If your application consistently uses more than this and you’re seeing connection errors, you’ll need to increase it. A common recommendation is to set it to a value slightly higher than your expected peak concurrent connections, considering OS limits.

When you increase maxIncomingConnections, remember that the operating system’s limit on open file descriptors (ulimit -n) can become the bottleneck. If mongod is configured for 1000 connections, but your OS limit is 256, you’ll still hit the OS limit first. You’ll need to adjust /etc/security/limits.conf or similar OS-specific settings to raise the file descriptor limit for the user running mongod.

The most surprising thing about connection management is how often developers overlook the application’s role. A perfectly tuned maxIncomingConnections on the server is useless if the application is opening and closing thousands of short-lived connections, each consuming valuable server resources during its handshake. It’s the combination of server limit and efficient application pooling that truly matters.

Once you’ve tuned maxIncomingConnections, the next common bottleneck you’ll encounter is often related to insufficient oplog size, leading to replica set sync issues.

Want structured learning?

Take the full Mongodb course →