Redis 内存
Redis 占用内存的大小
使用 redis-cli 登录到 Redis Server,执行 info
命令, 其中 '# Memory' 段落部分就是 Redis 内存的使用情况,例如:
# Memory
used_memory:3848224 //数据占用了多少内存(字节)
used_memory_human:3.67M //数据占用了多少内存(带单位的,可读性好)
used_memory_rss:10149888 //redis占用了多少内存
used_memory_rss_human:9.68M
used_memory_peak:24336080 //占用内存的峰值(字节)
used_memory_peak_human:23.21M
used_memory_peak_perc:15.81%
used_memory_overhead:2955778
used_memory_startup:512480
used_memory_dataset:892446
used_memory_dataset_perc:26.75%
allocator_allocated:3922952
allocator_active:8519680
allocator_resident:11620352
total_system_memory:16655781888
total_system_memory_human:15.51G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:8589934592
maxmemory_human:8.00G
maxmemory_policy:noeviction
allocator_frag_ratio:2.17
allocator_frag_bytes:4596728
allocator_rss_ratio:1.36
allocator_rss_bytes:3100672
rss_overhead_ratio:0.87
rss_overhead_bytes:18446744073708081152
mem_fragmentation_ratio:2.67
mem_fragmentation_bytes:6343656
mem_not_counted_for_evict:3780
mem_replication_backlog:0
mem_clients_slaves:0
mem_clients_normal:117342
mem_aof_buffer:3780
mem_allocator:jemalloc-5.1.0
active_defrag_running:0
lazyfree_pending_objects:0
Redis 最大占用内存的大小
这个在配置文件中:
############################## MEMORY MANAGEMENT ################################
# 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).
#
# 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).
#
# WARNING: If you have replicas attached to an instance with maxmemory on,
# the size of the output buffers needed to feed the replicas 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 replicas 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 replicas attached it is suggested that you set a lower
# limit for maxmemory so that there is some free RAM on the system for replica
# output buffers (but this is not needed if the policy is 'noeviction').
#
# maxmemory <bytes>
maxmemory 8589934592
Redis 支持运行时通过命令动态修改内存大小
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "8589934592"
127.0.0.1:6379> config set maxmemory 1024m
OK
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "1024000000"
127.0.0.1:6379>
如果不设置最大内存大小或者设置最大内存大小为 0,在 64 位操作系统下不限制内存大小,在 32 位操作系统下最多使用 3GB 内存。
Redis 的内存淘汰策略
既然可以设置 Redis 最大占用内存大小,那么配置的内存就有用完的时候。那在内存用完的时候,还继续往 Redis 里面添加数据不就没内存可用了吗?
实际上 Redis 定义了几种策略用来处理这种情况:
- noeviction(默认策略):对于写请求不再提供服务,直接返回错误(DEL请求和部分特殊请求除外)
- allkeys-lru:从所有key中使用LRU算法进行淘汰
- allkeys-random:从所有key中随机淘汰数据
- volatile-lru:从设置了过期时间的key中使用LRU算法进行淘汰
- volatile-random:从设置了过期时间的key中随机淘汰
- volatile-ttl:在设置了过期时间的key中,根据key的过期时间进行淘汰,越早过期的越优先被淘汰
当使用 volatile-lru、volatile-random、volatile-ttl 这三种策略时,如果没有 key 可以被淘汰,则和 noeviction 一样返回错误。
查看和设置 Redis 的内存淘汰策略
一种是写在配置文件中,redis.conf :
maxmemory-policy allkeys-lru
另一种是使用命令
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
127.0.0.1:6379> config set maxmemory-policy volatile-lru
OK
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "volatile-lru"
127.0.0.1:6379>
算法
LRU算法
上面说到了Redis可使用最大内存使用完了,是可以使用LRU算法进行内存淘汰的,那么什么是LRU算法呢?
LRU(Least Recently Used),即最近最少使用,是一种缓存置换算法。在使用内存作为缓存的时候,缓存的大小一般是固定的。当缓存被占满,这个时候继续往缓存里面添加数据,就需要淘汰一部分老的数据,释放内存空间用来存储新的数据。这个时候就可以使用LRU算法了。其核心思想是:如果一个数据在最近一段时间没有被用到,那么将来被使用到的可能性也很小,所以就可以被淘汰掉。
LRU 在 Redis 中的实现
近似LRU算法
Redis使用的是近似LRU算法,它跟常规的LRU算法还不太一样。近似LRU算法通过随机采样法淘汰数据,每次随机出5(默认)个key,从里面淘汰掉最近最少使用的key。
可以通过maxmemory-samples参数修改采样数量:例:maxmemory-samples 10 maxmenory-samples配置的越大,淘汰的结果越接近于严格的LRU算法
Redis为了实现近似LRU算法,给每个key增加了一个额外增加了一个24bit的字段,用来存储该key最后一次被访问的时间。
Redis3.0对近似LRU的优化
Redis3.0对近似LRU算法进行了一些优化。新算法会维护一个候选池(大小为16),池中的数据根据访问时间进行排序,第一次随机选取的key都会放入池中,随后每次随机选取的key只有在访问时间小于池中最小的时间才会放入池中,直到候选池被放满。当放满后,如果有新的key需要放入,则将池中最后访问时间最大(最近被访问)的移除。
当需要淘汰的时候,则直接从池中选取最近访问时间最小(最久没被访问)的key淘汰掉就行。
LFU算法
LFU 算法是Redis4.0里面新加的一种淘汰策略。它的全称是Least Frequently Used,它的核心思想是根据key的最近被访问的频率进行淘汰,很少被访问的优先被淘汰,被访问的多的则被留下来。
LFU算法能更好的表示一个key被访问的热度。假如你使用的是LRU算法,一个key很久没有被访问到,只刚刚是偶尔被访问了一次,那么它就被认为是热点数据,不会被淘汰,而有些key将来是很有可能被访问到的则被淘汰了。如果使用LFU算法则不会出现这种情况,因为使用一次并不会使一个key成为热点数据。
LFU一共有两种策略:
- volatile-lfu:在设置了过期时间的key中使用LFU算法淘汰key
- allkeys-lfu:在所有的key中使用LFU算法淘汰数据
设置使用这两种淘汰策略跟前面讲的一样,不过要注意的一点是这两周策略只能在Redis4.0及以上设置,如果在Redis4.0以下设置会报错。
参考文档: 面试官:Redis 内存数据满了,会宕机吗?