> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Configure the Ingress Connection Establishment Rate Limiter

This procedure describes how to configure rate limiting using the [`ingressConnectionEstablishmentRateLimiterEnabled`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.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`](https://www.mongodb.com/docs/manual/reference/connection-string-options.md#mongodb-urioption-urioption.connectTimeoutMS) setting

## Procedure

1. Enable the rate limiter

   To begin using the rate limiter, first enable it by setting the [`ingressConnectionEstablishmentRateLimiterEnabled`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.ingressConnectionEstablishmentRateLimiterEnabled) parameter to `true`:

   ```javascript
   db.adminCommand( {
      setParameter: 1,
      ingressConnectionEstablishmentRateLimiterEnabled: true
   } )
   ```

2. Set the connection establishment rate

   Set [`ingressConnectionEstablishmentRatePerSec`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.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.

   ```javascript
   db.adminCommand( {
      setParameter: 1,
      ingressConnectionEstablishmentRatePerSec: 20
   } )
   ```

3. Configure the connection queue depth

   Set [`ingressConnectionEstablishmentMaxQueueDepth`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.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`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.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.

4. Configure driver settings

   If you observe connection failures, decrease your driver's [`maxPoolSize`](https://www.mongodb.com/docs/manual/reference/connection-string-options.md#mongodb-urioption-urioption.maxPoolSize) and [`maxConnecting`](https://www.mongodb.com/docs/manual/reference/connection-string-options.md#mongodb-urioption-urioption.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:

- [`queues.ingressSessionEstablishment`](https://www.mongodb.com/docs/manual/reference/command/serverstatus.md#mongodb-serverstatus-serverstatus.queues.ingressSessionEstablishment)

- [`connections.establishmentRateLimit.rejected`](https://www.mongodb.com/docs/manual/reference/command/serverstatus.md#mongodb-serverstatus-serverstatus.connections.establishmentRateLimit.rejected)

If you observe increasing numbers of rejected connections, consider:

1. Increasing [`ingressConnectionEstablishmentRatePerSec`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.ingressConnectionEstablishmentRatePerSec) if the cluster appears to have sufficient CPU resources to handle this load.

2. Adjusting [`ingressConnectionEstablishmentMaxQueueDepth`](https://www.mongodb.com/docs/manual/reference/parameters.md#mongodb-parameter-param.ingressConnectionEstablishmentMaxQueueDepth) based on the formula.

3. Reviewing driver connection pool settings.
