Do you want a timeout?

Do you want a timeout?

You’re feeling accomplished and excited; the new features for your application are finished, committed, and deployed. You fire it up, expecting your shiny new app on your screen. Unexpectedly, the app errors out or hangs indefinitely.

What’s going on?!

You may have just had your first direct encounter with the subtleties of your MongoDB driver’sconnection options.

At a high level, whenever you create or use a MongoClient object to talk to your database, the driver establishes a connection to your MongoDB server. How long your application waits for the initial connection to be established and how long it waits for responses to subsequent 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 will wait for a connection to be established with the server.

This value is only used when making an initial connection to your database, and so selecting the correct setting for this timeout can be a balancing act.

On the one hand you want to make sure that your connection timeout is high enough that your application can reliably establish a connection to the database server, even in the face of high server load or intermittent network lag. On the other hand you don’t want the timeout to be so large that your application “hangs” for an inordinate amount of time while it waits to connect to a server that may be temporarily unreachable; in this case you will want to propagate the error state up to your users in a relatively timely matter and too high a timeout 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 30 seconds (e.g  the Ruby driver), so you really need to think about what the optimal setting is 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. 30 seconds) since your application is likely running in a container that can be “idled” or “passivated” during periods of low activity. In such cases it may take longer for your code to establish new database connections when your application is made active again.

Of course, we also recommend that you use connection pooling so that you are seldom creating new connections to the database server… (but this will 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 have this timeout set to 30 seconds, your driver will never wait more than 30 seconds for the result of a query (although the query will continue to run to completion on the server). While there are some types of operations where you might consider defining a hard timeout (e.g. authentication), you usually don’t want to limit the amount of time your database operations take as they can be inherently variable.

The default socket timeout value for most drivers is 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 at the default (i.e. no timeout) unless you have good reason to change it. That said, some drivers allow you to control this setting on a per operation basis, in which case you may consider fine-tuning 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 source code for details on how to configure these options with that driver.

These timeouts are only two of many MongoDB driver options available to you. We encourage you to explore and learn more about your driver configuration settings so that you may fine-tune your app with optimal settings based on your use case.

When in doubt, you can drop us a line anytime at [email protected]. With Mongo power comes Mongo responsibility!

时间: 2024-08-04 19:20:41

Do you want a timeout?的相关文章

Java_Hbase Timeout issue

设置参数hbase.rpc.timeout <property><name>hbase.regionserver.lease.period</name><value>180000</value></property> <property> <name>zookeeper.session.timeout</name> <value>120000</value> </pro

mysql Lock wait timeout exceeded

MySQL 事务没有提交导致 锁等待 Lock wait timeout exceeded java.lang.Exception:### Error updating database.  Cause: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction### The error may involve defaultParameterMap### The error occurred wh

cisco vpn timeout

Configuration > Remote Access VPN > Network (Client) Access > Advanced > Endpoint Security > Global NAC Parameters cisco vpn timeout,布布扣,bubuko.com

AngularJS中$timeout和$interval的用法详解

1. 先将$interval,$timeout,作为参数注入到controller中,例如rds.controller('controllerCtrl', ['app', '$scope','$http','$routeParams','$filter','$location','$interval','$timeout', function (app, $scope,$http,$routeParams,$filter,$location,$interval,$timeout) {2.在需要用

使用puTTY或Xshell连接阿里云TimeOut超时

根据网上很多主流的说法,我依次检查了 ssh: service sshd status 防火墙:service iptables stop (CentOS 7好像已经没有这个iptable了) 都没有什么问题,最后我在Windows端 Telnet了以下服务器,还是TimeOut. 这个时候我注意到了阿里云有一个自带的安全组,设置了一下就可以了. 具体如下: 当然阿里云本身在这方面有很健全的文档,更多的需求可以查看下面的链接: 安全组应用案例

go get timeout解决Ubuntu16.04VPN配置

博主在使用Micro开源框架里的例子时go get github.com/micro/examples/service,service这个例子依赖的golang.org/x/net目录库全被墙了报一堆timeout.所以写一篇Ubuntu16.04下VPN配置的步骤: 申请一个VPN账号 以这个网站:http://www.samiman.com/index.html为例子. 先购买一个最便宜的包月账号:TerryTong914,密码:XXX,再http://www.samiman.com/xia

解决 Docker pull 出现的net/http: TLS handshake timeout 的一个办法

docker pull 时发生以下错误: 原因:不可描述(政府屏蔽了?) 解决思路:百度搜了下net/http: TLS handshake timeout 出现一个这个结果比较满意 http://dockone.io/article/876?utm_source=tuicool&utm_medium=referral 我不用官方的dockhub了,转而使用国内的仓库daocloud $ echo "DOCKER_OPTS=\"\$DOCKER_OPTS --registry-

Batch containing 1 record(s) expired due to timeout while requesting metadata from brokers

一.异常信息如下: java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.TimeoutException: Batch containing 1 record(s) expired due to timeout while requesting metadata from brokers for user-video-0 at org.apache.kafka.clients.producer.inter

更改pip安装源的镜像解决安装总是timeout的情况

由于国外的pip源总是由于各种原因不能被访问或者网速过慢,而造成的timeout错误 解决方法是修改pip的配置文件(如果没有配置文件在相应的地方新建,配置文件的路径和名字参考这里),设置安装包时候访问的镜像,将其改为国内的镜像 然后设置timeout的时间长一些 例如配置文件设置如下: [global]timeout = 60index-url = http://pypi.douban.com/simple [install]use-mirrors = truemirrors = http:/

如何解决修改SQLServer表结构时 ,Timeout 时间已到

背景:最近修改一张有海量数据的SQLServer表结构,报错[无法修改表. Timeout 时间已到. 在操作完成之前超时时间已过或服务器未响应. ] [解决办法]:  工具->选项>设计器->表设计器和数据库设计器->右侧勾选"为表设计器更新重写连接字符串的超时值",在它下面的"事务超时时间"默认应该是 30 秒,改得稍微大一些,不过好像不能超过65535.