1. 程式人生 > >Redis系列(一)--安裝、helloworld以及讀懂配置檔案

Redis系列(一)--安裝、helloworld以及讀懂配置檔案

再開個redis系列,本系列打算不詳細講一系列的命名的了(會推薦別人寫的,人家寫的夠詳細了),我直接就是做redis方案提供,當然一開始還是講下helloworld和配置檔案好了。會逐步更新,歡迎關注。

文章結構:(1)安裝;(2)helloworld;(3)解析配置檔案(一一羅列方便複習);(4)重點配置

一、安裝:把到官網下載好的redis壓縮包放置到你想要的位置。並解壓。

這裡寫圖片描述

然後進入redis-3.0.4目錄,執行make命令

jackfrost@jackfrost-pc:~/MyResource/Redis/redis-3.0.4$ make

問題:如果遇到gcc沒找到
,那麼請看以下說明:

原因:redis是c語音編寫的,需要gcc環境支援。

gcc是linux下的一個編譯程式,是c程式的編譯工具。GCC是GNU計劃提供的編譯器家族,支援C,C++,Objective-C,Java等多語音,同時能執行在x86,x86-64,IA-64,PowerPC等幾乎所有硬體平臺。

解決:一個安裝命令(centos下的命令)

yum install gcc-c++

二、helloworld:

(1)關注第一個目錄!/usr

這是一個重要目錄,使用者的很多應用程式和檔案都放在這個目錄下面,類似windows的program file目錄

而我們的redis安裝好後,一般就是在/usr/local/bin下面啦

這裡寫圖片描述

(2)關注的第二個目錄:

我們在玩redis,當然關注自己的redis目錄啦!!,同時重點關注reids.conf

注意,一般我們不直接操作我們安裝好的reids配置檔案,因為他很重要,關乎生死,redis架構好壞。

這裡寫圖片描述

(3)於是為了安全,我們要另建一個目錄去操作redis.conf

這裡寫圖片描述

然後就把原來的那個redis.conf複製到我們新建的目錄嘛

這裡寫圖片描述

想要多少份就多少份,反正操作對應好的配置檔案就ok啦。

redis關鍵:能備份的先備份

(4)修改配置檔案

把此處的no改成yes

意思是:預設情況下 redis 不是作為守護程序執行的,如果你想讓它在後臺執行,你就把它改成 yes。我們必須保證它不被殺死!!!

這裡寫圖片描述

(5)啟動並觀察redis程序

使用下面命令可以看到,現在並沒有開啟redis嘛。

這裡寫圖片描述

那麼我們就啟動他嘛,進入/usr/local/bin目錄

要求:1.找到我們剛剛複製並修改過來的redis.conf全路徑,使用redis-server命令;2. 1的命令啟動後沒有報錯,繼續使用redis-cli -p 6379 命令,ping連線一下。返回pong即成功啟動!

這裡寫圖片描述

這個時候我們再來觀察redis的程序:就可以看到啦

這裡寫圖片描述

於是我們就可以開始寫我們熟悉的helloworld啦!

這裡寫圖片描述

先暫時給個常見錯誤解決:

這裡寫圖片描述

錯誤)misconf redis被配置以儲存資料庫快照,但misconf redis目前不能在硬碟上持久化。用來修改資料集合的命令不能用,請使用日誌的錯誤詳細資訊。

原因:這是由於強制停止redis快照,不能持久化引起的。edis在儲存資料到硬碟時為了避免主程序假死,需要Fork一份主程序,然後在Fork程序內完成資料儲存到硬碟的操作,如果主程序使用了4GB的記憶體,Fork子程序的時候需要額外的4GB,此時記憶體就不夠了,Fork失敗,進而資料儲存硬碟也失敗了。

解決:config set stop-writes-on-bgsave-error no 命令來關閉配置項stop-writes-on-bgsave-error。詳細解析

三、解析配置檔案:

一開始就先全部羅列解析一次吧(其實是基本的翻譯啦),,不過下面會有重點關注提醒!!

# Redis configuration file example
//redis 配置檔案示例
# Note on units: when memory size is needed, it is possible to specify
//當你需要為某個配置項指定記憶體大小的時候,必須要帶上單位
# it in the usual form of 1k 5GB 4M and so forth:
//通常的格式就是 1k 5gb 4m 
# 1k => 1000 bytes
# 1kb => 1024 bytes
# 1m => 1000000 bytes
# 1mb => 1024*1024 bytes
# 1g => 1000000000 bytes
# 1gb => 1024*1024*1024 bytes
#
# units are case insensitive so 1GB 1Gb 1gB are all the same.
//單位是不區分大小寫的,你寫 1K 5GB 4M 也行
################################## 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
//假如說你有一個可用於所有的 redis server 的標準配置模板,
# to customize a few per-server settings.  Include files can include
# other files, so use this wisely.
//但針對某些 server 又需要一些個性化的設定,可以使用 include 來包含一些其他的配置檔案,這對你來說是非常有用的。
# Notice option "include" won't be rewritten by command "CONFIG REWRITE"
//但是要注意,include 是不能被 config rewrite 命令改寫的
# from admin or Redis Sentinel. Since Redis always uses the last processed
//由於 redis 總是以最後的加工前作為一個配置指令值,所以你最好是把 include 放在這個檔案的最前面,以避免在執行時覆蓋配置的改變,相反,你就把它放在後面
# 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.
//如果您感興趣的是使用包括重寫配置選項,最好使用include作為最後一行。
# 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

################################ GENERAL常用  #####################################
//預設情況下 redis 不是作為守護程序執行的,如果你想讓它在後臺執行,你就把它改成 yes。
# By default Redis does not run as a daemon. Use 'yes' if you need it.
//當redis作為守護程序執行的時候,它會寫一個 pid 到 /var/run/redis.pid 檔案裡面。
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
daemonize no
//當redis作為守護程序執行的時候,它會把 pid 預設寫到 /var/run/redis.pid 檔案裡面,但是你可以在這裡自己制定它的檔案位置。
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
# default. You can specify a custom pid file location here.
pidfile /var/run/redis.pid
//監聽埠號,預設為 6379,如果你設為 0 ,redis 將不在 socket 上監聽任何客戶端連線。
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 6379
//TCP 監聽的最大容納數量
# TCP listen() backlog.
//在高併發的環境下,你需要把這個值調高以避免客戶端連線緩慢的問題。
# In high requests-per-second environments you need an high backlog in order
# to avoid slow clients connections issues. Note that the Linux kernel
# will silently truncate it to the value of /proc/sys/net/core/somaxconn so
//Linux 核心會一聲不響的把這個值縮小成 /proc/sys/net/core/somaxconn 對應的值,所以你要修改這兩個值才能達到你的預期。
# make sure to raise both the value of somaxconn and tcp_max_syn_backlog
# in order to get the desired effect.
tcp-backlog 511
//預設情況下,redis 在 server 上所有有效的網路介面上監聽客戶端連線。
//你如果只想讓它在一個網路介面上監聽,那你就繫結一個IP或者多個IP。
# By default Redis listens for connections from all the network interfaces
# available on the server. It is possible to listen to just one or multiple
//示例,多個IP用空格隔開
# interfaces using the "bind" configuration directive, followed by one or
# more IP addresses.
#
# Examples:
#
# bind 192.168.1.100 10.0.0.1
# bind 127.0.0.1
//指定 unix socket 的路徑。
# 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 700
//指定在一個 client 空閒多少秒之後關閉連線(0 就是不管它)
# Close the connection after a client is idle for N seconds (0 to disable)
timeout 0
//tcp 心跳包。
# TCP keepalive.
//如果設定為非零,則在與客戶端缺乏通訊的時候使用 SO_KEEPALIVE 傳送 tcp acks 給客戶端。
# If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence
//這個之所有有用,主要由兩個原因:
//1.防止死的 peers
//2.從網路的角度看連線的活躍性中間裝置。
# 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.
//推薦一個合理的值就是60秒
# A reasonable value for this option is 60 seconds.
tcp-keepalive 0
//定義日誌級別。
# Specify the server verbosity level.
# This can be one of:
//可以是下面的這些值:
//debug (適用於開發或測試階段)
# debug (a lot of information, useful for development/testing)
//基本不用的級別.許多很少有用的資訊,但不是像除錯級別混亂
# verbose (many rarely useful info, but not a mess like the debug level)
// (適用於生產環境)
# notice (moderately verbose, what you want in production probably)
//僅僅一些重要的訊息被記錄)
# warning (only very important / critical messages are logged)
loglevel notice

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
//指定日誌檔案的位置
logfile ""
//要想把日誌記錄到系統日誌,就把它改成 yes,
# To enable logging to the system logger, just set 'syslog-enabled' to yes,
//也可以可選擇性的更新其他的syslog 引數以達到你的要求
# and optionally update the other syslog parameters to suit your needs.
# syslog-enabled no
//設定 syslog 的 identity。
# Specify the syslog identity.
# syslog-ident redis
//設定 syslog 的 facility,必須是 USER 或者是 LOCAL0-LOCAL7 之間的值。
# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
# syslog-facility local0
//設定資料庫的數目。預設資料庫是 DB 0,你可以在每個連線上使用 select <dbid> 命令選擇一個不同的資料庫,
# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
//但是 dbid 必須是一個介於 0 到 databasees - 1 之間的值
# dbid is a number between 0 and 'databases'-1
databases 16

################################ SNAPSHOTTING快照   ################################
//存 DB 到磁碟:
# Save the DB on disk:
//格式:save <間隔時間(秒)> <寫入次數>
#   save <seconds> <changes>
//根據給定的時間間隔和寫入次數將資料儲存到磁碟
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
//下面的例子的意思是:
/*
900 秒內如果至少有 1 個 key 的值變化,則儲存
300 秒內如果至少有 10 個 key 的值變化,則儲存
60 秒內如果至少有 10000 個 key 的值變化,則儲存
*/
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
//注意:你可以註釋掉所有的 save 行來停用儲存功能,也可以直接一個空字串來實現停用.
#   Note: you can disable saving completely by commenting out all "save" lines.
#
#   It is also possible to remove all the previously configured save
#   points by adding a save directive with a single empty string argument
#   like in the following example:
#
#   save ""

save 900 1
save 300 10
save 60 10000
/*
    預設情況下,如果 redis 最後一次的後臺儲存失敗,redis 將停止接受寫操作,這樣以一種強硬的方式讓使用者知道資料不能正確的持久化到磁碟,否則就會沒人注意到災難的發生。
*/
# By default Redis will stop accepting writes if RDB snapshots are enabled
# (at least one save point) and the latest background save failed.
# This will make the user aware (in a hard way) that data is not persisting
# on disk properly, otherwise chances are that no one will notice and some
# disaster will happen.
//如果後臺儲存程序重新啟動工作了,redis 也將自動的允許寫操作。
# If the background saving process will start working again Redis will
# automatically allow writes again.
//然而你要是安裝了靠譜的監控,你可能不希望 redis 這樣做,那你就改成 no 好了。或者您可能希望禁用此功能,Redis會繼續工作,即使磁碟有問題。
# However if you have setup your proper monitoring of the Redis server
# and persistence, you may want to disable this feature so that Redis will
# continue to work as usual even if there are problems with disk,
# permissions, and so forth.
stop-writes-on-bgsave-error yes
//是否在 dump .rdb 資料庫的時候使用 LZF 壓縮字串
# Compress string objects using LZF when dump .rdb databases?
//預設都設為 yes
# For default that's set to 'yes' as it's almost always a win.
//如果你希望儲存子程序節省點 cpu ,你就設定它為 no ,
# If you want to save some CPU in the saving child set it to 'no' but
//不過這個資料集可能就會比較大
# the dataset will likely be bigger if you have compressible values or keys.
rdbcompression yes

# Since version 5 of RDB a CRC64 checksum is placed at the end of the file.
# This makes the format more resistant to corruption but there is a performance
# hit to pay (around 10%) when saving and loading RDB files, so you can disable it
# for maximum performances.
#
# RDB files created with checksum disabled have a checksum of zero that will
# tell the loading code to skip the check.
//是否校驗rdb檔案
rdbchecksum yes
//設定 dump 的檔案位置
# The filename where to dump the DB
dbfilename dump.rdb
//工作目錄
# The working directory.
//例如上面的 dbfilename 只指定了檔名,
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
//但是它會寫入到這個目錄下。這個配置項一定是個目錄,而不能是檔名。
# Note that you must specify a directory here, not a file name.
dir ./

################################# REPLICATION主從複製 #################################
//主從複製。使用 slaveof 來讓一個 redis 例項成為另一個reids 例項的副本。
//注意這個只需要在 slave 上配置。
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. A few things to understand ASAP about Redis replication.
//對redis複製需要了解:
//Redis複製是非同步的,但你可以配置一個主停止接受寫入,如果它似乎與至少一個給定數量的叢屬沒有連線。
# 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 slaves.
//Redis從屬可以執行部分同步主redis,如果複製鏈路則丟失相對少量的時間。
# 2) Redis slaves 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.
//複製是自動的,不需要使用者干預。在網路分割槽的從屬會自動嘗試重新連線到他們的主redis並同步。
# 3) Replication is automatic and does not need user intervention. After a
#    network partition slaves automatically try to reconnect to masters
#    and resynchronize with them.
#
# slaveof <masterip> <masterport>
//如果master設定了requirepass,那麼slave要連上master,需要有master的密碼才行。masterauth就是用來配置master的密碼,這樣可以在連上master後進行認證。
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth <master-password>
//當從庫同主機失去連線或者複製正在進行,從機庫有兩種執行方式:1) 如果slave-serve-stale-data設定為yes(預設設定),從庫會繼續響應客戶端的請求。2) 如果slave-serve-stale-data設定為no,除去INFO和SLAVOF命令之外的任何請求都會返回一個錯誤”SYNC with master in progress”。
# When a slave loses its connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
#    still reply to client requests, possibly with out of date data, or the
#    data set may just be empty if this is the first synchronization.
#
# 2) if slave-serve-stale-data is set to 'no' the slave will reply with
#    an error "SYNC with master in progress" to all the kind of commands
#    but to INFO and SLAVEOF.
#
slave-serve-stale-data yes
/*你可以配置一個 slave 實體是否接受寫入操作。
通過寫入操作來儲存一些短暫的資料對於一個 slave 例項來說可能是有用的,
因為相對從 master 重新同步數而言,據資料寫入到 slave 會更容易被刪除。
但是如果客戶端因為一個錯誤的配置寫入,也可能會導致一些問題。
*/
# You can configure a slave instance to accept writes or not. Writing against
# a slave instance may be useful to store some ephemeral data (because data
# written on a slave will be easily deleted after resync with the master) but
# may also cause problems if clients are writing to it because of a
# misconfiguration.
//從 redis 2.6 版起,預設 slaves 都是隻讀的。
# Since Redis 2.6 by default slaves are read-only.
#
# Note: read only slaves are not designed to be exposed to untrusted clients
# on the internet. It's just a protection layer against misuse of the instance.
# Still a read only slave exports by default all the administrative commands
# such as CONFIG, DEBUG, and so forth. To a limited extent you can improve
# security of read only slaves using 'rename-command' to shadow all the
# administrative / dangerous commands.
//作為從伺服器,預設情況下是隻讀的(yes),可以修改成NO,用於寫(不建議)。
slave-read-only yes

# Replication SYNC strategy: disk or socket.
#
# -------------------------------------------------------
# WARNING: DISKLESS REPLICATION IS EXPERIMENTAL CURRENTLY
# -------------------------------------------------------
/*
是否使用socket方式複製資料。目前redis複製提供兩種方式,disk和socket。如果新的slave連上來或者重連的slave無法部分同步,就會執行全量同步,master會生成rdb檔案。有2種方式:disk方式是master建立一個新的程序把rdb檔案儲存到磁碟,再把磁碟上的rdb檔案傳遞給slave。socket是master建立一個新的程序,直接把rdb檔案以socket的方式發給slave。disk方式的時候,當一個rdb儲存的過程中,多個slave都能共享這個rdb檔案。socket的方式就的一個個slave順序複製。在磁碟速度緩慢,網速快的情況下推薦用socket方式。
*/
# New slaves and reconnecting slaves that are not able to continue the replication
# process just receiving differences, need to do what is called a "full
# synchronization". An RDB file is transmitted from the master to the slaves.
# The transmission can happen in two different ways:
#
# 1) Disk-backed: The Redis master creates a new process that writes the RDB
#                 file on disk. Later the file is transferred by the parent
#                 process to the slaves incrementally.
# 2) Diskless: The Redis master creates a new process that directly writes the
#              RDB file to slave sockets, without touching the disk at all.
#
# With disk-backed replication, while the RDB file is generated, more slaves
# can be queued and served with the RDB file as soon as the current child producing
# the RDB file finishes its work. With diskless replication instead once
# the transfer starts, new slaves arriving will be queued and a new transfer
# will start when the current one terminates.
#
# When diskless replication is used, the master waits a configurable amount of
# time (in seconds) before starting the transfer in the hope that multiple slaves
# will arrive and the transfer can be parallelized.
#
# With slow disks and fast (large bandwidth) networks, diskless replication
# works better.
repl-diskless-sync no

# When diskless replication is enabled, it is possible to configure the delay
# the server waits in order to spawn the child that transfers the RDB via socket
# to the slaves.
#
# This is important since once the transfer starts, it is not possible to serve
# new slaves arriving, that will be queued for the next RDB transfer, so the server
# waits a delay in order to let more slaves arrive.
#
# The delay is specified in seconds, and by default is 5 seconds. To disable
# it entirely just set it to 0 seconds and the transfer will start ASAP.
//diskless複製的延遲時間,防止設定為0。一旦複製開始,節點不會再接收新slave的複製請求直到下一個rdb傳輸。所以最好等待一段時間,等更多的slave連上來。
repl-diskless-sync-delay 5

# Slaves send PINGs to server in a predefined interval. It's possible to change
# this interval with the repl_ping_slave_period option. The default value is 10
# seconds.
//slave根據指定的時間間隔向伺服器傳送ping請求。時間間隔可以通過 repl_ping_slave_period 來設定,預設10秒
# repl-ping-slave-period 10

# The following option sets the replication timeout for:
#
# 1) Bulk transfer I/O during SYNC, from the point of view of slave.
# 2) Master timeout from the point of view of slaves (data, pings).
# 3) Slave timeout from the point of view of masters (REPLCONF ACK pings).
#
# It is important to make sure that this value is greater than the value
# specified for repl-ping-slave-period otherwise a timeout will be detected
# every time there is low traffic between the master and the slave.
//複製連線超時時間。master和slave都有超時時間的設定。master檢測到slave上次傳送的時間超過repl-timeout,即認為slave離線,清除該slave資訊。slave檢測到上次和master互動的時間超過repl-timeout,則認為master離線。需要注意的是repl-timeout需要設定一個比repl-ping-slave-period更大的值,不然會經常檢測到超時。
# repl-timeout 60

# Disable TCP_NODELAY on the slave socket after SYNC?
#
# If you select "yes" Redis will use a smaller number of TCP packets and
# less bandwidth to send data to slaves. But this can add a delay for
# the data to appear on the slave side, up to 40 milliseconds with
# Linux kernels using a default configuration.
#
# If you select "no" the delay for data to appear on the slave side will
# be reduced but more bandwidth will be used for replication.
#
# By default we optimize for low latency, but in very high traffic conditions
# or when the master and slaves are many hops away, turning this to "yes" may
# be a good idea.
//是否禁止複製tcp連結的tcp nodelay引數,可傳遞yes或者no。預設是no,即使用tcp nodelay。如果master設定了yes來禁止tcp nodelay設定,在把資料複製給slave的時候,會減少包的數量和更小的網路頻寬。但是這也可能帶來資料的延遲。預設我們推薦更小的延遲,但是在資料量傳輸很大的場景下,建議選擇yes。
repl-disable-tcp-nodelay no

# Set the replication backlog size. The backlog is a buffer that accumulates
# slave data when slaves are disconnected for some time, so that when a slave
# wants to reconnect again, often a full resync is not needed, but a partial
# resync is enough, just passing the portion of data the slave missed while
# disconnected.
#
# The bigger the replication backlog, the longer the time the slave can be
# disconnected and later be able to perform a partial resynchronization.
#
# The backlog is only allocated once there is at least a slave connected.
//複製緩衝區大小,這是一個環形複製緩衝區,用來儲存最新複製的命令。這樣在slave離線的時候,不需要完全複製master的資料,如果可以執行部分同步,只需要把緩衝區的部分資料複製給slave,就能恢復正常複製狀態。緩衝區的大小越大,slave離線的時間可以更長,複製緩衝區只有在有slave連線的時候才分配記憶體。沒有slave的一段時間,記憶體會被釋放出來,預設1m。
# repl-backlog-size 1mb

# After a master has no longer connected slaves for some time, the backlog
# will be freed. The following option configures the amount of seconds that
# need to elapse, starting from the time the last slave disconnected, for
# the backlog buffer to be freed.
#
# A value of 0 means to never release the backlog.
//master沒有slave一段時間會釋放複製緩衝區的記憶體,repl-backlog-ttl用來設定該時間長度。單位為秒。
# repl-backlog-ttl 3600

# The slave priority is an integer number published by Redis in the INFO output.
# It is used by Redis Sentinel in order to select a slave to promote into a
# master if the master is no longer working correctly.
#
# A slave with a low priority number is considered better for promotion, so
# for instance if there are three slaves with priority 10, 100, 25 Sentinel will
# pick the one with priority 10, that is the lowest.
#
# However a special priority of 0 marks the slave as not able to perform the
# role of master, so a slave with priority of 0 will never be selected by
# Redis Sentinel for promotion.
#
# By default the priority is 100.
//當master不可用,Sentinel會根據slave的優先順序選舉一個master。最低的優先順序的slave,當選master。而配置成0,永遠不會被選舉。
slave-priority 100

# It is possible for a master to stop accepting writes if there are less than
# N slaves connected, having a lag less or equal than M seconds.
#
# The N slaves need to be in "online" state.
#
# The lag in seconds, that must be <= the specified value, is calculated from
# the last ping received from the slave, that is usually sent every second.
#
# This option does not GUARANTEE that N replicas will accept the write, but
# will limit the window of exposure for lost writes in case not enough slaves
# are available, to the specified number of seconds.
#
# For example to require at least 3 slaves with a lag <= 10 seconds use:
//redis提供了可以讓master停止寫入的方式,如果配置了min-slaves-to-write,健康的slave的個數小於N,mater就禁止寫入。master最少得有多少個健康的slave存活才能執行寫命令。這個配置雖然不能保證N個slave都一定能接收到master的寫操作,但是能避免沒有足夠健康的slave的時候,master不能寫入來避免資料丟失。設定為0是關閉該功能。
# min-slaves-to-write 3
//延遲小於min-slaves-max-lag秒的slave才認為是健康的slave。
# min-slaves-max-lag 10
#
# Setting one or the other to 0 disables the feature.
#
# By default min-slaves-to-write is set to 0 (feature disabled) and
//設定1或另一個設定為0禁用這個特性。
# min-slaves-max-lag is set to 10.

################################## SECURITY安全 ###################################
//requirepass配置可以讓使用者使用AUTH命令來認證密碼,才能使用其他命令。這讓redis可以使用在不受信任的網路中。為了保持向後的相容性,可以註釋該命令,因為大部分使用者也不需要認證。使用requirepass的時候需要注意,因為redis太快了,每秒可以認證15w次密碼,簡單的密碼很容易被攻破,所以最好使用一個更復雜的密碼。
# Require clients to issue AUTH <PASSWORD> before processing any other
# commands.  This might be useful in environments in which you do not trust
# others with access to the host running redis-server.
#
# This should stay commented out for backward compatibility and because most
# people do not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can try up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to break.
#
# requirepass foobared

# Command renaming.
#
# It is possible to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# hard to guess so that it will still be available for internal-use tools
# but not available for general clients.
#
# Example:
//把危險的命令給修改成其他名稱。比如CONFIG命令可以重新命名為一個很難被猜到的命令,這樣使用者不能使用,而內部工具還能接著使用。
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possible to completely kill a command by renaming it into
# an empty string:
//設定成一個空的值,可以禁止一個命令
# rename-command CONFIG ""
#
# Please note that changing the name of commands that are logged into the
# AOF file or transmitted to slaves may cause problems.

################################### LIMITS 限制####################################

# 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'.
//設定能連上redis的最大客戶端連線數量。預設是10000個客戶端連線。由於redis不區分連線是客戶端連線還是內部開啟檔案或者和slave連線等,所以maxclients最小建議設定到32。如果超過了maxclients,redis會給新的連線傳送’max number of clients reached’,並關閉連線。
# maxclients 10000

# Don't use more memory than 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 cache, or to set
# a hard memory limit for an instance (using the 'noeviction' policy).
#
# WARNING: If you have slaves attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the slaves 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 slaves 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 slaves attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for slave
# output buffers (but this is not needed if the policy is 'noeviction').
//redis配置的最大記憶體容量。當記憶體滿了,需要配合maxmemory-policy策略進行處理。注意slave的輸出緩衝區是不計算在maxmemory內的。所以為了防止主機記憶體使用完,建議設定的maxmemory需要更小一些。
# maxmemory <bytes>

# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached. You can select among five behaviors:
#
# volatile-lru -> remove the key with an expire set using an LRU algorithm
# allkeys-lru -> remove any key according to the LRU algorithm
# volatile-random -> remove a random key with an expire set
# allkeys-random -> remove a random key, any key
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
# noeviction -> don't expire at all, just return an error on write operations
#
# Note: with any of the above policies, Redis will return an error on write
#       operations, when there are no suitable keys for eviction.
#
#       At the date of writing these commands are: 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
#
# The default is:
/*
記憶體容量超過maxmemory後的處理策略。
#volatile-lru:利用LRU演算法移除設定過過期時間的key。
#volatile-random:隨機移除設定過過期時間的key。
#volatile-ttl:移除即將過期的key,根據最近過期時間來刪除(輔以TTL)
#allkeys-lru:利用LRU演算法移除任何key。
#allkeys-random:隨機移除任何key。
#noeviction:不移除任何key,只是返回一個寫錯誤。
#上面的這些驅逐策略,如果redis沒有合適的key驅逐,對於寫命令,還是會返回錯誤。redis將不再接收寫請求,只接收get請求。
*/
# maxmemory-policy noeviction

# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can tune it for speed or
# accuracy. For default Redis will check five keys and pick the one that was
# used less recently, you can change the sample size using the following
# configuration directive.
#
# The default of 5 produces good enough results. 10 Approximates very closely
# true LRU but costs a bit more CPU. 3 is very fast but not very accurate.
//lru檢測的樣本數。使用lru或者ttl淘汰演算法,從需要淘汰的列表中隨機選擇sample個key,選出閒置時間最長的key移除。
# maxmemory-samples 5

############################## 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.
/*
預設redis使用的是rdb方式持久化,這種方式在許多應用中已經足夠用了。但是redis如果中途宕機,會導致可能有幾分鐘的資料丟失,根據save來策略進行持久化,Append Only File是另一種持久化方式,可以提供更好的持久化特性。Redis會把每次寫入的資料在接收後都寫入 appendonly.aof 檔案,每次啟動時Redis都會先把這個檔案的資料讀入記憶體裡,先忽略RDB檔案。
*/
appendonly no
//aof檔名
# The name of the append only file (default: "appendonly.aof")

appendfilename "appendonly.aof"

# The fsync() call tells the Operating System to actually write data on disk
# instead of waiting for more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just try to do it ASAP.
#
# Redis supports three different modes:
#
# no: don't fsync, just let the OS flush the data when it wants. Faster.
# always: fsync after every write to the append only log. Slow, Safest.
# everysec: fsync only one time every second. Compromise.
#
# The default is "everysec", as that's usually the right compromise between
# speed and data safety. It's up to you to understand if you can relax this to
# "no" that will let the operating system flush the output buffer when
# it wants, for better performances (but if you can live with the idea of
# some data loss consider the default persistence mode that's snapshotting),
# or on the contrary, use "always" that's very slow but a bit safer than
# everysec.
#
# More details please check the following article:
# http://antirez.com/post/redis-persistence-demystified.html
#
# If unsure, use "everysec".

# appendfsync always
/*
#aof持久化策略的配置
#no表示不