INCLUDES
################################## INCLUDES ###################################
# Include one or more other config files here. This is useful if you
# have a standard template that goes to all Redis servers but also need
# to customize a few per-server settings. Include files can include
# other files, so use this wisely.
#
# Note that option "include" won't be rewritten by command "CONFIG REWRITE"
# from admin or Redis Sentinel. Since Redis always uses the last processed
# line as value of a configuration directive, you'd better put includes
# at the beginning of this file to avoid overwriting config change at runtime.
#
# If instead you are interested in using includes to override configuration
# options, it is better to use include as the last line.
#
# include /path/to/local.conf
# include /path/to/other.conf
我們知道Redis只有一個配置文件在刺,如果多個人進(jìn)行開發(fā)維護(hù)头镊,那么就需要多個這樣的配置文件蚣驼,這時候多個配置文件就可以在此通過 include /path/to/local.conf 配置進(jìn)來相艇,而原本的 redis.conf 配置文件就作為一個總閘,如果將此配置寫在redis.conf 文件的開頭,那么后面的配置會覆蓋引入文件的配置坛芽,如果想以引入文件的配置為主,那么需要將 include 配置寫在 redis.conf 文件的末尾咙轩。
MODULES
################################## MODULES #####################################
# Load modules at startup. If the server is not able to load modules
# it will abort. It is possible to use multiple loadmodule directives.
#
# loadmodule /path/to/my_module.so
# loadmodule /path/to/other_module.so
通過這里的 loadmodule 配置將引入自定義模塊來新增一些功能。
NETWORK
################################## NETWORK #####################################
# By default, if no "bind" configuration directive is specified, Redis listens
# for connections from all available network interfaces on the host machine.
# It is possible to listen to just one or multiple selected interfaces using
# the "bind" configuration directive, followed by one or more IP addresses.
# Each address can be prefixed by "-", which means that redis will not fail to
# start if the address is not available. Being not available only refers to
# addresses that does not correspond to any network interfece. Addresses that
# are already in use will always fail, and unsupported protocols will always BE
# silently skipped.
bind 127.0.0.1
protected-mode yes
port 6379
timeout 0
tcp-keepalive 300
1活喊、bind:綁定redis服務(wù)器網(wǎng)卡IP,默認(rèn)為127.0.0.1,即本地回環(huán)地址钾菊。如果未設(shè)置由于protected-mode=yes 依舊會使用本地回環(huán)地址
2、port:指定redis運行的端口煞烫,默認(rèn)是6379。
3滞详、timeout:設(shè)置客戶端連接時的超時時間喘落,單位為秒。當(dāng)客戶端在這段時間內(nèi)沒有發(fā)出任何指令,那么關(guān)閉該連接稀火。默認(rèn)值為0,表示不關(guān)閉凰狞。
4、tcp-keepalive :單位是秒赡若,表示將周期性的使用SO_KEEPALIVE檢測客戶端是否還處于健康狀態(tài),避免服務(wù)器一直阻塞逾冬,官方給出的建議值是300s,如果設(shè)置為0身腻,則不會周期性的檢測。
GENERAL
################################# GENERAL #####################################
# By default Redis does not run as a daemon. Use 'yes' if you need it.
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
# When Redis is supervised by upstart or systemd, this parameter has no impact.
daemonize no
# If you run Redis from upstart or systemd, Redis can interact with your
# supervision tree. Options:
# supervised no - no supervision interaction
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# requires "expect stop" in your upstart job config
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# on startup, and updating Redis status on a regular
# basis.
**1嘀趟、daemonize:設(shè)置為yes表示指定Redis以守護(hù)進(jìn)程的方式啟動(后臺啟動)。默認(rèn)值為 no
2她按、pidfile:配置PID文件路徑,當(dāng)redis作為守護(hù)進(jìn)程運行的時候酌泰,它會把 pid 默認(rèn)寫到 /var/redis/run/redis_6379.pid 文件里面
3、loglevel :定義日志級別宫莱。默認(rèn)值為notice,有如下4種取值:
debug(記錄大量日志信息授霸,適用于開發(fā)、測試階段)
verbose(較多日志信息)
notice(適量日志信息碘耳,使用于生產(chǎn)環(huán)境)
warning(僅有部分重要、關(guān)鍵信息才會被記錄)
4辛辨、logfile :配置log文件地址,默認(rèn)打印在命令行終端的窗口上
5瑟枫、databases:設(shè)置數(shù)據(jù)庫的數(shù)目。默認(rèn)的數(shù)據(jù)庫是DB 0 指攒,可以在每個連接上使用select <dbid> 命令選擇一個不同的數(shù)據(jù)庫,dbid是一個介于0到databases - 1 之間的數(shù)值允悦。默認(rèn)值是 16,也就是說默認(rèn)Redis有16個數(shù)據(jù)庫隙弛。**
SNAPSHOTTING
################################ SNAPSHOTTING ################################
# Save the DB to disk.
#
# save <seconds> <changes>
#
# Redis will save the DB if both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# Snapshotting can be completely disabled with a single empty string argument
# as in following example:
#
# save ""
#
# Unless specified otherwise, by default Redis will save the DB:
# * After 3600 seconds (an hour) if at least 1 key changed
# * After 300 seconds (5 minutes) if at least 100 keys changed
# * After 60 seconds if at least 10000 keys changed
#
# You can set these explicitly by uncommenting the three following lines.
#
# save 3600 1
# save 300 100
# save 60 10000
1、save:這里是用來配置觸發(fā) Redis的持久化條件叉寂,也就是什么時候?qū)?nèi)存中的數(shù)據(jù)保存到硬盤。
save 900 1:表示900 秒內(nèi)如果至少有 1 個 key 的值變化总珠,則保存
save 300 10:表示300 秒內(nèi)如果至少有 10 個 key 的值變化,則保存
save 60 10000:表示60 秒內(nèi)如果至少有 10000 個 key 的值變化局服,則保存
當(dāng)然如果你只是用Redis的緩存功能,不需要持久化腌逢,那么你可以注釋掉所有的 save 行來停用保存功能〔龋可以直接一個空字符串來實現(xiàn)停用:save ""
2、stop-writes-on-bgsave-error :默認(rèn)值為yes媒惕。當(dāng)啟用了RDB且最后一次后臺保存數(shù)據(jù)失敗,Redis是否停止接收數(shù)據(jù)妒蔚。這會讓用戶意識到數(shù)據(jù)沒有正確持久化到磁盤上,否則沒有人會注意到災(zāi)難(disaster)發(fā)生了肴盏。如果Redis重啟了,那么又可以重新開始接收數(shù)據(jù)了
3菜皂、rdbcompression ;默認(rèn)值是yes恍飘。對于存儲到磁盤中的快照谴垫,可以設(shè)置是否進(jìn)行壓縮存儲。如果是的話翩剪,redis會采用LZF算法進(jìn)行壓縮。如果你不想消耗CPU來進(jìn)行壓縮的話彩郊,可以設(shè)置為關(guān)閉此功能,但是存儲在磁盤上的快照會比較大焦辅。
4椿胯、rdbchecksum :默認(rèn)值是yes。在存儲快照后哩盲,我們還可以讓redis使用CRC64算法來進(jìn)行數(shù)據(jù)校驗,如果希望獲取到最大的性能提升廉油,可以關(guān)閉此功能。
5抒线、dbfilename :設(shè)置快照的文件名,默認(rèn)是 dump.rdb
6嘶炭、dir:設(shè)置快照文件的存放路徑,這個配置項一定是個目錄眨猎,而不能是文件名抑进。使用上面的 dbfilename 作為保存的文件名睡陪。
REPLICATION
################################# REPLICATION #################################
# Master-Replica replication. Use replicaof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
#
# +------------------+ +---------------+
# | Master | ---> | Replica |
# | (receive writes) | | (exact copy) |
# +------------------+ +---------------+
#
# 1) Redis replication is asynchronous, but you can configure a master to
# stop accepting writes if it appears to be not connected with at least
# a given number of replicas.
# 2) Redis replicas are able to perform a partial resynchronization with the
# master if the replication link is lost for a relatively small amount of
# time. You may want to configure the replication backlog size (see the next
# sections of this file) with a sensible value depending on your needs.
# 3) Replication is automatic and does not need user intervention. After a
# network partition replicas automatically try to reconnect to masters
# and resynchronize with them.
#
# replicaof <masterip> <masterport>
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
①、slave-serve-stale-data:默認(rèn)值為yes兰迫。當(dāng)一個 slave 與 master 失去聯(lián)系,或者復(fù)制正在進(jìn)行的時候汁果,slave 可能會有兩種表現(xiàn):
1) 如果為 yes ,slave 仍然會應(yīng)答客戶端請求须鼎,但返回的數(shù)據(jù)可能是過時府蔗,或者數(shù)據(jù)可能是空的在第一次同步的時候
2) 如果為 no 汞窗,在你執(zhí)行除了 info he salveof 之外的其他命令時姓赤,slave 都將返回一個 "SYNC with master in progress" 的錯誤
②仲吏、slave-read-only:配置Redis的Slave實例是否接受寫操作不铆,即Slave是否為只讀Redis裹唆。默認(rèn)值為yes。
③许帐、repl-diskless-sync:主從數(shù)據(jù)復(fù)制是否使用無硬盤復(fù)制功能。默認(rèn)值為no成畦。
④距芬、repl-diskless-sync-delay:當(dāng)啟用無硬盤備份循帐,服務(wù)器等待一段時間后才會通過套接字向從站傳送RDB文件,這個等待時間是可配置的拄养。 這一點很重要,因為一旦傳送開始瘪匿,就不可能再為一個新到達(dá)的從站服務(wù)。從站則要排隊等待下一次RDB傳送柿顶。因此服務(wù)器等待一段 時間以期更多的從站到達(dá)茄袖。延遲時間以秒為單位嘁锯,默認(rèn)為5秒。要關(guān)掉這一功能家乘,只需將它設(shè)置為0秒,傳送會立即啟動仁锯。默認(rèn)值為5耀找。
⑤、repl-disable-tcp-nodelay:同步之后是否禁用從站上的TCP_NODELAY 如果你選擇yes野芒,redis會使用較少量的TCP包和帶寬向從站發(fā)送數(shù)據(jù)。但這會導(dǎo)致在從站增加一點數(shù)據(jù)的延時狞悲。 Linux內(nèi)核默認(rèn)配置情況下最多40毫秒的延時。如果選擇no摇锋,從站的數(shù)據(jù)延時不會那么多,但備份需要的帶寬相對較多荸恕。默認(rèn)情況下我們將潛在因素優(yōu)化,但在高負(fù)載情況下或者在主從站都跳的情況下融求,把它切換為yes是個好主意。默認(rèn)值為no双肤。
MEMORY MANAGEMENT
############################## MEMORY MANAGEMENT ################################
# Set a memory usage limit to the specified amount of bytes.
# When the memory limit is reached Redis will try to remove keys
# according to the eviction policy selected (see maxmemory-policy).
#
# If Redis can't remove keys according to the policy, or if the policy is
# set to 'noeviction', Redis will start to reply with errors to commands
# that would use more memory, like SET, LPUSH, and so on, and will continue
# to reply to read-only commands like GET.
#
# This option is usually useful when using Redis as an LRU or LFU cache, or to
# set a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have replicas attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the replicas are subtracted
# from the used memory count, so that network problems / resyncs will
# not trigger a loop where keys are evicted, and in turn the output
# buffer of replicas is full with DELs of keys evicted triggering the deletion
# of more keys, and so forth until the database is completely emptied.
#
# In short... if you have replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is 'noeviction').
1钮惠、maxmemory:設(shè)置Redis的最大內(nèi)存,如果設(shè)置為0 素挽。表示不作限制。通常是配合下面介紹的maxmemory-policy參數(shù)一起使用预明。
2、maxmemory-policy :當(dāng)內(nèi)存使用達(dá)到maxmemory設(shè)置的最大值時撰糠,redis使用的內(nèi)存清除策略酥馍。有以下幾種可以選擇:
1)volatile-lru 利用LRU算法移除設(shè)置過過期時間的key (LRU:最近使用 Least Recently Used )
2)allkeys-lru 利用LRU算法移除任何key
3)volatile-random 移除設(shè)置過過期時間的隨機(jī)key
4)allkeys-random 移除隨機(jī)key
5)volatile-ttl 移除即將過期的key(minor TTL)
6)noeviction noeviction 不移除任何key阅酪,只是返回一個寫錯誤 ,默認(rèn)選項
3术辐、maxmemory-samples :LRU 和 minimal TTL 算法都不是精準(zhǔn)的算法,但是相對精確的算法(為了節(jié)省內(nèi)存)辉词。隨意你可以選擇樣本大小進(jìn)行檢,redis默認(rèn)選擇3個樣本進(jìn)行檢測瑞躺,你可以通過maxmemory-samples進(jìn)行設(shè)置樣本數(shù)兴想。
APPEND ONLY MODE
############################## APPEND ONLY MODE ###############################
# By default Redis asynchronously dumps the dataset on disk. This mode is
# good enough in many applications, but an issue with the Redis process or
# a power outage may result into a few minutes of writes lost (depending on
# the configured save points).
#
# The Append Only File is an alternative persistence mode that provides
# much better durability. For instance using the default data fsync policy
# (see later in the config file) Redis can lose just one second of writes in a
# dramatic event like a server power outage, or a single write if something
# wrong with the Redis process itself happens, but the operating system is
# still running correctly.
#
# AOF and RDB persistence can be enabled at the same time without problems.
# If the AOF is enabled on startup Redis will load the AOF, that is the file
# with the better durability guarantees.
#
# Please check http://redis.io/topics/persistence for more information.
appendonly no
1、appendonly:默認(rèn)redis使用的是rdb方式持久化襟企,這種方式在許多應(yīng)用中已經(jīng)足夠用了。但是redis如果中途宕機(jī)顽悼,會導(dǎo)致可能有幾分鐘的數(shù)據(jù)丟失,根據(jù)save來策略進(jìn)行持久化蔚龙,Append Only File是另一種持久化方式, 可以提供更好的持久化特性木羹。Redis會把每次寫入的數(shù)據(jù)在接收后都寫入appendonly.aof文件,每次啟動時Redis都會先把這個文件的數(shù)據(jù)讀入內(nèi)存里解孙,先忽略RDB文件。默認(rèn)值為no弛姜。
2、appendfilename :aof文件名廷臼,默認(rèn)是"appendonly.aof"
3、appendfsync:aof持久化策略的配置荠商;no表示不執(zhí)行fsync,由操作系統(tǒng)保證數(shù)據(jù)同步到磁盤莱没,速度最快初肉;always表示每次寫入都執(zhí)行fsync饰躲,以保證數(shù)據(jù)同步到磁盤;everysec表示每秒執(zhí)行一次fsync属铁,可能會導(dǎo)致丟失這1s數(shù)據(jù)
4、no-appendfsync-on-rewrite:在aof重寫或者寫入rdb文件的時候焦蘑,會執(zhí)行大量IO盯拱,此時對于everysec和always的aof模式來說,執(zhí)行fsync會造成阻塞過長時間狡逢,no-appendfsync-on-rewrite字段設(shè)置為默認(rèn)設(shè)置為no。如果對延遲要求很高的應(yīng)用奢浑,這個字段可以設(shè)置為yes,否則還是設(shè)置為no雀彼,這樣對持久化特性來說這是更安全的選擇。 設(shè)置為yes表示rewrite期間對新寫操作不fsync,暫時存在內(nèi)存中,等rewrite完成后再寫入徊哑,默認(rèn)為no,建議yes莺丑。Linux的默認(rèn)fsync策略是30秒∩颐В可能丟失30秒數(shù)據(jù)。默認(rèn)值為no昏名。
5涮雷、auto-aof-rewrite-percentage:默認(rèn)值為100葡粒。aof自動重寫配置膜钓,當(dāng)目前aof文件大小超過上一次重寫的aof文件大小的百分之多少進(jìn)行重寫嗽交,即當(dāng)aof文件增長到一定大小的時候颂斜,Redis能夠調(diào)用bgrewriteaof對日志文件進(jìn)行重寫。當(dāng)前AOF文件大小是上次日志重寫得到AOF文件大小的二倍(設(shè)置為100)時沃疮,自動啟動新的日志重寫過程。
6司蔬、auto-aof-rewrite-min-size:64mb邑茄。設(shè)置允許重寫的最小aof文件大小俊啼,避免了達(dá)到約定百分比但尺寸仍然很小的情況還要重寫。
7、aof-load-truncated:aof文件可能在尾部是不完整的同木,當(dāng)redis啟動的時候,aof文件的數(shù)據(jù)被載入內(nèi)存彤路。重啟可能發(fā)生在redis所在的主機(jī)操作系統(tǒng)宕機(jī)后,尤其在ext4文件系統(tǒng)沒有加上data=ordered選項洲尊,出現(xiàn)這種現(xiàn)象 redis宕機(jī)或者異常終止不會造成尾部不完整現(xiàn)象,可以選擇讓redis退出颊郎,或者導(dǎo)入盡可能多的數(shù)據(jù)。如果選擇的是yes姆吭,當(dāng)截斷的aof文件被導(dǎo)入的時候,會自動發(fā)布一個log給客戶端然后load内狸。如果是no,用戶必須手動redis-check-aof修復(fù)AOF文件才可以昆淡。默認(rèn)值為 yes锰瘸。
REDIS CLUSTER
################################ REDIS CLUSTER ###############################
# Normal Redis instances can't be part of a Redis Cluster; only nodes that are
# started as cluster nodes can. In order to start a Redis instance as a
# cluster node enable the cluster support uncommenting the following:
#
# cluster-enabled yes
# Every cluster node has a cluster configuration file. This file is not
# intended to be edited by hand. It is created and updated by Redis nodes.
# Every Redis Cluster node requires a different cluster configuration file.
# Make sure that instances running in the same system do not have
# overlapping cluster configuration file names.
#
# cluster-config-file nodes-6379.conf
1昂灵、cluster-enabled:集群開關(guān),默認(rèn)是不開啟集群模式眨补。
2、cluster-config-file:集群配置文件的名稱撑螺,每個節(jié)點都有一個集群相關(guān)的配置文件,持久化保存集群的信息甘晤。 這個文件并不需要手動配置,這個配置文件有Redis生成并更新线婚,每個Redis集群節(jié)點需要一個單獨的配置文件遏弱。請確保與實例運行的系統(tǒng)中配置文件名稱不沖突塞弊。默認(rèn)配置為nodes-6379.conf缀踪。
3、cluster-node-timeout :可以配置值為15000驴娃。節(jié)點互連超時的閥值,集群節(jié)點超時毫秒數(shù)
4唇敞、cluster-slave-validity-factor :可以配置值為10。在進(jìn)行故障轉(zhuǎn)移的時候咒彤,全部slave都會請求申請為master,但是有些slave可能與master斷開連接一段時間了镶柱, 導(dǎo)致數(shù)據(jù)過于陳舊,這樣的slave不應(yīng)該被提升為master歇拆。該參數(shù)就是用來判斷slave節(jié)點與master斷線的時間是否過長。判斷方法是:比較slave斷開連接的時間和(node-timeout * slave-validity-factor) + repl-ping-slave-period 如果節(jié)點超時時間為三十秒, 并且slave-validity-factor為10,假設(shè)默認(rèn)的repl-ping-slave-period是10秒故觅,即如果超過310秒slave將不會嘗試進(jìn)行故障轉(zhuǎn)移
5、cluster-migration-barrier :可以配置值為1输吏。master的slave數(shù)量大于該值,slave才能遷移到其他孤立master上贯溅,如這個參數(shù)若被設(shè)為2,那么只有當(dāng)一個主節(jié)點擁有2 個可工作的從節(jié)點時它浅,它的一個從節(jié)點會嘗試遷移译柏。
6罚缕、cluster-require-full-coverage:默認(rèn)情況下怎静,集群全部的slot有節(jié)點負(fù)責(zé)邮弹,集群狀態(tài)才為ok蚓聘,才能提供服務(wù)腌乡。 設(shè)置為no夜牡,可以在slot沒有全部分配的時候提供服務(wù)侣签。不建議打開該配置,這樣會造成分區(qū)的時候影所,小分區(qū)的master一直在接受寫請求,而造成很長時間數(shù)據(jù)不一致猴娩。