# 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.
# include /path/to/local.conf # include /path/to/other.conf
1.2 NETWORK
主机配置
1 2 3 4 5 6 7 8
# By default, if no "bind" configuration directive is specified, Redis listens # for connections from all the network interfaces available on the server. # It is possible to listen to just one or multiple selected 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 ::1
# Accept connections on the specified port, default is 6379 (IANA #815344). # If port 0 is specified Redis will not listen on a TCP socket. port 6379
翻译
1
接受指定端口上的连接,默认值为6379。如果指定端口0,Redis将不在TCP套接字上侦听。
客户端超时关闭
1 2
# Close the connection after a client is idle for N seconds (0 to disable) timeout 0
翻译
1
客户端空闲n秒后关闭连接(0表示永不关闭)
1.3 GENERAL
pid文件路径配置
1 2 3 4 5 6 7 8 9 10
# If a pid file is specified, Redis writes it where specified at startup # and removes it at exit. # # When the server runs non daemonized, no pid file is created if none is # specified in the configuration. When the server is daemonized, the pid file # is used even if not specified, defaulting to "/var/run/redis.pid". # # Creating a pid file is best effort: if Redis is not able to create it # nothing bad happens, the server will start and run normally. pidfile /var/run/redis_6379.pid
# Specify the server verbosity level. # This 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 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 ""
# 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'-1 databases 16
翻译
1
设置数据数量,默认数据库为0号库,可以通过SELECT <dbid>指令选择不同的数据库
1.4 SNAPSHOTTING
redis数据持久化
1 2 3 4 5 6
# 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.
翻译
1
如果给定的秒数和给定的对数据库的写入操作数都发生,则将数据持久化到磁盘上。
指定数据库文件名
1 2
# The filename where to dump the DB dbfilename dump.rdb
指定数据库文件目录
1 2 3 4 5 6 7 8 9
# The working directory. # # 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 ./
翻译
1
数据文件就写入到指定的工作目录,文件名由dbfilename配置项指定。
1.5 MEMORY MANAGEMENT
设置最大内存限制
1 2 3 4
# 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). maxmemory <bytes>
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory # is reached. You can select among five behaviors: # # volatile-lru -> Evict using approximated LRU among the keys with an expire set. # allkeys-lru -> Evict any key using approximated LRU. # volatile-lfu -> Evict using approximated LFU among the keys with an expire set. # allkeys-lfu -> Evict any key using approximated LFU. # volatile-random -> Remove a random key among the ones 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 evict anything, just return an error on write operations. # # LRU means Least Recently Used # LFU means Least Frequently Used # # Both LRU, LFU and volatile-ttl are implemented using approximated # randomized algorithms. # The default is: # # maxmemory-policy noeviction
# LRU, LFU 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 more CPU. 3 is faster but not very accurate. maxmemory-samples 5
# 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
慢查询是一个固定长度,先进先出的队列,具有两个常用的配置参数
slowlog-log-slower-than
设定慢查询的执行时间,默认是10ms,负数表示不记录慢查询,0表示记录每一条指令
slowlog-max-len 128
设置慢查询存储的条数,默认是128
相关指令
指令
描述
SLOWLOG GET number
查看 slow log
SLOWLOG LEN
查看当前日志的数量
SLOWLOG RESET
清空日志
配置建议
slowlog-log-slower-than不要设置过大,默认10ms,通常设置为1ms
slowlog-max-len不要设置过小,通常设置1000左右
定期持久化慢查询
8.2 pipline
1次pipline=1次网络时间+n次命令时间
pipline的作用
命令
N个命令操作
1次pipline操作(n个命令)
时间
n次网络+n次命令
1次命令+n次指令
数据量
1次命令
n次命令
注意
redis的命令时间是微秒级别
pipline每次条数要控制(网络带宽)
代码演示说明
普通操作
1 2 3 4 5 6 7 8 9 10 11
publicstaticvoidmain(String[] args){ Jedis jedis = new Jedis("192.168.0.110", 6379);
long startTime = System.currentTimeMillis(); for(int i = 0; i < 10000; i++) { jedis.set("k" + i,i + ""); } long endTime = System.currentTimeMillis(); long usedTime = (endTime-startTime)/1000; System.out.println(usedTime + "s"); }
使用pipline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicstaticvoidmain(String[] args){ Jedis jedis = new Jedis("192.168.0.110", 6379);
long startTime = System.currentTimeMillis(); for(int i = 0; i < 100; i++) { Pipeline pipelined = jedis.pipelined(); for(int j = i * 100; j < (i + 1) * 100; j++) { pipelined.set("k" + i,i + ""); } pipelined.sync(); } long endTime = System.currentTimeMillis(); long usedTime = (endTime-startTime)/1000; System.out.println(usedTime + "s"); }
################################ 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 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
# 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. # # 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 usual 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 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. 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. # # The Append Only File will also be created inside this directory. # # Note that you must specify a directory here, not a file name. dir ./
############################## 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 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 appendfsync everysec # appendfsync no