This procedure describes how to configure rate limiting using the 此过程描述了如何使用MongoDB中的ingressConnectionEstablishmentRateLimiterEnabled parameter in MongoDB. The rate limiter helps protect your deployment from connection storms that can impact system stability and performance.ingressConnectionEstablishmentRateLimiterEnabled参数配置速率限制。速率限制器有助于保护部署免受可能影响系统稳定性和性能的连接风暴的影响。
Before You Begin开始之前
Before you begin this procedure, ensure you meet the following requirements:在开始此过程之前,请确保满足以下要求:
You have administrative privileges on your MongoDB deployment您对MongoDB部署拥有管理权限You understand your system's connection load patterns and requirements您了解系统的连接负载模式和要求You know your driver's您知道驱动程序的connectTimeoutMSsettingconnectTimeoutMS设置
Procedure过程
Enable the rate limiter启用速率限制器
To begin using the rate limiter, first enable it by setting the 要开始使用速率限制器,首先通过将ingressConnectionEstablishmentRateLimiterEnabled parameter to true:ingressConnectionEstablishmentRateLimiterEnabled参数设置为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.ingressConnectionEstablishmentRatePerSec设置为集群可以处理的最大速率,而不会对吞吐量产生负面影响。这应该是一个固定的数字,不会因其他工作负载动态而变化。
db.adminCommand( {
setParameter: 1,
ingressConnectionEstablishmentRatePerSec: 20
} )Configure the connection queue depth配置连接队列深度
Set 根据以下公式设置ingressConnectionEstablishmentMaxQueueDepth according to the following formula:ingressConnectionEstablishmentMaxQueueDepth:
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+authattempts succeed, rather than failing due to a timeout error.hello+auth尝试都会成功,而不是由于超时错误而失败。
The averageTimeQueuedMicros metric can assist in tuning ingressConnectionEstablishmentMaxQueueDepth, as it equals approximately (maxQueueDepth / establishRatePerSec) * 1e6.averageTimeQueuedMicros度量可以帮助调整ingressConnectionEstablishmentMaxQueueDepth,因为它大约等于(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. maxPoolSize和maxConnecting设置。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.maxPoolSize和maxConnecting,查询可能会在驱动程序层花费更长的时间等待连接返回到池中。减少这些设置有助于在负载高峰期间以增加延迟为代价来保持可用性。
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如果集群似乎有足够的CPU资源来处理此负载,则增加ingressConnectionEstablishmentRatePerSecif the cluster appears to have suffiecient CPU resources to handle this load.ingressConnectionEstablishmentRatePerSec。Adjusting根据公式调整ingressConnectionEstablishmentMaxQueueDepthbased on the formula.ingressConnectionEstablishmentMaxQueueDepth。Reviewing driver connection pool settings.正在查看驱动程序连接池设置。