redis.conf简单介绍
redis启动的时候,就会通过配置文件来启动。
- units对大小写不敏感
# 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.
- 可以把其他配置文件导入进来(python 的 import)
################################## INCLUDES ###################################
# include /path/to/local.conf
# include /path/to/other.conf
网络
################################## NETWORK #####################################
bind 127.0.0.1
protected-mode yes
port 6379
通用
################################# GENERAL #####################################
# 以守护进程方式后台运行,默认是no,需要手动开启,不开启的话,一退出进程就结束了
daemonize yes
# 如果以后台的方式运行,我们就需要指定一个pid文件
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
# 日志的文件名,如果为空的话,就转化为标准输出
logfile ""
# 数据库的数量,默认是16个
databases 16
# 启动redis时显示的log,默认为yes
always-show-logo yes
快照
################################ SNAPSHOTTING ################################
save 900 1 # 如果900秒内至少1个key发生变化(新增、修改和删除),则重写rdb文件;
save 300 10 # 如果每300秒内至少10个key发生变化(新增、修改和删除),则重写rdb文件;
save 60 10000 # 如果每60秒内至少10000个key发生变化(新增、修改和删除),则重写rdb文件。
# >>>>> 我们之后学习持久化,会自己定义数据
# 持久化发生错误之后,是否还继续工作
stop-writes-on-bgsave-error yes
# 是否压缩rdb文件,需要消耗一些cpu资源
rdbcompression yes
# 保存rdb文件的时候,进行文件检查,校验和
rdbchecksum yes
# rdb文件保存目录
dir ./
持久化,在规定的时间内,执行了多少次操作,则会持久化到文件(.rdb或.aof文件)
redis是内存数据库,如果没有持久化,数据断电即失。
################################# REPLICATION #################################
# >>>>>>>>>>>>> 复制,后面学习主从复制的时候,再来了解
安全
################################## SECURITY ###################################
# requirepass foobared 需要密码
密码默认为空,可以通过命令行的方式进行设置
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) ""
127.0.0.1:6379> config set requirepass "123456" # 设置密码
OK
127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "123456"
127.0.0.1:6379> auth 123456 # 登录
OK
客户端限制
################################### CLIENTS ####################################
# Set the max number of connected clients at the same time.
# Once the limit is reached Redis will close all the new connections sending
# an error 'max number of clients reached'.
# IMPORTANT: When Redis Cluster is used, the max number of connections is also
# shared with the cluster bus: every node in the cluster will use two
# connections, one incoming and another outgoing. It is important to size the
# limit accordingly in case of very large clusters.
# maxclients 10000
内存
############################## MEMORY MANAGEMENT ################################
# Set a memory usage limit to the specified amount of bytes.
# 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).
# maxmemory <bytes>
# Note: with any of the above policies, Redis will return an error on write
# operations, when there are no suitable keys for eviction.
# The default is:
#
# maxmemory-policy noeviction
# 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
# replica-ignore-maxmemory yes
redis回收过期的keys有两种方式,一种是访问时发现这些keys已经过期,并且在后台被称为“活动的过期keys”。
第二种是缓慢的,交互式的扫描keys空间,以查找要回收的过期keys,以便可以释放已过期且短时间之内不会
再访问的keys。
# Redis reclaims expired keys in two ways: upon access when those keys are
# found to be expired, and also in background, in what is called the
# "active expire key". The key space is slowly and interactively scanned
# looking for expired keys to reclaim, so that it is possible to free memory
# of keys that are expired and will never be accessed again in a short time.
默认的过期周期的设置将避免超过10%的过期keys在内存中,并且将尽量避免消费超过总内存的25%,
以及避免增加系统延迟。
# The default effort of the expire cycle will try to avoid having more than
# ten percent of expired keys still in memory, and will try to avoid consuming
# more than 25% of total memory and to add latency to the system.
effort每增加1,就会增加10%,系统就会消耗更多的CPU资源,并会减少仍然存在于系统中的过期密钥的数量。
所以必须在内存,cpu和延迟之间做权衡。
# However it is possible to increase the expire "effort" that is normally set to
# "1", to a greater value, up to the value "10". At its maximum value the
# system will use more CPU, longer cycles (and technically may introduce
# more latency), and will tollerate less already expired keys still present
# in the system. It's a tradeoff betweeen memory, CPU and latecy.
# active-expire-effort 1
总结:
# maxmemory <bytes> # redis配置的最大内存容量
# maxmemory-policy noeviction # 内存达到上限之后的处理策略
LRU表示最近最少使用
# LRU means Least Recently Used
LFU表示最少使用
# LFU means Least Frequently Used
LRU,LFU和volatile-ttl均使用近似随机算法实现。
maxmemory-policy策略
策略 | 官方 | 白话 |
---|---|---|
volatile-lru | 在设置了过期时间的键中采用LRU算法删除键, 直到腾出足够内存为止。 |
只对设置了过期时间的 keys进行LRU |
allkeys-lru | 在所有键中采用LRU算法删除键, 直到腾出足够内存为止。 |
删除LRU算法的keys |
volatile-random | 在设置了过期时间的键中随机删除键, 直到腾出足够内存为止。 |
随机删除即将过期的keys |
allkeys-random | 在所有键中采用随机删除键, 直到腾出足够内存为止。 |
随机删除keys,无论是否过期 |
volatile-ttl | 在设置了过期时间的键空间中, 具有更早过期时间的key优先移除。 |
删除即将过期的keys |
noeviction | 当内存使用达到阈值的时候, 所有引起申请内存的命令会报错。 |
永不过期,返回错误 |
volatile-lfu | 在设置了过期时间的键中采用LFU算法删除键, 直到腾出足够内存为止。 |
只对设置了过期时间的 keys进行LFU |
allkeys-lfu | 在所有键中采用LFU算法删除键, 直到腾出足够内存为止。 |
删除LFU算法的keys |
AOF配置,默认是不开启的,默认使用RDB持久化方式。
############################## 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).
appendonly no
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.
# If unsure, use "everysec".
# appendfsync always # 每次修改都会修改fsync()
appendfsync everysec # 每秒执行一次 fsync()
# appendfsync no # 不执行fsync(),这时候操作系统自己同步数据,速度最快!