zoukankan      html  css  js  c++  java
  • [转]redis服务器与客户端保活参数(tcp-keepalive)设置

    最近使用redis的list做跨进程的消息队列,客户端使用的是redis-cplusplus-client.这个client库还是蛮好用的,提供了和redis命令行一致的接口,很方便。

    使用过程中发现下面一个问题。

    我有多个客户端连接同一数据库,client从redis中blpop数据,设置超时为5秒。按说没什么问题,客户端也不多,不会对数据库造成什么压力。但运行一段时间后,client就从redis取不到数据了。

    首先想到的是,是不是数据库连接断开了。从redis和client两侧查看6379端口的tcp连接,发现确实部分client的连接没了。但是客户端竟然没有异常,还在那傻傻的blpop!看来这个第三方库还是做得不够完善。

    一个解决办法是,client和redis之间不保持长连接,每次操作都重新连接。可行,但是too simple。

    后来查看了一下redis的配置文件,发现有一个tcp-keepalive的选项。

    # TCP keepalive.
    #
    # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
    # of communication. This is useful for two reasons:
    #
    # 1) Detect dead peers.
    # 2) Take the connection alive from the point of view of network
    # equipment in the middle.
    #
    # On Linux, the specified value (in seconds) is the period used to send ACKs.
    # Note that to close the connection the double of the time is needed.
    # On other kernels the period depends on the kernel configuration.
    #
    # A reasonable value for this option is 60 seconds.
    tcp-keepalive 0

    就是用来定时向client发送tcp_ack包来探测client是否存活的。默认不探测,官方建议值为60秒。那就试试吧。

    如此设置,观察一段时间后发现client和redis之间的连接一直保持着。管用!

    更多我的文章,请访问:零一积流(www.it-refer.com)

  • 相关阅读:
    java基础35 双例集合Map及其常用方法
    java基础34 泛型的使用
    java基础33 Set集合下的HashSet集合和TreeSet集合
    java基础32 List集合下的ArrayList集合
    部分日期时间函数
    SQL语句常见视图操作部分试题(一)
    高级子查询常见用法及举例
    DDL/DML/DCL区别概述
    SQL语句常见DDL/DML/DCL操作部分试题(一)
    Create database 创建数据库
  • 原文地址:https://www.cnblogs.com/rd-log/p/4651731.html
Copyright © 2011-2022 走看看