简体   繁体   English

了解 MongoDB 超时设置

[英]Understanding MongoDB timeout settings

I am having a hard time understanding what is the best timeout settings to configure for my application There are three types of timeout settings:我很难理解为我的应用程序配置的最佳超时设置是三种类型的超时设置:

connectTimeoutMS socketTimeoutMS maxTimeMS connectTimeoutMS socketTimeoutMS maxTimeMS

and along with them we also have:与他们一起,我们还有:

KeepAlive poolSize autoReconnect KeepAlive poolSize autoReconnect

connectTimeout corresponds to the timeout setting when the application fails to connect to mongoDB within the specificed timeframe. connectTimeout 对应于应用程序在指定时间范围内无法连接到 mongoDB 时的超时设置。 I assume this will result into an exception.我认为这会导致异常。

socketTimeoutMS corresponds to the socket waits to get a response from the db server before closing. socketTimeoutMS 对应于套接字在关闭之前等待从数据库服务器获得响应。

maxTimeMS corresponds to the timeout of an operation running in the db. maxTimeMS 对应于在数据库中运行的操作的超时时间。 This results in an exception.这导致异常。

What is the difference between socketTimeout and maxTimeout?. socketTimeout 和 maxTimeout 有什么区别? I have the requirement that I have to log an error code for when Request to Database has timed out after {5} seconds.我要求我必须在 {5} 秒后请求数据库超时时记录错误代码。 No response is received from this Database.没有收到来自该数据库的响应。 What is the best timeout setting for this purpose?为此目的,最佳超时设置是什么?

How is poolsize, keepAlive, auto connected conected to the timeout settings? poolsize、keepAlive、自动连接如何连接到超时设置? Examples would be useful.例子会很有用。

Let's say you have a mongod running.假设你有一个 mongod 正在运行。

You connect to it, and run a query that scans a collection with millions of documents, not using an index.您连接到它,并运行一个查询来扫描包含数百万文档的集合,而不是使用索引。

You would get the exception for maxTimeMS, because the query is running longer than that.您会得到 maxTimeMS 的异常,因为查询运行的时间比这更长。

Let's say your mongod becomes very overloaded.假设您的 mongod 变得非常超载。 Your driver is still connected to the socket, but when you send another query, it times out waiting for the db to respond to the socket.您的驱动程序仍然连接到套接字,但是当您发送另一个查询时,等待数据库响应套接字超时。 That's what the socketTimeoutMS parameter controls.这就是 socketTimeoutMS 参数控制的。

There's some good information at https://developer.mongodb.com/community/forums/t/connect-timeout-and-execution-timeout-in-nodejs-driver/2129/2 https://developer.mongodb.com/community/forums/t/connect-timeout-and-execution-timeout-in-nodejs-driver/2129/2上有一些很好的信息

Also at https://docs.mongodb.com/drivers/node/faq#what-is-the-difference-between-connecttimeoutms-sockettimeoutms-and-maxtimems同样在https://docs.mongodb.com/drivers/node/faq#what-is-the-difference-between-connecttimeoutms-sockettimeoutms-and-maxtimems

Keepalive is at https://docs.mongodb.com/drivers/node/faq#what-does-the-keepalive-setting-do Keepalive 位于https://docs.mongodb.com/drivers/node/faq#what-does-the-keepalive-setting-do

And if you find in page for 'poolsize' on that faq page you'll see the details about that.如果您在该常见问题页面上找到“poolsize”页面,您将看到有关它的详细信息。

The simplest timeout to explain is socket timeout .最简单的超时解释是套接字超时 Whenever a read or write is performed on a socket, if that read or write takes longer than the timeout the operation would fail with an error.每当对套接字执行读取或写入操作时,如果读取或写入操作的时间超过超时时间,则操作将失败并显示错误。

However, setting the socket timeout is far from simple.但是,设置套接字超时远非简单。 When a query is executed, the server does not reply until it has at least one document (or it determines that no documents match).当一个查询被执行时,服务器直到它至少有一个文档(或者它确定没有文档匹配)才会回复。 This means that the socket timeout must be set at least as high as the longest running query the application will issue .这意味着套接字超时必须设置为至少与应用程序将发出的最长运行查询一样高

Plus, a single query may involve multiple reads or writes.另外,单个查询可能涉及多次读取或写入。 Therefore a slow network could conceivably not actually trigger a timeout.因此可以想象,慢速网络实际上不会触发超时。

The solution to this are max time and keep-alives:对此的解决方案是最大时间和保持活动:

  • Max time limits how long the query executes on the server .最长时间限制查询在服务器上执行的时间 When this limit is exceeded, the server returns an error.当超过此限制时,服务器将返回错误。 The application then knows that the query took too long as opposed to there being a network issue.然后应用程序知道查询花费的时间太长,而不是存在网络问题。 Additionally, long-running queries that the client stops waiting for are not left running on the server until completion which could take quite some time.此外,客户端停止等待的长时间运行的查询在完成之前不会在服务器上运行,这可能需要相当长的时间。
  • Keep-alive is a TCP feature that periodically sends "pings" from one end of connection to the other to identify dead connections.保持活动是一种TCP 功能,它定期从连接的一端向另一端发送“ping”以识别死连接。 The keep-alive settings are somewhat more complicated to set, but are intended to permit long running queries to complete successfully while detecting network errors relatively quickly.保持活动设置的设置稍微复杂一些,但旨在允许长时间运行的查询成功完成,同时相对较快地检测网络错误 All MongoDB drivers configure keep-alives to a reasonable default (about 2 minutes), you can lower the interval if you like.所有 MongoDB 驱动程序都将 keep-alives 配置为合理的默认值(大约 2 分钟),您可以根据需要降低间隔。

Connect timeout is similar to socket timeout but applies when a connection is first established.连接超时类似于套接字超时,但适用于首次建立连接时。 As such it can be set lower than socket timeout because it doesn't need to be bounded by query execution time.因此,它可以设置为低于套接字超时,因为它不需要受查询执行时间的限制。 By setting a lower connect timeout dead servers can be detected faster when they are being connected to the first time.通过设置较低的连接超时,可以在第一次连接时更快地检测到死服务器。

Pool size doesn't really have anything to do with timeouts, and auto reconnect is a driver-specific option that also isn't really in the same category as timeouts.池大小与超时没有任何关系,自动重新连接是一个特定于驱动程序的选项,它也与超时不在同一类别中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM