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,
mongodperforms a handshake. This involves authentication, TLS/SSL negotiation (if applicable), and other initial setup. This process uses server resources. - Connection Pool Management:
mongodmaintains 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
mongodthat handle its requests. Exceeding the limit meansmongodcan’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 themongodinstance.
The key levers you control are:
maxIncomingConnections(inmongod.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 tomongod. - Resource Limits (OS Level): While
maxIncomingConnectionsis a MongoDB setting, the operating system also has limits on open file descriptors (which sockets are). You might need to adjustulimit -non Linux systems.
Here’s how you’d tune maxIncomingConnections:
-
Diagnose: Monitor your
mongodlogs for errors like[connXXX] connection accepted, followed by[connXXX] too many connections. Also, usedb.serverStatus().connectionsin the mongo shell to see the current and available connections.db.serverStatus().connectionsLook for
currentbeing close toavailable. -
Tune: Edit your
mongod.conffile (e.g.,/etc/mongod.conf). Find thenetsection and add or modifymaxIncomingConnections. For example, to allow up to 500 connections:net: port: 27017 bindIp: 127.0.0.1 maxIncomingConnections: 500 -
Restart: Restart the
mongodservice for the change to take effect.sudo systemctl restart mongod -
Verify: Check
db.serverStatus().connectionsagain. You should seeavailableincrease.
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.