redis.conf詳解之maxclients

本文基于 redis_version:6.2.5

用法

設(shè)置一個(gè)redis實(shí)例支持的最大連接數(shù)。

maxclients 10000

?

注意事項(xiàng):

  • 默認(rèn)情況下砸彬,一個(gè)實(shí)例支持1萬條連接疯攒。
  • 如果你的服務(wù)是redis-cluster集群托享,最大連接數(shù)會與內(nèi)部連接數(shù)共享俊卤。例:3主3從的cluster集群(6個(gè)實(shí)例)。每個(gè)實(shí)例會創(chuàng)建 (6-1)*2=10個(gè)內(nèi)部連接血筑。假設(shè)每個(gè)實(shí)例的maxclients配置為100绘沉,那么100條連接中會被占用10條。如果你的cluster集群很龐大豺总,尤其要注意這一點(diǎn)车伞。
  • 如果你要設(shè)置的maxclients再加上32超過資源限制(例:我要設(shè)置maxclient=1000,會用1032去判斷是否超過資源限制)喻喳,redis會根據(jù)內(nèi)部邏輯去設(shè)置實(shí)際可以支持的maxclients另玖。具體看源碼部分。

?

實(shí)操

配置一個(gè)三主的cluster集群表伦,分別為6370日矫,63806390绑榴。

$ redis-cli --cluster check 127.0.0.1:6370
127.0.0.1:6370 (3cd5de09...) -> 0 keys | 5461 slots | 0 slaves.
127.0.0.1:6390 (d4ea04d6...) -> 0 keys | 5461 slots | 0 slaves.
127.0.0.1:6380 (fad5592c...) -> 0 keys | 5462 slots | 0 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:6370)
M: 3cd5de0940dbbed9dcf0f3d9541464664dd05c3d 127.0.0.1:6370
   slots:[0-5460] (5461 slots) master
M: d4ea04d67e5e7dc8e1bae6e586c3999edbd99c16 127.0.0.1:6390
   slots:[10923-16383] (5461 slots) master
M: fad5592c85a84e2d97ea3158468594c46720c28d 127.0.0.1:6380
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

內(nèi)部連接的樣子見下圖。


cluster集群內(nèi)部連接圖示.png

以監(jiān)聽在6380的實(shí)例為例子盈魁,它的內(nèi)部連接數(shù)為(3-1)X 2 = 4條(后4行)

$ lsof -p 2688
COMMAND    PID USER   FD      TYPE DEVICE SIZE/OFF   NODE NAME
redis-ser 2688 root  cwd       DIR  0,173     4096 396203 /root/redis-cluster
...忽略部分...
redis-ser 2688 root    6u     IPv4 571968      0t0    TCP localhost:6380 (LISTEN)
redis-ser 2688 root    7wW     REG  0,173      370 397395 /root/redis-cluster/nodes-6380.conf
redis-ser 2688 root    8u     IPv4 571969      0t0    TCP localhost:16380 (LISTEN)
redis-ser 2688 root   10u     IPv4 571977      0t0    TCP localhost:46051->localhost:16370 (ESTABLISHED)
redis-ser 2688 root   11u     IPv4 572704      0t0    TCP localhost:16380->localhost:44247 (ESTABLISHED)
redis-ser 2688 root   12u     IPv4 569320      0t0    TCP localhost:45051->localhost:16390 (ESTABLISHED)
redis-ser 2688 root   13u     IPv4 569321      0t0    TCP localhost:16380->localhost:40545 (ESTABLISHED)

設(shè)置下maxclients=5(設(shè)置完畢后不要關(guān)閉該窗口和該連接翔怎,此時(shí)連接數(shù)是4個(gè)內(nèi)部連接+當(dāng)前連接=5條)

$ redis-cli -p 6380
127.0.0.1:6380> config get maxclients
1) "maxclients"
2) "10000"
127.0.0.1:6380> config set maxclients 5
OK
127.0.0.1:6380> config get maxclients
1) "maxclients"
2) "5"
127.0.0.1:6380>

另啟一個(gè)窗口,建立連接發(fā)送請求報(bào)連接超限杨耙。因?yàn)?+1=6超過了maxclients=5赤套。

$ redis-cli -p 6380
127.0.0.1:6380> get c
(error) ERR max number of clients + cluster connections reached

?

redis源碼

cluster port = 基礎(chǔ)端口+1W(例子:實(shí)例監(jiān)聽在6379端口,則cluster port為16379)珊膜,其他節(jié)點(diǎn)與cluster port建立內(nèi)部連接容握。

// https://github.com/redis/redis/blob/6.2.6/src/cluster.h
12 #define CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */

關(guān)于連接數(shù)的判斷(1101~1109行):

  • 如果是cluster集群,則判斷外部鏈接數(shù)(server.clients)+內(nèi)部連接數(shù)(clusterConn)的大小是否超過maxclients并輸出相應(yīng)錯(cuò)誤车柠。
  • 非cluster集群只判斷外部鏈接數(shù)的大小是否超過maxclients.并輸出相應(yīng)錯(cuò)誤剔氏。
// https://github.com/redis/redis/blob/6.2.6/src/networking.c
1082 static void acceptCommonHandler(connection *conn, int flags, char *ip) {
...忽略部分...
1101     if (listLength(server.clients) + getClusterConnectionsCount()
1102         >= server.maxclients)
1103     {
1104         char *err;
1105         if (server.cluster_enabled)
1106             err = "-ERR max number of clients + cluster "
1107                   "connections reached\r\n";
1108         else
1109             err = "-ERR max number of clients reached\r\n";
1110
1111         /* That's a best effort error message, don't check write errors.
1112          * Note that for TLS connections, no handshake was done yet so nothing
1113          * is written and the connection will just drop. */
1114         if (connWrite(conn,err,strlen(err)) == -1) {
1115             /* Nothing to do, Just to avoid the warning... */
1116         }
1117         server.stat_rejected_conn++;
1118         connClose(conn);
1119         return;
1120     }

內(nèi)部連接數(shù)的獲取:

  • 如果是cluster集群模式竹祷,返回(節(jié)點(diǎn)數(shù)-1)*2谈跛。節(jié)點(diǎn)數(shù)包含主節(jié)點(diǎn)與從節(jié)點(diǎn)。
  • 普通模式塑陵,返回0感憾。
// https://github.com/redis/redis/blob/6.2.6/src/networking.c
 779 unsigned long getClusterConnectionsCount(void) {
 780     /* We decrement the number of nodes by one, since there is the
 781      * "myself" node too in the list. Each node uses two file descriptors,
 782      * one incoming and one outgoing, thus the multiplication by 2. */
 783     return server.cluster_enabled ?
 784            ((dictSize(server.cluster->nodes)-1)*2) : 0;
 785 }

maxclients被設(shè)置為實(shí)際支持的數(shù)值

// https://github.com/redis/redis/blob/6.2.6/src/config.c
// CONFIG_MIN_RESERVED_FDS = 32
// 這個(gè)數(shù)字是為redis持久化、偵聽套接字令花、日志文件等額外操作保留的文件描述符數(shù)
void adjustOpenFilesLimit(void) {
    // maxfiles設(shè)置為maxclients+32
    rlim_t maxfiles = server.maxclients+CONFIG_MIN_RESERVED_FDS;
    struct rlimit limit;
    // getrlimit是一個(gè)系統(tǒng)調(diào)用阻桅,返回rlim_cur凉倚,rlim_max
    // rlim_cur是進(jìn)程資源的實(shí)際限制
    // rlim_max是rlim_cur限制的上限,rlim_cur不可超過rlim_max
    if (getrlimit(RLIMIT_NOFILE,&limit) == -1) { // 獲取失敗
        serverLog(LL_WARNING,"Unable to obtain the current NOFILE limit (%s), assuming 1024 and setting the max clients configuration accordingly.",
            strerror(errno));
        // maxclients被設(shè)置為 1024-32
        server.maxclients = 1024-CONFIG_MIN_RESERVED_FDS;
    } else {
        // 保存下來當(dāng)前獲取到的限制數(shù)
        rlim_t oldlimit = limit.rlim_cur;

        // redis所需要的fd數(shù)量超過了獲取到的限制數(shù)
        if (oldlimit < maxfiles) {
            rlim_t bestlimit;
            int setrlimit_error = 0;

            // 先設(shè)置bestlimit為redis所需要的fd數(shù)量
            bestlimit = maxfiles;
            // 如果redis所需要的fd數(shù)量一直大于獲取到的限制數(shù)嫂沉,就一直循環(huán)
            while(bestlimit > oldlimit) {
                // 每次循環(huán)將redis所需要的fd數(shù)量減16
                rlim_t decr_step = 16;

                limit.rlim_cur = bestlimit;
                limit.rlim_max = bestlimit;
                // 通過系統(tǒng)調(diào)用稽寒,去重新設(shè)置限制數(shù),設(shè)置成功跳出while循環(huán)
                if (setrlimit(RLIMIT_NOFILE,&limit) != -1) break;
                setrlimit_error = errno;

                // 如果redis所需要的fd數(shù)量比16還小了
                if (bestlimit < decr_step) {
                    // 將redis所需要的fd數(shù)量設(shè)置為最初我們拿到的限制數(shù)并跳出while循環(huán)
                    bestlimit = oldlimit;
                    break;
                }
                // 遞減16
                bestlimit -= decr_step;
            }

            // 當(dāng)前的redis所需要的fd數(shù)量比最開始拿到的限制數(shù)還小输瓜,還是用之前的限制數(shù)吧
            if (bestlimit < oldlimit) bestlimit = oldlimit;

            // 已經(jīng)處理過的redis所需要的fd數(shù)量比最開始的redis所需要的fd數(shù)量小
            if (bestlimit < maxfiles) {
                unsigned int old_maxclients = server.maxclients;
                // 設(shè)置maxclients
                server.maxclients = bestlimit-CONFIG_MIN_RESERVED_FDS;
                // 如果經(jīng)過一番操作已經(jīng)處理過的redis所需要的fd數(shù)量比17還小瓦胎,直接掛掉!
                if (bestlimit <= CONFIG_MIN_RESERVED_FDS) {
                    serverLog(LL_WARNING,"Your current 'ulimit -n' "
                        "of %llu is not enough for the server to start. "
                        "Please increase your open file limit to at least "
                        "%llu. Exiting.",
                        (unsigned long long) oldlimit,
                        (unsigned long long) maxfiles);
                    exit(1);
                }
                // 忽略部分尤揣,一些日志打印
            } else {
                // 忽略部分搔啊,一些日志打印
            }
        }
    }
}

?

原生注釋

# Set the max number of connected clients at the same time. By default
# this limit is set to 10000 clients, however if the Redis server is not
# able to configure the process file limit to allow for the specified limit
# the max number of allowed clients is set to the current file limit
# minus 32 (as Redis reserves a few file descriptors for internal uses).
#
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
#
# IMPORTANT: When Redis Cluster is used, the max number of connections is also
# shared with the cluster bus: every node in the cluster will use two
# connections, one incoming and another outgoing. It is important to size the
# limit accordingly in case of very large clusters.
#
# maxclients 10000

本文屬于原創(chuàng),首發(fā)于微信公眾號【小易哥學(xué)呀學(xué)】北戏,如需轉(zhuǎn)載請后臺留言负芋。
加微信入交流群。vx:17610015120

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末嗜愈,一起剝皮案震驚了整個(gè)濱河市旧蛾,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌蠕嫁,老刑警劉巖锨天,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異剃毒,居然都是意外死亡病袄,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門赘阀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來益缠,“玉大人,你說我怎么就攤上這事基公》牛” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵轰豆,是天一觀的道長胰伍。 經(jīng)常有香客問我,道長酸休,這世上最難降的妖魔是什么喇辽? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮雨席,結(jié)果婚禮上菩咨,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好抽米,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布特占。 她就那樣靜靜地躺著,像睡著了一般云茸。 火紅的嫁衣襯著肌膚如雪是目。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天标捺,我揣著相機(jī)與錄音懊纳,去河邊找鬼。 笑死亡容,一個(gè)胖子當(dāng)著我的面吹牛嗤疯,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播闺兢,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼茂缚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了屋谭?” 一聲冷哼從身側(cè)響起脚囊,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎桐磁,沒想到半個(gè)月后悔耘,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡我擂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年淮逊,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片扶踊。...
    茶點(diǎn)故事閱讀 40,561評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖郎任,靈堂內(nèi)的尸體忽然破棺而出秧耗,到底是詐尸還是另有隱情,我是刑警寧澤舶治,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布分井,位于F島的核電站,受9級特大地震影響霉猛,放射性物質(zhì)發(fā)生泄漏尺锚。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一惜浅、第九天 我趴在偏房一處隱蔽的房頂上張望瘫辩。 院中可真熱鬧,春花似錦、人聲如沸伐厌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽挣轨。三九已至军熏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間卷扮,已是汗流浹背荡澎。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留晤锹,地道東北人摩幔。 一個(gè)月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像抖甘,于是被迫代替她去往敵國和親热鞍。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評論 2 359

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