Do you want a timeout?
You ' re feeling accomplished and excited; The new features for your application is finished, committed, and deployed. Expecting your shiny new app on your screen. Unexpectedly, the app errors out or hangs indefinitely.
What ' s going on?!
Just had your first direct encounter with the subtleties of your ' driver ' options.
At a high level, whenever you create or use a Mongoclient object to talk to your database, the driver establishes a Connec tion to your MongoDB server. How long your application waits for the initial connection to being established and how long it waits for responses to Subseq Uent Requests is determined by the values of your connection and socket timeouts.
Connection Timeout
The connection timeout value determines the maximum amount of time your driver would wait for a connection to be establishe D with the server.
This value was only used when making a initial connection to your database, and so selecting the correct setting for this Timeout can be a balancing act.
On the one hand your want to make sure that your connection timeout was high enough that your application can reliably estab Lish a connection to the database server, even with the face of the high server load or intermittent network lag. On the other hand you don't want the timeout to being so large it your application "hangs" for an inordinate amount of time While the IT waits to connect to a server, the may be temporarily unreachable; In this case you'll want to propagate the "error state" up to your users "a relatively timely matter and too high a time Out can make this difficult.
The default driver connection timeout value ranges anywhere from 1 second (e.g. the node. JS driver in some cases) to + SE Conds (e.g the Ruby driver), so are really need to think about what the optimal setting are for your use case.
Our suggestions for the connection timeout
We suggest starting with a relatively low timeout value and slowly increasing it if you face regular connection timeouts. Generally, you'll want to use a connection timeout of 5 seconds.
For connections made through a platform-as-a-serivce (PaaS) such as Heroku, you might consider an even higher timeout (e.g . seconds) Since your application is likely running in a container so can be "idled" or "passivated" during periods of Low activity. In such cases it could take longer for your code to establish new database connections when your application was made active Again.
Of course, we also recommend that your use connection pooling so that's is seldom creating new connections to the Databa Se server ... (But this'll be the topic of another post:)
Socket Timeout
The socket timeout option specifies to your driver how long to wait for responses from the server. This timeout governs all types of requests (queries, writes, commands, authentication, etc.).
For example, if you had this timeout set to seconds, your driver would never wait more than a seconds for the result O f a query (although the query would continue to run to completion on the server). While there is some types of operations where you might consider defining a hard-timeout (e.g. authentication), you usual ly don ' t want to limit the amount of time your database operations take as they can inherently variable.
The default socket timeout value for most drivers are infinite (i.e. no timeout) which is usually expressed as 0 or a null Value.
Our suggestions for the socket timeout
We suggest leaving this setting on the default (i.e. no timeout) unless you have good reason to change it. That is said, some drivers allow you to control the setting on a per operation basis, in which case you may consider fine-tu Ning this setting based on your knowledge of specific operations.
Further your knowledge
Each MongoDB driver implements the Mongoclient class and these network timeout options differently. If you would like to investigate these timeout options further, you can peruse each specific driver ' s documentation and so Urce code for details on how to configure the these options with the that driver.
These timeouts is only a many MongoDB driver options available to you. We encourage you to explore and learn more on your driver configuration settings so then you may fine-tune your app wit h optimal settings based on your.
When in doubt, you can drop us a line anytime at [email protected]. With Mongo Power comes Mongo responsibility!
Do you want a timeout?