本文將主要來探討redis強(qiáng)大的配置文件。
我現(xiàn)在使用的redis版本為2.6。首先奉上配置文件的源文件。
# Redis configuration file example # 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 => 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. # 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. daemonize no # 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 # Accept connections on the specified port, default is 6379. # If port 0 is specified Redis will not listen on a TCP socket. port 6666# If you want you can bind a single interface, if the bind option is not # specified all the interfaces will listen for incoming connections. # # 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# Close the connection after a client is idle for N seconds (0 to disable) timeout 0# Set server verbosity to 'debug'# it can be one of: # 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 'stdout' 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/nulllogfile stdout # 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 # 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 is a number between 0 and 'databases'-1databases 16################################ SNAPSHOTTING ################################# # # Save the DB on disk: # # 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. # # 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 # # Note: you can disable saving at all commenting all the "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 1save 300 10save 60 10000# 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 an hard way) that data is not persisting # on disk properly, otherwise chances are that no one will notice and some # distater will happen. # # If the background saving process will start working again Redis will # automatically allow writes again. # # 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 usually even if there are problems with disk, # permissions, and so forth. stop-writes-on-bgsave-error yes # Compress string objects using LZF when dump .rdb databases?# For default that's set to 'yes' as it's almost always a win. # 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 verison 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. rdbchecksum yes # The filename where to dump the DB dbfilename dump.rdb # The working directory. # # The DB will be written inside this directory, with the filename specified # above using the 'dbfilename' configuration directive. # # Also the Append Only File will be created inside this directory. # # Note that you must specify a directory here, not a file name. dir ./################################# REPLICATION ################################# # Master-Slave replication. Use slaveof to make a Redis instance a copy of # another Redis server. Note that the configuration is local to the slave # so for example it is possible to configure the slave to save the DB with a # different interval, or to listen to another port, and so on. # # slaveof <masterip> <masterport># 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># When a slave lost the 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 # 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. # # 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 extend you can improve # security of read only slaves using 'rename-command' to shadow all the # administrative / dangerous commands. slave-read-only yes # 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. # # repl-ping-slave-period 10# The following option sets a timeout for both Bulk transfer I/O timeout and # master data or ping response timeout. The default value is 60 seconds. # # 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. # # repl-timeout 60# 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 wtih 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. slave-priority 100################################## SECURITY ################################### # 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 # of hard to guess so that it will be still available for internal-use # tools but not available for general clients. # # Example: # # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # # It is also possible to completely kill a command renaming it into # an empty string: # # rename-command CONFIG ""################################### 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 ot 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'. # # 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 # accordingly to the eviction policy selected (see maxmemmory-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# an 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'). # # maxmemory <bytes># MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached? You can select among five behavior: # # volatile-lru -> remove the key with an expire set using an LRU algorithm # allkeys-lru -> remove any key accordingly 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 all the kind of policies, Redis will return an error on write # operations, when there are not suitable keys for eviction. # # At the date of writing this 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-policy volatile-lru # LRU and minimal TTL algorithms are not precise algorithms but approximated # algorithms (in order to save memory), so you can select as well the sample # size to check. For instance for default Redis will check three keys and # pick the one that was used less recently, you can change the sample size # using the following configuration directive. # # maxmemory-samples 3############################## 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 # 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 to wait 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" 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 appendfsync everysec # appendfsync no # When the AOF fsync policy is set to always or everysec, and a background # saving process (a background save or AOF log background rewriting) is# performing a lot of I/O against the disk, in some Linux configurations # Redis may block too long on the fsync() call. Note that there is no fix for# this currently, as even performing fsync in a different thread will block # our synchronous write(2) call. # # In order to mitigate this problem it's possible to use the following option# that will prevent fsync() from being called in the main process while a # BGSAVE or BGREWRITEAOF is in progress. # # This means that while another child is saving the durability of Redis is# the same as "appendfsync none", that in practical 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. # Redis is able to automatically rewrite the log file implicitly calling # BGREWRITEAOF when the AOF log size will growth by the specified percentage. # # This is how it works: Redis remembers the size of the AOF file after the # latest rewrite (or if no rewrite happened since the restart, the size of # the AOF at startup is used). # # This base size is compared to the current size. If the current size is# bigger than the specified percentage, the rewrite is triggered. Also # you need to specify a minimal size for the AOF file to be rewritten, this# is useful to avoid rewriting the AOF file even if the percentage increase # is reached but it is still pretty small. # # Specify a percentage of zero in order to disable the automatic AOF # rewrite feature. auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb ################################ LUA SCRIPTING ############################### # Max execution time of a Lua script in milliseconds. # # If the maximum execution time is reached Redis will log that a script is# still in execution after the maximum allowed time and will start to # reply to queries with an error. # # When a long running script exceed the maximum execution time only the # SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be # used to stop a script that did not yet called write commands. The second # is the only way to shut down the server in the case a write commands was # already issue by the script but the user don't want to wait for the natural# termination of the script. # # Set it to 0 or a negative value for unlimited execution without warnings. lua-time-limit 5000################################## SLOW LOG ################################### # The Redis Slow Log is a system to log queries that exceeded a specified # execution time. The execution time does not include the I/O operations # like talking with the client, sending the reply and so forth, # but just the time needed to actually execute the command (this is the only # stage of command execution where the thread is blocked and can not serve # other requests in the meantime). # # You can configure the slow log with two parameters: one tells Redis # what is the execution time, in microseconds, to exceed in order for the # command to get logged, and the other parameter is the length of the # slow log. When a new command is logged the oldest one is removed from the # queue of logged commands. # The following time is expressed in microseconds, so 1000000 is equivalent # to one second. Note that a negative number disables the slow log, while# a value of zero forces the logging of every command. slowlog-log-slower-than 10000# There is no limit to this length. Just be aware that it will consume memory. # You can reclaim memory used by the slow log with SLOWLOG RESET. slowlog-max-len 128############################### ADVANCED CONFIG ############################### # Hashes are encoded using a memory efficient data structure when they have a # small number of entries, and the biggest entry does not exceed a given # threshold. These thresholds can be configured using the following directives. hash-max-ziplist-entries 512hash-max-ziplist-value 64# Similarly to hashes, small lists are also encoded in a special way in order # to save a lot of space. The special representation is only used when # you are under the following limits: list-max-ziplist-entries 512list-max-ziplist-value 64# Sets have a special encoding in just one case: when a set is composed # of just strings that happens to be integers in radix 10 in the range # of 64 bit signed integers. # The following configuration setting sets the limit in the size of the # set in order to use this special memory saving encoding.set-max-intset-entries 512# Similarly to hashes and lists, sorted sets are also specially encoded in# order to save a lot of space. This encoding is only used when the length and # elements of a sorted set are below the following limits: zset-max-ziplist-entries 128zset-max-ziplist-value 64# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in# order to help rehashing the main Redis hash table (the one mapping top-level # keys to values). The hash table implementation Redis uses (see dict.c) # performs a lazy rehashing: the more operation you run into an hash table # that is rehashing, the more rehashing "steps" are performed, so if the # server is idle the rehashing is never complete and some more memory is used # by the hash table. # # The default is to use this millisecond 10 times every second in order to # active rehashing the main dictionaries, freeing memory when possible. # # If unsure: # use "activerehashing no" if you have hard latency requirements and it is# not a good thing in your environment that Redis can reply form time to time # to queries with 2 milliseconds delay. # # use "activerehashing yes" if you don't have such hard requirements but# want to free memory asap when possible. activerehashing yes # The client output buffer limits can be used to force disconnection of clients # that are not reading data from the server fast enough for some reason (a # common reason is that a Pub/Sub client can't consume messages as fast as the# publisher can produce them). # # The limit can be set differently for the three different classes of clients: # # normal -> normal clients # slave -> slave clients and MONITOR clients # pubsub -> clients subcribed to at least one pubsub channel or pattern # # The syntax of every client-output-buffer-limit directive is the following: # # client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds># # A client is immediately disconnected once the hard limit is reached, or if# the soft limit is reached and remains reached for the specified number of # seconds (continuously). # So for instance if the hard limit is 32 megabytes and the soft limit is# 16 megabytes / 10 seconds, the client will get disconnected immediately # if the size of the output buffers reach 32 megabytes, but will also get# disconnected if the client reaches 16 megabytes and continuously overcomes # the limit for 10 seconds. # # By default normal clients are not limited because they don't receive data# without asking (in a push way), but just after a request, so only # asynchronous clients may create a scenario where data is requested faster # than it can read. # # Instead there is a default limit for pubsub and slave clients, since # subscribers and slaves receive data in a push fashion. # # Both the hard or the soft limit can be disabled just setting it to zero. client-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 256mb 64mb 60client-output-buffer-limit pubsub 32mb 8mb 60################################## INCLUDES ################################### # Include one or more other config files here. This is useful if you # have a standard template that goes to all Redis server but also need # to customize a few per-server settings. Include files can include # other files, so use this wisely. # # include /path/to/local.conf # include /path/to/other.conf
配置文件解析包括以下幾個(gè)部分
一、基本配置
二、快照
三、同步
四、安全
五、限制
六、純累加模式
七、LUA腳本
八、慢查詢?nèi)罩?/p>
九、高級(jí)配置
十、其他
一、基本配置
1、
# 注意單位問題:當(dāng)需要設(shè)置內(nèi)存大小的時(shí)候,可以使用類似1k、5GB、4M這樣的常見格式: # # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 bytes # 1g => 1000000000 bytes # 1gb => 1024*1024*1024 bytes # # 單位是大小寫不敏感的,所以1GB 1Gb 1gB的寫法都是完全一樣的。
2、
# Redis默認(rèn)是不作為守護(hù)進(jìn)程來運(yùn)行的。你可以把這個(gè)設(shè)置為"yes"讓它作為守護(hù)進(jìn)程來運(yùn)行。 # 注意,當(dāng)作為守護(hù)進(jìn)程的時(shí)候,Redis會(huì)把進(jìn)程ID寫到 /var/run/redis.pid daemonize no
3、
# 當(dāng)以守護(hù)進(jìn)程方式運(yùn)行的時(shí)候,Redis會(huì)把進(jìn)程ID默認(rèn)寫到 /var/run/redis.pid。你可以在這里修改路徑。 pidfile /var/run/redis.pid
4、
# 接受連接的特定端口,默認(rèn)是6379。 # 如果端口設(shè)置為0,Redis就不會(huì)監(jiān)聽TCP套接字。 port 6379
5、
# 如果你想的話,你可以綁定單一接口;如果這里沒單獨(dú)設(shè)置,那么所有接口的連接都會(huì)被監(jiān)聽。 # # bind 127.0.0.1
6、
# 指定用來監(jiān)聽連接的unxi套接字的路徑。這個(gè)沒有默認(rèn)值,所以如果你不指定的話,Redis就不會(huì)通過unix套接字來監(jiān)聽。 # # unixsocket /tmp/redis.sock # unixsocketperm 755
7、
#一個(gè)客戶端空閑多少秒后關(guān)閉連接。(0代表禁用,永不關(guān)閉) timeout 0
8、
# 設(shè)置服務(wù)器調(diào)試等級(jí)。 # 可能值: # debug (很多信息,對(duì)開發(fā)/測(cè)試有用) # verbose (很多精簡(jiǎn)的有用信息,但是不像debug等級(jí)那么多) # notice (適量的信息,基本上是你生產(chǎn)環(huán)境中需要的程度) # warning (只有很重要/嚴(yán)重的信息會(huì)記錄下來) loglevel verbose
9、
# 指明日志文件名。也可以使用"stdout"來強(qiáng)制讓Redis把日志信息寫到標(biāo)準(zhǔn)輸出上。 # 注意:如果Redis以守護(hù)進(jìn)程方式運(yùn)行,而你設(shè)置日志顯示到標(biāo)準(zhǔn)輸出的話,那么日志會(huì)發(fā)送到 /dev/nulllogfile stdout
10、
# 然后根據(jù)需要設(shè)置其他一些syslog參數(shù)就可以了。 # syslog-enabled no # 指明syslog身份 # syslog-ident redis # 指明syslog的設(shè)備。必須是一個(gè)用戶或者是 LOCAL0 ~ LOCAL7 之一。 # syslog-facility local0
11、
# 設(shè)置數(shù)據(jù)庫個(gè)數(shù)。默認(rèn)數(shù)據(jù)庫是 DB 0,你可以通過SELECT <dbid> WHERE dbid(0~'databases' - 1)來為每個(gè)連接使用不同的數(shù)據(jù)庫。 databases 16
二、快照
12、
################################ 快照 ################################# # # 把數(shù)據(jù)庫存到磁盤上: # # save <seconds> <changes># # 會(huì)在指定秒數(shù)和數(shù)據(jù)變化次數(shù)之后把數(shù)據(jù)庫寫到磁盤上。 # # 下面的例子將會(huì)進(jìn)行把數(shù)據(jù)寫入磁盤的操作: # 900秒(15分鐘)之后,且至少1次變更 # 300秒(5分鐘)之后,且至少10次變更 # 60秒之后,且至少10000次變更 # # 注意:你要想不寫磁盤的話就把所有 "save" 設(shè)置注釋掉就行了。 save 900 1save 300 10save 60 10000
13、
#后臺(tái)存儲(chǔ)錯(cuò)誤停止寫。 stop-writes-on-bgsave-error yes
14、
# 當(dāng)導(dǎo)出到 .rdb 數(shù)據(jù)庫時(shí)是否用LZF壓縮字符串對(duì)象。 # 默認(rèn)設(shè)置為 "yes",所以幾乎總是生效的。 # 如果你想節(jié)省CPU的話你可以把這個(gè)設(shè)置為 "no",但是如果你有可壓縮的key的話,那數(shù)據(jù)文件就會(huì)更大了。 rdbcompression yes
15、
#存儲(chǔ)和加載rdb文件時(shí)校驗(yàn)。 rdbchecksum yes
16、
# 數(shù)據(jù)庫的文件名 dbfilename dump.rdb
17、
# 工作目錄 # # 數(shù)據(jù)庫會(huì)寫到這個(gè)目錄下,文件名就是上面的 "dbfilename" 的值。 # # 累加文件也放這里。 # # 注意你這里指定的必須是目錄,不是文件名。 dir ./
三、同步
18、
# # 主從同步。通過 slaveof 配置來實(shí)現(xiàn)Redis實(shí)例的備份。 # 注意,這里是本地從遠(yuǎn)端復(fù)制數(shù)據(jù)。也就是說,本地可以有不同的數(shù)據(jù)庫文件、綁定不同的IP、監(jiān)聽不同的端口。 # # slaveof <masterip> <masterport> # 如果master設(shè)置了密碼(通過下面的 "requirepass" 選項(xiàng)來配置),那么slave在開始同步之前必須進(jìn)行身份驗(yàn)證,否則它的同步請(qǐng)求會(huì)被拒絕。 # # masterauth <master-password> # 當(dāng)一個(gè)slave失去和master的連接,或者同步正在進(jìn)行中,slave的行為有兩種可能: # # 1) 如果 slave-serve-stale-data 設(shè)置為 "yes" (默認(rèn)值),slave會(huì)繼續(xù)響應(yīng)客戶端請(qǐng)求,可能是正常數(shù)據(jù),也可能是還沒獲得值的空數(shù)據(jù)。 # 2) 如果 slave-serve-stale-data 設(shè)置為 "no",slave會(huì)回復(fù)"正在從master同步(SYNC with master in progress)"來處理各種請(qǐng)求,除了 INFO 和 SLAVEOF 命令。 # slave-serve-stale-data yes
19、
#如果為yes,slave實(shí)例只讀,如果為no,slave實(shí)例可讀可寫。 slave-read-only yes # slave根據(jù)指定的時(shí)間間隔向服務(wù)器發(fā)送ping請(qǐng)求。 # 時(shí)間間隔可以通過 repl_ping_slave_period 來設(shè)置。 # 默認(rèn)10秒。 # # repl-ping-slave-period 10 # 下面的選項(xiàng)設(shè)置了大塊數(shù)據(jù)I/O、向master請(qǐng)求數(shù)據(jù)和ping響應(yīng)的過期時(shí)間。 # 默認(rèn)值60秒。 # # 一個(gè)很重要的事情是:確保這個(gè)值比 repl-ping-slave-period 大,否則master和slave之間的傳輸過期時(shí)間比預(yù)想的要短。 # # repl-timeout 60
20、
#如果master不能再正常工作,那么會(huì)在多個(gè)slave中,選擇優(yōu)先值最小的一個(gè)slave提升為master,優(yōu)先值為0表示不能提升為master。 slave-priority 100
四、安全
21、
# 要求客戶端在處理任何命令時(shí)都要驗(yàn)證身份和密碼。 # 這在你信不過來訪者時(shí)很有用。 # # 為了向后兼容的話,這段應(yīng)該注釋掉。而且大多數(shù)人不需要身份驗(yàn)證(例如:它們運(yùn)行在自己的服務(wù)器上。) # # 警告:因?yàn)镽edis太快了,所以居心不良的人可以每秒嘗試150k的密碼來試圖破解密碼。 # 這意味著你需要一個(gè)高強(qiáng)度的密碼,否則破解太容易了。 # # requirepass foobared # 命令重命名 # # 在共享環(huán)境下,可以為危險(xiǎn)命令改變名字。比如,你可以為 CONFIG 改個(gè)其他不太容易猜到的名字,這樣你自己仍然可以使用,而別人卻沒法做壞事了。 # # 例如: # # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52 # # 甚至也可以通過給命令賦值一個(gè)空字符串來完全禁用這條命令: # # rename-command CONFIG ""
五、限制
22、
# # 設(shè)置最多同時(shí)連接客戶端數(shù)量。 # 默認(rèn)沒有限制,這個(gè)關(guān)系到Redis進(jìn)程能夠打開的文件描述符數(shù)量。 # 特殊值"0"表示沒有限制。 # 一旦達(dá)到這個(gè)限制,Redis會(huì)關(guān)閉所有新連接并發(fā)送錯(cuò)誤"達(dá)到最大用戶數(shù)上限(max number of clients reached)"# # maxclients 10000
23、
# 不要用比設(shè)置的上限更多的內(nèi)存。一旦內(nèi)存使用達(dá)到上限,Redis會(huì)根據(jù)選定的回收策略(參見:maxmemmory-policy)刪除key。 # # 如果因?yàn)閯h除策略問題Redis無法刪除key,或者策略設(shè)置為 "noeviction",Redis會(huì)回復(fù)需要更多內(nèi)存的錯(cuò)誤信息給命令。 # 例如,SET,LPUSH等等。但是會(huì)繼續(xù)合理響應(yīng)只讀命令,比如:GET。 # # 在使用Redis作為L(zhǎng)RU緩存,或者為實(shí)例設(shè)置了硬性內(nèi)存限制的時(shí)候(使用 "noeviction" 策略)的時(shí)候,這個(gè)選項(xiàng)還是滿有用的。 # # 警告:當(dāng)一堆slave連上達(dá)到內(nèi)存上限的實(shí)例的時(shí)候,響應(yīng)slave需要的輸出緩存所需內(nèi)存不計(jì)算在使用內(nèi)存當(dāng)中。 # 這樣當(dāng)請(qǐng)求一個(gè)刪除掉的key的時(shí)候就不會(huì)觸發(fā)網(wǎng)絡(luò)問題/重新同步的事件,然后slave就會(huì)收到一堆刪除指令,直到數(shù)據(jù)庫空了為止。 # # 簡(jiǎn)而言之,如果你有slave連上一個(gè)master的話,那建議你把master內(nèi)存限制設(shè)小點(diǎn)兒,確保有足夠的系統(tǒng)內(nèi)存用作輸出緩存。 # (如果策略設(shè)置為"noeviction"的話就不無所謂了) # # maxmemory <bytes>
24、
# 內(nèi)存策略:如果達(dá)到內(nèi)存限制了,Redis如何刪除key。你可以在下面五個(gè)策略里面選:181#182# volatile-lru -> 根據(jù)LRU算法生成的過期時(shí)間來刪除。183# allkeys-lru -> 根據(jù)LRU算法刪除任何key。184# volatile-random -> 根據(jù)過期設(shè)置來隨機(jī)刪除key。185# allkeys->random -> 無差別隨機(jī)刪。186# volatile-ttl -> 根據(jù)最近過期時(shí)間來刪除(輔以TTL)187# noeviction -> 誰也不刪,直接在寫操作時(shí)返回錯(cuò)誤。188# # 注意:對(duì)所有策略來說,如果Redis找不到合適的可以刪除的key都會(huì)在寫操作時(shí)返回一個(gè)錯(cuò)誤。 # # 這里涉及的命令: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和最小TTL算法的實(shí)現(xiàn)都不是很精確,但是很接近(為了省內(nèi)存),所以你可以用樣例做測(cè)試。 # 例如:默認(rèn)Redis會(huì)檢查三個(gè)key然后取最舊的那個(gè),你可以通過下面的配置項(xiàng)來設(shè)置樣本的個(gè)數(shù)。 # # maxmemory-samples 3
六、純累加模式
25、
# 默認(rèn)情況下,Redis是異步的把數(shù)據(jù)導(dǎo)出到磁盤上。這種情況下,當(dāng)Redis掛掉的時(shí)候,最新的數(shù)據(jù)就丟了。 # 如果不希望丟掉任何一條數(shù)據(jù)的話就該用純累加模式:一旦開啟這個(gè)模式,Redis會(huì)把每次寫入的數(shù)據(jù)在接收后都寫入 appendonly.aof 文件。 # 每次啟動(dòng)時(shí)Redis都會(huì)把這個(gè)文件的數(shù)據(jù)讀入內(nèi)存里。 # # 注意,異步導(dǎo)出的數(shù)據(jù)庫文件和純累加文件可以并存(你得把上面所有"save"設(shè)置都注釋掉,關(guān)掉導(dǎo)出機(jī)制)。 # 如果純累加模式開啟了,那么Redis會(huì)在啟動(dòng)時(shí)載入日志文件而忽略導(dǎo)出的 dump.rdb 文件。 # # 重要:查看 BGREWRITEAOF 來了解當(dāng)累加日志文件太大了之后,怎么在后臺(tái)重新處理這個(gè)日志文件。 appendonly no
26、
# 純累加文件名字(默認(rèn):"appendonly.aof") # appendfilename appendonly.aof # fsync() 請(qǐng)求操作系統(tǒng)馬上把數(shù)據(jù)寫到磁盤上,不要再等了。 # 有些操作系統(tǒng)會(huì)真的把數(shù)據(jù)馬上刷到磁盤上;有些則要磨蹭一下,但是會(huì)盡快去做。 # # Redis支持三種不同的模式: # # no:不要立刻刷,只有在操作系統(tǒng)需要刷的時(shí)候再刷。比較快。 # always:每次寫操作都立刻寫入到aof文件。慢,但是最安全。 # everysec:每秒寫一次。折衷方案。 # # 默認(rèn)的 "everysec" 通常來說能在速度和數(shù)據(jù)安全性之間取得比較好的平衡。 # 如果你真的理解了這個(gè)意味著什么,那么設(shè)置"no"可以獲得更好的性能表現(xiàn)(如果丟數(shù)據(jù)的話,則只能拿到一個(gè)不是很新的快照); # 或者相反的,你選擇 "always" 來犧牲速度確保數(shù)據(jù)安全、完整。 # # 如果拿不準(zhǔn),就用 "everysec" # appendfsync always appendfsync everysec # appendfsync no
27、
# 如果AOF的同步策略設(shè)置成 "always" 或者 "everysec",那么后臺(tái)的存儲(chǔ)進(jìn)程(后臺(tái)存儲(chǔ)或?qū)懭階OF日志)會(huì)產(chǎn)生很多磁盤I/O開銷。 # 某些Linux的配置下會(huì)使Redis因?yàn)?nbsp;fsync() 而阻塞很久。 # 注意,目前對(duì)這個(gè)情況還沒有完美修正,甚至不同線程的 fsync() 會(huì)阻塞我們的 write(2) 請(qǐng)求。 # # 為了緩解這個(gè)問題,可以用下面這個(gè)選項(xiàng)。它可以在 BGSAVE 或 BGREWRITEAOF 處理時(shí)阻止 fsync()。 # # 這就意味著如果有子進(jìn)程在進(jìn)行保存操作,那么Redis就處于"不可同步"的狀態(tài)。 # 這實(shí)際上是說,在最差的情況下可能會(huì)丟掉30秒鐘的日志數(shù)據(jù)。(默認(rèn)Linux設(shè)定) # # 如果你有延遲的問題那就把這個(gè)設(shè)為 "yes",否則就保持 "no",這是保存持久數(shù)據(jù)的最安全的方式。 no-appendfsync-on-rewrite no
28、
# 自動(dòng)重寫AOF文件 # # 如果AOF日志文件大到指定百分比,Redis能夠通過 BGREWRITEAOF 自動(dòng)重寫AOF日志文件。 # # 工作原理:Redis記住上次重寫時(shí)AOF日志的大。ɑ蛘咧貑⒑鬀]有寫操作的話,那就直接用此時(shí)的AOF文件), # 基準(zhǔn)尺寸和當(dāng)前尺寸做比較。如果當(dāng)前尺寸超過指定比例,就會(huì)觸發(fā)重寫操作。 # # 你還需要指定被重寫日志的最小尺寸,這樣避免了達(dá)到約定百分比但尺寸仍然很小的情況還要重寫。 # # 指定百分比為0會(huì)禁用AOF自動(dòng)重寫特性。 auto-aof-rewrite-percentage 100auto-aof-rewrite-min-size 64mb
七、LUA腳本
29、
#一個(gè)Lua腳本最長(zhǎng)的執(zhí)行時(shí)間為5000毫秒(5秒),如果為0或負(fù)數(shù)表示無限執(zhí)行時(shí)間。 lua-time-limit 5000
30、
# Redis慢查詢?nèi)罩究梢杂涗洺^指定時(shí)間的查詢。運(yùn)行時(shí)間不包括各種I/O時(shí)間。 # 例如:連接客戶端,發(fā)送響應(yīng)數(shù)據(jù)等。只計(jì)算命令運(yùn)行的實(shí)際時(shí)間(這是唯一一種命令運(yùn)行線程阻塞而無法同時(shí)為其他請(qǐng)求服務(wù)的場(chǎng)景) # # 你可以為慢查詢?nèi)罩九渲脙蓚(gè)參數(shù):一個(gè)是超標(biāo)時(shí)間,單位為微妙,記錄超過個(gè)時(shí)間的命令。 # 另一個(gè)是慢查詢?nèi)罩鹃L(zhǎng)度。當(dāng)一個(gè)新的命令被寫進(jìn)日志的時(shí)候,最老的那個(gè)記錄會(huì)被刪掉。 # # 下面的時(shí)間單位是微秒,所以1000000就是1秒。注意,負(fù)數(shù)時(shí)間會(huì)禁用慢查詢?nèi)罩,?則會(huì)強(qiáng)制記錄所有命令。 slowlog-log-slower-than 10000 # 這個(gè)長(zhǎng)度沒有限制。只要有足夠的內(nèi)存就行。你可以通過 SLOWLOG RESET 來釋放內(nèi)存。(譯者注:日志居然是在內(nèi)存里的Orz) slowlog-max-len 128
八、慢查詢?nèi)罩?/p>
31、
# Redis慢查詢?nèi)罩究梢杂涗洺^指定時(shí)間的查詢。運(yùn)行時(shí)間不包括各種I/O時(shí)間。 # 例如:連接客戶端,發(fā)送響應(yīng)數(shù)據(jù)等。只計(jì)算命令運(yùn)行的實(shí)際時(shí)間(這是唯一一種命令運(yùn)行線程阻塞而無法同時(shí)為其他請(qǐng)求服務(wù)的場(chǎng)景) # # 你可以為慢查詢?nèi)罩九渲脙蓚(gè)參數(shù):一個(gè)是超標(biāo)時(shí)間,單位為微妙,記錄超過個(gè)時(shí)間的命令。 # 另一個(gè)是慢查詢?nèi)罩鹃L(zhǎng)度。當(dāng)一個(gè)新的命令被寫進(jìn)日志的時(shí)候,最老的那個(gè)記錄會(huì)被刪掉。 # # 下面的時(shí)間單位是微秒,所以1000000就是1秒。注意,負(fù)數(shù)時(shí)間會(huì)禁用慢查詢?nèi)罩,?則會(huì)強(qiáng)制記錄所有命令。 slowlog-log-slower-than 10000 # 這個(gè)長(zhǎng)度沒有限制。只要有足夠的內(nèi)存就行。你可以通過 SLOWLOG RESET 來釋放內(nèi)存。(譯者注:日志居然是在內(nèi)存里的Orz) slowlog-max-len 128
九、高級(jí)配置
32、
# 當(dāng)有大量數(shù)據(jù)時(shí),適合用哈希編碼(需要更多的內(nèi)存),元素?cái)?shù)量上限不能超過給定限制。 # 你可以通過下面的選項(xiàng)來設(shè)定這些限制: hash-max-zipmap-entries 512hash-max-zipmap-value 64 # 與哈希相類似,數(shù)據(jù)元素較少的情況下,可以用另一種方式來編碼從而節(jié)省大量空間。 # 這種方式只有在符合下面限制的時(shí)候才可以用: list-max-ziplist-entries 512list-max-ziplist-value 64 # 還有這樣一種特殊編碼的情況:數(shù)據(jù)全是64位無符號(hào)整型數(shù)字構(gòu)成的字符串。 # 下面這個(gè)配置項(xiàng)就是用來限制這種情況下使用這種編碼的最大上限的。set-max-intset-entries 512 # 與第一、第二種情況相似,有序序列也可以用一種特別的編碼方式來處理,可節(jié)省大量空間。 # 這種編碼只適合長(zhǎng)度和元素都符合下面限制的有序序列: zset-max-ziplist-entries 128zset-max-ziplist-value 64
33、
# 哈希刷新,每100個(gè)CPU毫秒會(huì)拿出1個(gè)毫秒來刷新Redis的主哈希表(頂級(jí)鍵值映射表)。 # redis所用的哈希表實(shí)現(xiàn)(見dict.c)采用延遲哈希刷新機(jī)制:你對(duì)一個(gè)哈希表操作越多,哈希刷新操作就越頻繁; # 反之,如果服務(wù)器非常不活躍那么也就是用點(diǎn)內(nèi)存保存哈希表而已。 # # 默認(rèn)是每秒鐘進(jìn)行10次哈希表刷新,用來刷新字典,然后盡快釋放內(nèi)存。 # # 建議: # 如果你對(duì)延遲比較在意的話就用 "activerehashing no",每個(gè)請(qǐng)求延遲2毫秒不太好嘛。 # 如果你不太在意延遲而希望盡快釋放內(nèi)存的話就設(shè)置 "activerehashing yes"。 activerehashing yes
34、
# 哈希刷新,每100個(gè)CPU毫秒會(huì)拿出1個(gè)毫秒來刷新Redis的主哈希表(頂級(jí)鍵值映射表)。 # redis所用的哈希表實(shí)現(xiàn)(見dict.c)采用延遲哈希刷新機(jī)制:你對(duì)一個(gè)哈希表操作越多,哈希刷新操作就越頻繁; # 反之,如果服務(wù)器非常不活躍那么也就是用點(diǎn)內(nèi)存保存哈希表而已。 # # 默認(rèn)是每秒鐘進(jìn)行10次哈希表刷新,用來刷新字典,然后盡快釋放內(nèi)存。 # # 建議: # 如果你對(duì)延遲比較在意的話就用 "activerehashing no",每個(gè)請(qǐng)求延遲2毫秒不太好嘛。 # 如果你不太在意延遲而希望盡快釋放內(nèi)存的話就設(shè)置 "activerehashing yes"。 #重新哈希the main Redis hash table(the one mapping top-level keys to values),這樣會(huì)節(jié)省更多的空間。 activerehashing yes #對(duì)客戶端輸出緩沖進(jìn)行限制可以強(qiáng)迫那些就不從服務(wù)器讀取數(shù)據(jù)的客戶端斷開連接。對(duì)于normal client,第一個(gè)0表示取消hard limit,第二個(gè)0和第三個(gè)0表示取消soft limit,normal client默認(rèn)取消限制,因?yàn)槿绻麤]有尋問,他們是不會(huì)接收數(shù)據(jù)的。 client-output-buffer-limit normal 0 0 0 #對(duì)于slave client和MONITER client,如果client-output-buffer一旦超過256mb,又或者超過64mb持續(xù)60秒,那么服務(wù)器就會(huì)立即斷開客戶端連接。 client-output-buffer-limit slave 256mb 64mb 60 #對(duì)于pubsub client,如果client-output-buffer一旦超過32mb,又或者超過8mb持續(xù)60秒,那么服務(wù)器就會(huì)立即斷開客戶端連接。 client-output-buffer-limit pubsub 32mb 8mb 60
35、
# 包含一個(gè)或多個(gè)其他配置文件。 # 這在你有標(biāo)準(zhǔn)配置模板但是每個(gè)redis服務(wù)器又需要個(gè)性設(shè)置的時(shí)候很有用。 # 包含文件特性允許你引人其他配置文件,所以好好利用吧。 # # include /path/to/local.conf # include /path/to/other.conf
總結(jié)
從上看到下,如果你對(duì)redis研究不是很深入,就像我一樣,會(huì)感覺到redis的配置功能的確是很強(qiáng)大的,貌似也是很復(fù)雜的,不過沒關(guān)系,如果你在實(shí)踐的項(xiàng)目中有用到的話,你會(huì)發(fā)現(xiàn)其實(shí)也沒那么復(fù)雜。下一篇希望簡(jiǎn)單的在C#使用redis。