【Azure Redis】Redis客戶端出現(xiàn)15分鐘的超時異常

問題描述

客戶端使用 Lettuce.io 連接 Azure Redis,出現(xiàn)了長達(dá)15分鐘的Timeout異常阵难。

問題解答

Azure Redis作為PaaS服務(wù)岳枷,由于一些平臺的升級操作而引發(fā)的故障轉(zhuǎn)移(Failover)。 如Redis的客戶端時部署在Linux服務(wù)器上呜叫,則可能導(dǎo)致長達(dá)15分鐘無法重新連接的問題空繁。

某些 Linux 版本中的默認(rèn) TCP 設(shè)置可能會導(dǎo)致 Redis 服務(wù)器連接失敗 13 分鐘或更長時間。 默認(rèn)設(shè)置可以防止客戶端應(yīng)用程序檢測關(guān)閉的連接朱庆,并在連接未正常關(guān)閉的情況下防止自動還原這些關(guān)閉的連接盛泡。

如果網(wǎng)絡(luò)連接中斷或 Redis 服務(wù)器脫機(jī)進(jìn)行計劃外維護(hù),重新建立連接可能會失敗娱颊。

目前Lettuce社區(qū)已知問題傲诵,在server端未發(fā)RST斷開服務(wù)的場景下,Lettuce自恢復(fù)需要15+分鐘的時間箱硕。https://github.com/lettuce-io/lettuce-core/issues/2082

目前已知有效的方式是修改linux tcp_retries參數(shù)拴竹,https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-best-practices-connection#tcp-settings-for-linux-hosted-client-applications

此外,Lettuce社區(qū)也有一些解決方案剧罩,https://github.com/lettuce-io/lettuce-core/issues/2082#issuecomment-1407609439

附錄: Connection does not re-establish for 15 minutes when running on Linux

Connection stalls lasting for 15 minutes like this are often caused by very optimistic default TCP settings in some Linux distros (confirmed on CentOS so far). When a server stops responding without gracefully closing the connection, the client TCP stack will continue retransmitting packets for 15 minutes before declaring the connection dead and allowing the StackExchange.Redis reconnect logic to kick in.

With Azure Cache for Redis, it's fairly easy to reproduce this by rebooting nodes as mentioned above. In this case, the machine goes down abruptly and the Redis server isn't able to transmit a FIN packet to the client. The client TCP stack continues retransmitting on the same socket hoping the server will come back up. Even when the node has rebooted and come back, it has no record of that connection so it continues ignoring the client. If the client gave up and created a NEW connection, it would be able to resume communication with the server much sooner than 15 minutes.

As you found, there are TCP settings you can change on the client machine to force it to timeout the connection sooner and allow for reconnect. In addition to tcp_retries2, you can try tuning the keepalive settings as discussed here: lettuce-io/lettuce-core#1428 (comment). It should be safe to reduce these timeouts to more realistic durations machine-wide unless you have systems that actually depend on the unusually long retransmits.

An additional approach is using the ForceReconnect pattern recommended in the Azure best practices. If you're seeing issues like this, it's perfectly appropriate to trigger reconnect on RedisTimeoutExceptions in addition to RedisConnectionExceptions. Just don't be too aggressive with it because an overloaded server can also result in persistent RedisTimeoutExceptions. Recreating connections in that situation can cause additional server load and a cascade failure.

Unfortunately there's not much the StackExchange.Redis library can do about this situation, because the Linux TCP stack is hiding the lost connection. Detecting the stall at the library level would require making assumptions that would almost certainly lead to false positives in some scenarios. Instead, it's better for the client application to implement some detection/reconnection logic based on what it knows about its load and latency patterns.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末栓拜,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌幕与,老刑警劉巖挑势,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異纽门,居然都是意外死亡薛耻,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門赏陵,熙熙樓的掌柜王于貴愁眉苦臉地迎上來饼齿,“玉大人,你說我怎么就攤上這事蝙搔÷聘龋” “怎么了?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵吃型,是天一觀的道長证鸥。 經(jīng)常有香客問我,道長勤晚,這世上最難降的妖魔是什么枉层? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮赐写,結(jié)果婚禮上鸟蜡,老公的妹妹穿的比我還像新娘。我一直安慰自己挺邀,他們只是感情好贷笛,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布米同。 她就那樣靜靜地躺著珍德,像睡著了一般旦棉。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上禾蚕,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天您朽,我揣著相機(jī)與錄音,去河邊找鬼换淆。 笑死虚倒,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的产舞。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼菠剩,長吁一口氣:“原來是場噩夢啊……” “哼易猫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起具壮,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤准颓,失蹤者是張志新(化名)和其女友劉穎哈蝇,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體攘已,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡炮赦,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了样勃。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片吠勘。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖峡眶,靈堂內(nèi)的尸體忽然破棺而出剧防,到底是詐尸還是另有隱情,我是刑警寧澤辫樱,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布峭拘,位于F島的核電站,受9級特大地震影響狮暑,放射性物質(zhì)發(fā)生泄漏鸡挠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一搬男、第九天 我趴在偏房一處隱蔽的房頂上張望拣展。 院中可真熱鬧,春花似錦止后、人聲如沸瞎惫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽瓜喇。三九已至,卻和暖如春歉糜,著一層夾襖步出監(jiān)牢的瞬間乘寒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工匪补, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留伞辛,地道東北人。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓夯缺,卻偏偏與公主長得像蚤氏,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子踊兜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容