# Redis 配置文件
# 當(dāng)配置中需要配置內(nèi)存大小時收奔,可以使用 1k, 5GB, 4M 等類似的格式输钩,其轉(zhuǎn)換方式如下(不區(qū)分大小寫)
#
# 1k =>
1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb =>
1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024
bytes
#
# 內(nèi)存配置大小寫是一樣的.比如 1gb 1Gb 1GB 1gB
# daemonize no 默認(rèn)情況下篮幢,redis不是在后臺運(yùn)行的,如果需要在后臺運(yùn)行,把該項(xiàng)的值更改為yes
daemonize yes
# 當(dāng)redis在后臺運(yùn)行的時候瑞筐,Redis默認(rèn)會把pid文件放在/var/run/redis.pid胰丁,你可以配置到其他地址随橘。
#
當(dāng)運(yùn)行多個redis服務(wù)時,需要指定不同的pid文件和端口
pidfile /var/run/redis.pid
# 指定redis運(yùn)行的端口锦庸,默認(rèn)是6379
port 6379
# 指定redis只接收來自于該IP地址的請求机蔗,如果不進(jìn)行設(shè)置,那么將處理所有請求甘萧,
# 在生產(chǎn)環(huán)境中最好設(shè)置該項(xiàng)
# bind
127.0.0.1
# Specify the path for the unix socket that will be used to listen for
#
incoming connections. There is no default, so Redis will not listen
# on a
unix socket when not specified.
#
# unixsocket /tmp/redis.sock
#
unixsocketperm 755
# 設(shè)置客戶端連接時的超時時間萝嘁,單位為秒。當(dāng)客戶端在這段時間內(nèi)沒有發(fā)出任何指令扬卷,那么關(guān)閉該連接
# 0是關(guān)閉此設(shè)置
timeout
0
# 指定日志記錄級別
# Redis總共支持四個級別:debug牙言、verbose、notice怪得、warning咱枉,默認(rèn)為verbose
#
debug? 記錄很多信息卑硫,用于開發(fā)和測試
# varbose 有用的信息,不像debug會記錄那么多
#
notice 普通的verbose庞钢,常用于生產(chǎn)環(huán)境
# warning 只有非常重要或者嚴(yán)重的信息會記錄到日志
loglevel
debug
# 配置log文件地址
# 默認(rèn)值為stdout拔恰,標(biāo)準(zhǔn)輸出,若后臺模式會輸出到/dev/null
#logfile
stdout
logfile /var/log/redis/redis.log
# To enable logging to the system logger, just set 'syslog-enabled' to
yes,
# and optionally update the other syslog parameters to suit your
needs.
# syslog-enabled no
# Specify the syslog identity.
# syslog-ident redis
# Specify the syslog facility.? Must be USER or between LOCAL0-LOCAL7.
#
syslog-facility local0
# 可用數(shù)據(jù)庫數(shù)
# 默認(rèn)值為16基括,默認(rèn)數(shù)據(jù)庫為0颜懊,數(shù)據(jù)庫范圍在0-(database-1)之間
databases 16
################################ 快照?
#################################
#
# 保存數(shù)據(jù)到磁盤,格式如下:
#
#? save
#
#?
指出在多長時間內(nèi)风皿,有多少次更新操作河爹,就將數(shù)據(jù)同步到數(shù)據(jù)文件rdb。
#? 相當(dāng)于條件觸發(fā)抓取快照桐款,這個可以多個條件配合
#?
#?
比如默認(rèn)配置文件中的設(shè)置咸这,就設(shè)置了三個條件
#
#? save 900 1? 900秒內(nèi)至少有1個key被改變
#? save 300
10? 300秒內(nèi)至少有300個key被改變
#? save 60 10000? 60秒內(nèi)至少有10000個key被改變
save 900 1
save 300 10
save 60 10000
# 存儲至本地?cái)?shù)據(jù)庫時(持久化到rdb文件)是否壓縮數(shù)據(jù),默認(rèn)為yes
rdbcompression yes
# 本地持久化數(shù)據(jù)庫文件名魔眨,默認(rèn)值為dump.rdb
dbfilename dump.rdb
# 工作目錄
#
# 數(shù)據(jù)庫鏡像備份的文件放置的路徑媳维。
#
這里的路徑跟文件名要分開配置是因?yàn)閞edis在進(jìn)行備份時,先會將當(dāng)前數(shù)據(jù)庫的狀態(tài)寫入到一個臨時文件中遏暴,等備份完成時侄刽,
#
再把該該臨時文件替換為上面所指定的文件,而這里的臨時文件和上面所配置的備份文件都會放在這個指定的路徑當(dāng)中朋凉。
#
#
AOF文件也會存放在這個目錄下面
#
# 注意這里必須制定一個目錄而不是文件
dir ./
################################# 復(fù)制
#################################
# 主從復(fù)制. 設(shè)置該數(shù)據(jù)庫為其他數(shù)據(jù)庫的從數(shù)據(jù)庫.
#
設(shè)置當(dāng)本機(jī)為slav服務(wù)時州丹,設(shè)置master服務(wù)的IP地址及端口,在Redis啟動時杂彭,它會自動從master進(jìn)行數(shù)據(jù)同步
#
# slaveof
# 當(dāng)master服務(wù)設(shè)置了密碼保護(hù)時(用requirepass制定的密碼)
# slav服務(wù)連接master的密碼
#
#
masterauth
# 當(dāng)從庫同主機(jī)失去連接或者復(fù)制正在進(jìn)行墓毒,從機(jī)庫有兩種運(yùn)行方式:
#
# 1)
如果slave-serve-stale-data設(shè)置為yes(默認(rèn)設(shè)置),從庫會繼續(xù)相應(yīng)客戶端的請求
#
# 2)
如果slave-serve-stale-data是指為no亲怠,出去INFO和SLAVOF命令之外的任何請求都會返回一個
#? ? 錯誤"SYNC with
master in progress"
#
slave-serve-stale-data yes
# 從庫會按照一個時間間隔向主庫發(fā)送PINGs.可以通過repl-ping-slave-period設(shè)置這個時間間隔所计,默認(rèn)是10秒
#
#
repl-ping-slave-period 10
# repl-timeout 設(shè)置主庫批量數(shù)據(jù)傳輸時間或者ping回復(fù)時間間隔,默認(rèn)值是60秒
#
一定要確保repl-timeout大于repl-ping-slave-period
# repl-timeout 60
################################## 安全
###################################
# 設(shè)置客戶端連接后進(jìn)行任何其他指定前需要使用的密碼团秽。
#
警告:因?yàn)閞edis速度相當(dāng)快醉箕,所以在一臺比較好的服務(wù)器下,一個外部的用戶可以在一秒鐘進(jìn)行150K次的密碼嘗試徙垫,這意味著你需要指定非常非常強(qiáng)大的密碼來防止暴力破解
#
#
requirepass foobared
# 命令重命名.
#
# 在一個共享環(huán)境下可以重命名相對危險的命令。比如把CONFIG重名為一個不容易猜測的字符放棒。
#
#
舉例:
#
# rename-command CONFIG
b840fc02d524045429941cc15f59e41cb7be6c52
#
#
如果想刪除一個命令姻报,直接把它重命名為一個空字符""即可,如下:
#
# rename-command CONFIG ""
################################### 約束
####################################
# 設(shè)置同一時間最大客戶端連接數(shù)间螟,默認(rèn)無限制吴旋,Redis可以同時打開的客戶端連接數(shù)為Redis進(jìn)程可以打開的最大文件描述符數(shù)损肛,
# 如果設(shè)置
maxclients 0,表示不作限制荣瑟。
# 當(dāng)客戶端連接數(shù)到達(dá)限制時治拿,Redis會關(guān)閉新的連接并向客戶端返回max number of clients
reached錯誤信息
#
# maxclients 128
# 指定Redis最大內(nèi)存限制,Redis在啟動時會把數(shù)據(jù)加載到內(nèi)存中笆焰,達(dá)到最大內(nèi)存后劫谅,Redis會先嘗試清除已到期或即將到期的Key
#
Redis同時也會移除空的list對象
#
#
當(dāng)此方法處理后,仍然到達(dá)最大內(nèi)存設(shè)置嚷掠,將無法再進(jìn)行寫入操作捏检,但仍然可以進(jìn)行讀取操作
#
#
注意:Redis新的vm機(jī)制,會把Key存放內(nèi)存不皆,Value會存放在swap區(qū)
#
#
maxmemory的設(shè)置比較適合于把redis當(dāng)作于類似memcached的緩存來使用贯城,而不適合當(dāng)做一個真實(shí)的DB。
#
當(dāng)把Redis當(dāng)做一個真實(shí)的數(shù)據(jù)庫使用的時候霹娄,內(nèi)存使用將是一個很大的開銷
# maxmemory
# 當(dāng)內(nèi)存達(dá)到最大值的時候Redis會選擇刪除哪些數(shù)據(jù)能犯?有五種方式可供選擇
#
# volatile-lru ->
利用LRU算法移除設(shè)置過過期時間的key (LRU:最近使用 Least Recently Used )
# allkeys-lru ->
利用LRU算法移除任何key
# volatile-random -> 移除設(shè)置過過期時間的隨機(jī)key
#
allkeys->random -> remove a random key, any key
# volatile-ttl ->
移除即將過期的key(minor TTL)
# noeviction -> 不移除任何可以,只是返回一個寫錯誤
#
#
犬耻、
#
#? ? ? 寫命令包括: set setnx
setex append
#? ? ? incr decr rpush lpush rpushx lpushx linsert lset
rpoplpush sadd
#? ? ? sinter sinterstore sunion sunionstore sdiff sdiffstore
zadd zincrby
#? ? ? zunionstore zinterstore hset hsetnx hmset hincrby incrby
decrby
#? ? ? getset mset msetnx exec sort
#
# 默認(rèn)是:
#
#
maxmemory-policy volatile-lru
# LRU 和 minimal TTL 算法都不是精準(zhǔn)的算法踩晶,但是相對精確的算法(為了節(jié)省內(nèi)存),隨意你可以選擇樣本大小進(jìn)行檢測。
#
Redis默認(rèn)的灰選擇3個樣本進(jìn)行檢測相速,你可以通過maxmemory-samples進(jìn)行設(shè)置
#
# maxmemory-samples
3
############################## AOF ###############################
#
默認(rèn)情況下铣鹏,redis會在后臺異步的把數(shù)據(jù)庫鏡像備份到磁盤,但是該備份是非常耗時的晴楔,而且備份也不能很頻繁,如果發(fā)生諸如拉閘限電峭咒、拔插頭等狀況税弃,那么將造成比較大范圍的數(shù)據(jù)丟失。
#
所以redis提供了另外一種更加高效的數(shù)據(jù)庫備份及災(zāi)難恢復(fù)方式凑队。
# 開啟append
only模式之后则果,redis會把所接收到的每一次寫操作請求都追加到appendonly.aof文件中,當(dāng)redis重新啟動時漩氨,會從該文件恢復(fù)出之前的狀態(tài)西壮。
#
但是這樣會造成appendonly.aof文件過大,所以redis還支持了BGREWRITEAOF指令叫惊,對appendonly.aof 進(jìn)行重新整理款青。
#
你可以同時開啟asynchronous dumps 和 AOF
appendonly no
# AOF文件名稱 (默認(rèn): "appendonly.aof")
# appendfilename appendonly.aof
# Redis支持三種同步AOF文件的策略:
#
# no: 不進(jìn)行同步,系統(tǒng)去操作 . 非郴粽快.
# always:
always表示每次有寫操作都進(jìn)行同步. Slow, Safest.
# everysec: 表示對寫操作進(jìn)行累積抡草,每秒同步一次.
Compromise.
#
# 默認(rèn)是"everysec"饰及,按照速度和安全折中這是最好的。
#
如果想讓Redis能更高效的運(yùn)行康震,你也可以設(shè)置為"no"燎含,讓操作系統(tǒng)決定什么時候去執(zhí)行
#
或者相反想讓數(shù)據(jù)更安全你也可以設(shè)置為"always"
#
# 如果不確定就用 "everysec".
# appendfsync always
appendfsync everysec
# appendfsync no
# AOF策略設(shè)置為always或者everysec時,后臺處理進(jìn)程(后臺保存或者AOF日志重寫)會執(zhí)行大量的I/O操作
#
在某些Linux配置中會阻止過長的fsync()請求腿短。注意現(xiàn)在沒有任何修復(fù)屏箍,即使fsync在另外一個線程進(jìn)行處理
#
#
為了減緩這個問題,可以設(shè)置下面這個參數(shù)no-appendfsync-on-rewrite
#
# This means that while
another child is saving the durability of Redis is
# the same as "appendfsync
none", that in pratical terms means that it is
# possible to lost up to 30
seconds of log in the worst scenario (with the
# default Linux
settings).
#
# If you have latency problems turn this to "yes". Otherwise
leave it as
# "no" that is the safest pick from the point of view of
durability.
no-appendfsync-on-rewrite no
# Automatic rewrite of the append only file.
# AOF 自動重寫
#
當(dāng)AOF文件增長到一定大小的時候Redis能夠調(diào)用 BGREWRITEAOF 對日志文件進(jìn)行重寫
#
#
它是這樣工作的:Redis會記住上次進(jìn)行些日志后文件的大小(如果從開機(jī)以來還沒進(jìn)行過重寫答姥,那日子大小在開機(jī)的時候確定)
#
#
基礎(chǔ)大小會同現(xiàn)在的大小進(jìn)行比較铣除。如果現(xiàn)在的大小比基礎(chǔ)大小大制定的百分比,重寫功能將啟動
#
同時需要指定一個最小大小用于AOF重寫鹦付,這個用于阻止即使文件很小但是增長幅度很大也去重寫AOF文件的情況
# 設(shè)置 percentage
為0就關(guān)閉這個特性
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
################################## SLOW LOG
###################################
# Redis Slow Log 記錄超過特定執(zhí)行時間的命令尚粘。執(zhí)行時間不包括I/O計(jì)算比如連接客戶端,返回結(jié)果等敲长,只是命令執(zhí)行時間
#
#
可以通過兩個參數(shù)設(shè)置slow log:一個是告訴Redis執(zhí)行超過多少時間被記錄的參數(shù)slowlog-log-slower-than(微妙)郎嫁,
#
另一個是slow log 的長度。當(dāng)一個新命令被記錄的時候最早的命令將被從隊(duì)列中移除
# 下面的時間以微妙微單位祈噪,因此1000000代表一分鐘泽铛。
#
注意制定一個負(fù)數(shù)將關(guān)閉慢日志,而設(shè)置為0將強(qiáng)制每個命令都會記錄
slowlog-log-slower-than 10000
# 對日志長度沒有限制辑鲤,只是要注意它會消耗內(nèi)存
# 可以通過 SLOWLOG RESET
回收被慢日志消耗的內(nèi)存
slowlog-max-len 1024
################################ VM ###############################
### WARNING! Virtual Memory is deprecated in Redis 2.4
### The use of
Virtual Memory is strongly discouraged.
# Virtual Memory allows Redis to work with datasets bigger than the
actual
# amount of RAM needed to hold the whole dataset in memory.
# In
order to do so very used keys are taken in memory while the other keys
# are
swapped into a swap file, similarly to what operating systems do
# with
memory pages.
#
# To enable VM just set 'vm-enabled' to yes, and set the
following three
# VM parameters accordingly to your needs.
vm-enabled no
# vm-enabled yes
# This is the path of the Redis swap file. As you can guess, swap
files
# can't be shared by different Redis instances, so make sure to use a
swap
# file for every redis process you are running. Redis will complain if
the
# swap file is already in use.
#
# The best kind of storage for the
Redis swap file (that's accessed at random)
# is a Solid State Disk
(SSD).
#
# *** WARNING *** if you are using a shared hosting the default
of putting
# the swap file under /tmp is not secure. Create a dir with access
granted
# only to Redis user and configure Redis to create the swap file
there.
vm-swap-file /tmp/redis.swap
# vm-max-memory configures the VM to use at max the specified amount
of
# RAM. Everything that deos not fit will be swapped on disk *if* possible,
that
# is, if there is still enough contiguous space in the swap
file.
#
# With vm-max-memory 0 the system will swap everything it can. Not
a good
# default, just specify the max amount of RAM you can in bytes, but
it's
# better to leave some margin. For instance specify an amount of
RAM
# that's more or less between 60 and 80% of your free
RAM.
vm-max-memory 0
# Redis swap files is split into pages. An object can be saved using
multiple
# contiguous pages, but pages can't be shared between different
objects.
# So if your page is too big, small objects swapped out on disk will
waste
# a lot of space. If you page is too small, there is less space in the
swap
# file (assuming you configured the same number of total swap file
pages).
#
# If you use a lot of small objects, use a page size of 64 or 32
bytes.
# If you use a lot of big objects, use a bigger page size.
# If
unsure, use the default :)
vm-page-size 32
# Number of total memory pages in the swap file.
# Given that the page
table (a bitmap of free/used pages) is taken in memory,
# every 8 pages on
disk will consume 1 byte of RAM.
#
# The total swap size is vm-page-size *
vm-pages
#
# With the default of 32-bytes memory pages and 134217728 pages
Redis will
# use a 4 GB swap file, that will use 16 MB of RAM for the page
table.
#
# It's better to use the smallest acceptable value for your
application,
# but the default is large in order to work in most
conditions.
vm-pages 134217728
# Max number of VM I/O threads running at the same time.
# This threads
are used to read/write data from/to swap file, since they
# also encode and
decode objects from disk to memory or the reverse, a bigger
# number of
threads can help with big objects even if they can't help with
# I/O itself
as the physical device may not be able to couple with many
# reads/writes
operations at the same time.
#
# The special value of 0 turn off threaded
I/O and enables the blocking
# Virtual Memory
implementation.
vm-max-threads 4
############################### ADVANCED CONFIG
###############################
# 當(dāng)hash中包含超過指定元素個數(shù)并且最大的元素沒有超過臨界時盔腔,
#
hash將以一種特殊的編碼方式(大大減少內(nèi)存使用)來存儲,這里可以設(shè)置這兩個臨界值
# Redis
Hash對應(yīng)Value內(nèi)部實(shí)際就是一個HashMap月褥,實(shí)際這里會有2種不同實(shí)現(xiàn)弛随,
#
這個Hash的成員比較少時Redis為了節(jié)省內(nèi)存會采用類似一維數(shù)組的方式來緊湊存儲,而不會采用真正的HashMap結(jié)構(gòu)宁赤,對應(yīng)的value
redisObject的encoding為zipmap,
#
當(dāng)成員數(shù)量增大時會自動轉(zhuǎn)成真正的HashMap,此時encoding為ht舀透。
hash-max-zipmap-entries
512
hash-max-zipmap-value 64
# list數(shù)據(jù)類型多少節(jié)點(diǎn)以下會采用去指針的緊湊存儲格式。
#
list數(shù)據(jù)類型節(jié)點(diǎn)值大小小于多少字節(jié)會采用緊湊存儲格式决左。
list-max-ziplist-entries
512
list-max-ziplist-value 64
# set數(shù)據(jù)類型內(nèi)部數(shù)據(jù)如果全部是數(shù)值型愕够,且包含多少節(jié)點(diǎn)以下會采用緊湊格式存儲。
set-max-intset-entries
512
# zsort數(shù)據(jù)類型多少節(jié)點(diǎn)以下會采用去指針的緊湊存儲格式佛猛。
#
zsort數(shù)據(jù)類型節(jié)點(diǎn)值大小小于多少字節(jié)會采用緊湊存儲格式惑芭。
zset-max-ziplist-entries
128
zset-max-ziplist-value 64
# Redis將在每100毫秒時使用1毫秒的CPU時間來對redis的hash表進(jìn)行重新hash,可以降低內(nèi)存的使用
#
#
當(dāng)你的使用場景中继找,有非常嚴(yán)格的實(shí)時性需要强衡,不能夠接受Redis時不時的對請求有2毫秒的延遲的話,把這項(xiàng)配置為no。
#
#
如果沒有這么嚴(yán)格的實(shí)時性要求漩勤,可以設(shè)置為yes,以便能夠盡可能快的釋放內(nèi)存
activerehashing yes
################################## INCLUDES
###################################
# 指定包含其它的配置文件缩搅,可以在同一主機(jī)上多個Redis實(shí)例之間使用同一份配置文件越败,而同時各個實(shí)例又擁有自己的特定配置文件
#
include /path/to/local.conf
# include /path/to/other.conf