Info指令、Redis过期key 删除策略和内存淘汰策略

1. info指令

  info指令显示的信息繁多,分为9大块。9大块如下:

1. server    服务器运行的环境参数

2. clients    客户端相关信息

3. memory    服务器运行内存统计数据

4.persistence    持久化信息

5.stats    通用统计数据

6.replication    主从复制想信息

7.cpu    cpu使用情况

8.cluster    集群信息

9.keyspace    键值对统计数量信息

  info 可以一次性获取所有的信息,也可以按块获取信息。

C:Usersliqiang>redis-cli
127.0.0.1:6379> info
# Server
redis_version:3.2.100
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:dd26f1f93c5130ee
redis_mode:standalone
os:Windows
arch_bits:64
multiplexing_api:WinSock_IOCP
process_id:1636
run_id:6cfcd258a26ce2e4d592dc10cc85f2b993a258b3
。。。

    按模块获取信息:

127.0.0.1:6379> info memory
# Memory
used_memory:707488
used_memory_human:690.91K
used_memory_rss:670560
used_memory_rss_human:654.84K
used_memory_peak:784512
used_memory_peak_human:766.13K
total_system_memory:0
total_system_memory_human:0B
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:0.95
mem_allocator:jemalloc-3.6.0
127.0.0.1:6379> info server
# Server
redis_version:3.2.100
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:dd26f1f93c5130ee
redis_mode:standalone
os:Windows
arch_bits:64
multiplexing_api:WinSock_IOCP
process_id:1636
run_id:6cfcd258a26ce2e4d592dc10cc85f2b993a258b3
tcp_port:6379
uptime_in_seconds:1214459
uptime_in_days:14
hz:10
lru_clock:782074
executable:E:Redis"E:Redis
edis-server.exe"
config_file:E:Redis
edis.windows.conf

  查看客户端连接数量以及客户端连接地址:

127.0.0.1:6379> info clients
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
127.0.0.1:6379> client list
id=3 addr=127.0.0.1:53504 fd=8 name= age=507 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=18446744073709537584 events=r cmd=client

2. 过期策略

  Redis 所有的数据结构都可以设置过期时间,时间一到,就会被自动删除。

1. 过期的key集合

  Redis会将每个设置了过期时间的key放入一个独立的字典中,以后会定时遍历每个字段来删除到期的key。除了定时遍历之外,它还会使用惰性策略来删除过期的key。所谓惰性策略就是在客户端访问这个key的时候,Redis 对key 的过期时间进行检查,如果过期了就立即删除。如果说定时删除是集中处理,那么惰性删除就是零散处理。

2. 定时扫描策略

Redis 默认每秒进行10次过期扫描,过期扫描不会遍历过期字典中所有的key,而是采用了一种简单的谈心除了:

(1)从过期字典中随机选出20个key

(2)删除这20个key中已经过期的key

(3)如果过期的key的比例超过1/4,那就重复步骤(1)

  为了保证过期扫描不会出现循环过度,导致线程卡死的线性,算法还增加了扫描时间的上限,默认不会超过25 ms。

  假设一个大型的redis实例中所有的key在同一时间过期了,会出现怎样的结果?redis 会持续扫描过期字段(循环多次),直到过期字典中过期的key 变得稀疏,才会停止。这会导致线上读写请求出现明显的卡顿现在。导致这种卡顿的另外一种原因是内存管理器需要频繁回收内存也,这也会产生一定的CPU消耗。

  当客户端请求到来时,服务器如果正好进入过期扫描状态,客户端的请求会等待至少25 ms 后才会进行处理,如果客户端的超时事件设置的比较短,比如10 ms,那么就会有大量的连接因为超时而关闭,业务端就会出现很多异常,而且无法从redis的slowlog 中看到慢查询记录,因为慢查询指的是逻辑处理过程慢,不包含等待时间。

3. 从节点的过期策略

  从节点不会过期扫描,从节点对过期的处理是被动的。主节点在key 到期后,会在AOF文件增加一条del 指令,同步到所有的从节点,从节点通过执行del 指令来删除过期的key。

3. 内存淘汰策略-LRU

  当redis 内存超出物理内存限制时,内存的数据会开始和磁盘产生频繁的交换。交换会让redis 的性能急剧下降,对于访问量比较大的redis 来说,这样的存取率基本上等于不可用。

  在生产环境我们不允许出现交换,为了限制最大使用内存,redis 提供了配置参数 maxmemory 来限制内存超出期望大小。

  当实际内存超出maxmemory 时,redis 提供了几种可选策略来让用户自己决定该如何腾出新的空间以继续提供读写服务。

1. noeviction: 不接收写请求(del 可以继续请求), 读请求可以继续进行。这样保证不会丢失数据,但是会让线上的业务不能持续进行。这是默认的淘汰策略。

2.volatile-lru:尝试淘汰设置了过期时间的key,最少使用的key优先被淘汰。没有设置过期时间的key不会被淘汰,这样可以保证需要持久化的数据不会突然丢失。

3.volatile-ttl:跟上面几乎一样,不过淘汰的策略不是LRU,而是比较key的剩余寿命ttl的值,ttl 越小越优先被淘汰。

4.volatile-random:跟上面一样,不过淘汰的key是过期key集合中随机的key。

5.allkeys-lru:区别于volatile-lru,这个策略要淘汰的key 对象是全体的key 集合,而不是过期的key集合。这意味着一些没有设置过期时间的key 也会被淘汰。

6.allkeys-random:同5几乎一样,只是淘汰的key 是随机的key。

  volatile-xxx 策略只会针对带过期时间的key 进行淘汰,allkeys-xxx 策略会对所有的key进行淘汰。如果只是拿redis做缓存,那么应该使用allkeys-xxx 策略,客户端写缓存时不必携带过期时间。如果还想同时使用redis的持久化功能,那就使用volatile-xxx策略,这样可以保留没有设置过期时间的key,它们是永久的key,不会被LRU算法淘汰。

测试如下:

(1) 设置以及查看最大内存

127.0.0.1:6379> config set maxmemory 100mb
OK
127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "104857600"

(2) 查看内存淘汰策略

127.0.0.1:6379> config get maxmemory
1) "maxmemory"
2) "104857600"
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
127.0.0.1:6379> config set maxmemory-policy allkeys-lru
OK
127.0.0.1:6379> config get maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"

(3) 也可以修改redis.conf 文件进行修改,查看redis.conf 文件默认的相关配置如下:

# 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 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.
#
...skipping...
# maxmemory-policy noeviction

补充:redis 4.0 提供了八种淘汰策略,查看配置文件redis.conf 文件如下

# 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.

  增加了两种lfu进行淘汰,也就是优先淘汰使用频率最小的数据。

【当你用心写完每一篇博客之后,你会发现它比你用代码实现功能更有成就感!】
原文地址:https://www.cnblogs.com/qlqwjy/p/15106384.html