This procedure describes how to configure rate limiting using the ingressConnectionEstablishmentRateLimiterEnabled
parameter in MongoDB. The rate limiter helps protect your deployment from connection storms that can impact system stability and performance.
Before You Begin
Before you begin this procedure, ensure you meet the following requirements:
You have administrative privileges on your MongoDB deployment
You understand your system's connection load patterns and requirements
You know your driver's
connectTimeoutMS
setting
Procedure
Enable the rate limiter
To begin using the rate limiter, first enable it by setting the ingressConnectionEstablishmentRateLimiterEnabled
parameter to true
:
db.adminCommand( { setParameter: 1, ingressConnectionEstablishmentRateLimiterEnabled: true } )
Set the connection establishment rate
Set ingressConnectionEstablishmentRatePerSec
to the maximum rate that your cluster can handle without negatively impacting throughput. This should be a fixed number that doesn't vary based on other workload dynamics.
db.adminCommand( { setParameter: 1, ingressConnectionEstablishmentRatePerSec: 20 } )
Configure the connection queue depth
Set ingressConnectionEstablishmentMaxQueueDepth
according to the following formula:
maxQueueDepth < (establishmentRatePerSec / 1000) * (connectTimeoutMs - ((averageTimeToCompletedHelloMicros-averageTimeQueuedMicros)*1000))
This formula ensures that:
New connection attempts do not remain in the queue past the time that the driver has abandoned them.
Most
hello+auth
attempts succeed, rather than failing due to a timeout error.
The averageTimeQueuedMicros
metric can assist in tuning ingressConnectionEstablishmentMaxQueueDepth
, as it equals approximately (maxQueueDepth / establishRatePerSec) * 1e6
.
Important
When tuning the queue size, prioritize a longer queue size, rather than a shorter queue size. Drivers clearing their connection pools due to closed connections are a larger risk than connections remaining in the queue for too long and timing out.
Configure driver settings
If you observe connection failures, decrease your driver's maxPoolSize
and maxConnecting
settings. If you decrease these settings, the driver reuses connections, rather than opening more connections to the server during periods of increased latency, which can lead to connection storms.
Note
If you decrease maxPoolSize
and maxConnecting
, queries may spend a longer time at the driver layer waiting for a connection to be returned to the pool. Decreasing these settings helps maintain availability at the cost of increased latency during load spikes.
Monitoring and Troubleshooting
Monitor the following metrics to ensure you properly configure your rate limiter:
If you observe increasing numbers of rejected connections, consider:
Increasing
ingressConnectionEstablishmentRatePerSec
if the cluster appears to have suffiecient CPU resources to handle this load.Adjusting
ingressConnectionEstablishmentMaxQueueDepth
based on the formula.Reviewing driver connection pool settings.