Tuning Your Connection Pool Settings调整连接池设置

On this page本页内容

Problem问题Solution解决方案
Slow application-side operation times that are not reflected in the database server logs or the real time panel.数据库服务器日志或实时面板中未反映的应用程序端操作时间缓慢。

Use connectTimeoutMS to ensure the driver does not wait indefinitely during the connection phase.使用connectTimeoutMS确保驱动程序在连接阶段不会无限期等待。

Set connectTimeoutMS to a value greater than the longest network latency you have to a member of the set.connectTimeoutMS设置为大于对该组成员的最长网络延迟的值。

For example: if a member has a latency of 10000 milliseconds, setting connectTimeoutMS to 5000(milliseconds) prevents the driver from connecting to that member.例如:如果一个成员的延迟为10000毫秒,则将connectTimeoutMS设置为5000毫秒将阻止驱动程序连接到该成员。

A misconfigured firewall closes a socket connection incorrectly and the driver cannot detect that the connection closed improperly.配置错误的防火墙错误地关闭套接字连接,并且驱动程序无法检测到连接关闭不正确。

Use socketTimeoutMS to ensure that sockets are always closed.使用socketTimeoutMS确保插座始终关闭。

Set socketTimeoutMS to two or three times the length of the slowest operation that the driver runs.socketTimeoutMS设置为驱动程序运行的最慢操作长度的两倍或三倍。

The server logs or real time panel show that the application spends too much time creating new connections.服务器日志或实时面板显示应用程序花费太多时间创建新连接。

Not enough connections are available at startup. 启动时没有足够的连接可用。Allocate connections in the pool by setting minPoolSize.通过设置minPoolSize在池中分配连接。

Set minPoolSize to the number of connections you want to be available at startup.minPoolSize设置为启动时可用的连接数。

The MongoClient instance ensures that number of connections exists at all times.MongoClient实例确保始终存在连接数。

The load on the database is low and there's a small number of active connections at any time. 数据库上的负载很低,任何时候都有少量活动连接。Application performs fewer operations at any one time than expected.应用程序一次执行的操作比预期的少。Increase maxPoolSize, or increase the number of active threads in your application or the framework you are using.增加maxPoolSize,或增加应用程序或正在使用的框架中的活动线程数。
Database CPU usage is higher than expected. 数据库CPU使用率高于预期。The server logs or real time panel show more connection attempts than expected.服务器日志或实时面板显示的连接尝试次数超过预期。Decrease the maxPoolSize or reduce the number of threads in your application. 减少maxPoolSize或减少应用程序中的线程数。This can reduce load and response times.这可以减少负载和响应时间。
Warning警告

Do not use socketTimeoutMS to prevent long-running server operations. 不要使用socketTimeoutMS来防止长时间运行的服务器操作。Instead, use maxTimeMS() with queries so that the server can cancel long-running operations.相反,在查询中使用maxTimeMS(),以便服务器可以取消长时间运行的操作。

Calculate Maximum Number of Connections计算最大连接数

Calculate usage to find the number of operations running for each connection.计算使用率以查找每个连接运行的操作数。

Consider four application servers connecting to a replica set with three members. 考虑四个应用程序服务器连接到具有三个成员的副本集。In this scenario, each of the four application servers creates a connection pool for each replica set member.在此场景中,四个应用程序服务器中的每一个都为每个副本集成员创建一个连接池。

Calculate the maximum number of connections that are opened by each application server by multiplying maxPoolSize by the number of members.通过将maxPoolSize乘以成员数,计算每个应用程序服务器打开的最大连接数。

Calculate outgoing connections from an application to a three-member replica set:计算从应用程序到三成员副本集的传出连接:

100 (maxPoolSize default 100) x 3 (replica set members复制集成员) = 300 (outgoing connections from the application).(应用程序的传出连接)。

Calculate incoming connections from four application servers to a replica set:计算从四个应用程序服务器到副本集的传入连接:

100 (maxPoolSize default 100) x 4 (application servers应用服务器) = 400 (incoming connections to each mongod).(每个mongod的输入连接)。

←  Connection Pool OverviewDatabase Profiler →