Fernando
Fernando

Reputation: 433

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

and along with them we also have:

KeepAlive poolSize autoReconnect

connectTimeout corresponds to the timeout setting when the application fails to connect to mongoDB within the specificed timeframe. I assume this will result into an exception.

socketTimeoutMS corresponds to the socket waits to get a response from the db server before closing.

maxTimeMS corresponds to the timeout of an operation running in the db. This results in an exception.

What is the difference between socketTimeout and maxTimeout?. I have the requirement that I have to log an error code for when Request to Database has timed out after {5} seconds. 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? Examples would be useful.

Upvotes: 1

Views: 23014

Answers (2)

D. SM
D. SM

Reputation: 14480

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. 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.

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.

Upvotes: 6

Sheeri
Sheeri

Reputation: 608

Let's say you have a mongod running.

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.

Let's say your mongod becomes very overloaded. 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.

There's some good information at 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

Keepalive is at 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.

Upvotes: 3

Related Questions