SocketTimeout with opened connection in MongoDB

后端 未结 2 964
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-15 08:37

I\'ve a Java application that performs some aggregations on MongoDB, but sometimes it just hangs and throw a SocketTimeout exception. After the Exception the app will run ju

相关标签:
2条回答
  • 2020-12-15 09:21

    After few tries I've found out that it was a problem with the Azure's Load Balancer.
    After 60s of inactivity it will disconnect any pending TCP connection.

    After further digging I've found this post of the MongoDB diagnostics FAQ, and I've set the tcp keepalive to 120s:

    sudo sysctl -w net.ipv4.tcp_keepalive_time=<value>
    

    and I've also set the socketKeepAlive of the MongoClient to true:

    MongoClientOptions.Builder options = MongoClientOptions.builder();
    options.socketKeepAlive(true);
    mongoClient = new MongoClient(mongoAddress, options.build());
    

    After these fixes the issue seems gone!

    0 讨论(0)
  • 2020-12-15 09:30

    If you experience socket errors between clients and servers or between members of a sharded cluster or replica set that do not have other reasonable causes, check the TCP keepalive value (for example, the tcp_keepalive_time value on Linux systems). A common keepalive period is 7200 seconds (2 hours); however, different distributions and macOS may have different settings.

    For MongoDB, you will have better results with shorter keepalive periods, on the order of 120 seconds (two minutes).

    Where you have installed mongodb you have to simply run this command on Linux

    sudo sysctl -w net.ipv4.tcp_keepalive_time=120

    Reference: Does TCP keepalive time affect MongoDB Deployments?

    0 讨论(0)
提交回复
热议问题