阿里云redis4.0引擎是以社区4.0为基础,合入大量阿里云开发的特性以及bugfix后全新推出的售卖版本。除了拥有redis-2.8引擎所具备的所有优势之外,还带来了很多新功能。
Lazyfree
redis-4.0带来的Lazyfree机制可以避免del,flushdb/flushall,rename等命令引起的redis-server阻塞,提高服务稳定性。
unlink
在redis-4.0之前,redis执行del命令会在释放掉key的所有内存以后才会返回OK,这在key比较大的时候(比如说一个hash里头有1000W条数据),其他连接可能要等待很久。为了兼容已有的del语义,redis-4.0引入unlink命令,效果以及用法和del完全一样,但内存释放动作放到后台线程中执行。
UNLINK key [key ...]
flushdb/flushall
flushdb/flushall在redis-4.0中新引入了选项,可以指定是否使用Lazyfree的方式来清空整个内存。
FLUSHALL [ASYNC]
FLUSHDB [ASYNC]
rename
执行 rename oldkey newkey 时,如果newkey已经存在,redis会先删除,这也会引发上面提到的删除大key问题,如果想让redis在这种场景下也使用lazyfree的方式来删除,可以在控制台上打开如下配置:
lazyfree-lazy-server-del yes/no
其他场景
某些用户对数据设置过期时间,依赖redis的淘汰机制去删除已经过期的数据,这同样也存在上面提到的问题,淘汰某个大key会导致进程CPU出现抖动,redis-4.0提供了两个配置,可以让redis在淘汰或者逐出数据时也使用lazyfree的方式。
lazyfree-lazy-eviction yes/no
lazyfree-lazy-expire yes/no
LFU&hotkey
redis-4.0新增了 allkey-lfu 和 volatile-lfu 两种数据逐出策略,同时还可以通过object命令来获取某个key的访问频度。
object freq user_key
基于LFU机制,用户可以使用 scan + object freq 来发现热点key,当然redis也一起发布了更好用的工具——redis-cli,使用实例如下所示。
$./redis-cli --hotkeys
# Scanning the entire keyspace to find hot keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
[00.00%] Hot key 'counter:000000000002' found so far with counter 87
[00.00%] Hot key 'key:000000000001' found so far with counter 254
[00.00%] Hot key 'mylist' found so far with counter 107
[00.00%] Hot key 'key:000000000000' found so far with counter 254
[45.45%] Hot key 'counter:000000000001' found so far with counter 87
[45.45%] Hot key 'key:000000000002' found so far with counter 254
[45.45%] Hot key 'myset' found so far with counter 64
[45.45%] Hot key 'counter:000000000000' found so far with counter 93
-------- summary -------
Sampled 22 keys in the keyspace!
hot key found with counter: 254 keyname: key:000000000001
hot key found with counter: 254 keyname: key:000000000000
hot key found with counter: 254 keyname: key:000000000002
hot key found with counter: 107 keyname: mylist
hot key found with counter: 93 keyname: counter:000000000000
hot key found with counter: 87 keyname: counter:000000000002
hot key found with counter: 87 keyname: counter:000000000001
hot key found with counter: 64 keyname: myset
其他
redis-4.0还有一些其他新的特性对用户来说是透明的,未来阿里云也会在4.0的基础上为用户提供更加丰富的功能,敬请期待!