標籤:
最近使用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)設定