[轉]redis伺服器與用戶端保活參數(tcp-keepalive)設定

來源:互聯網
上載者:User

標籤:

最近使用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)

[轉]redis伺服器與用戶端保活參數(tcp-keepalive)設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.