作者:阿飛的博客;

鏈接:https://www.jianshu.com/p/fa989a228d59

作爲一統緩存江山的redis,正式發佈了5.0穩定版(stable),激不激動,開不開森?Github鏈接:https://github.com/antirez/redis/releases/tag/5.0.0 ,讓我們一起看看它帶來了哪些振奮人心的新特性:

10 分鐘不全面瞭解 Redis 5.0 新特性

redis5.0 stable

官方列舉特性如下:

======================================================
Redis 5.0.0 Released Wed Oct 17 13:28:26 CEST 2018
======================================================
what's new in Redis 5:
1. The new Stream data type. https://redis.io/topics/streams-intro
2. New Redis modules APIs: Timers, Cluster and Dictionary APIs.
3. RDB now store LFU and LRU information.
4. The cluster manager was ported from Ruby (redis-trib.rb) to C code
inside redis-cli. Check `redis-cli --cluster help` for more info.
5. New sorted set commands: ZPOPMIN/MAX and blocking variants.
6. Active defragmentation version 2.
7. Improvemenets in HyperLogLog implementations.
8. Better memory reporting capabilities.
9. Many commands with sub-commands now have an HELP subcommand.
10. Better performances when clients connect and disconnect often.
11. Many bug fixes and other random improvements.
12. Jemalloc was upgraded to version 5.1
13. CLIENT UNBLOCK and CLIENT ID.
14. The LOLWUT command was added. http://antirez.com/news/123
15. We no longer use the "slave" word if not for API backward compatibility.
16. Differnet optimizations in the networking layer.
17. Lua improvements:
- Better propagation of Lua scripts to replicas / AOF.
- Lua scripts can now timeout and get in -BUSY state in the replica as well.
18. Dynamic HZ to balance idle CPU usage with responsiveness.
19. The Redis core was refactored and improved in many ways.

Redis開源社區版新特性

下面我們對一些重要特性進行更深入的瞭解。

Streams

全新的數據類型:Streams。官方把這個當做是最重要的改進(The most important user facing improvement is without doubts the introduction

of the new general purpose data type after years: the streams.

)。而且官方提到在發版本的幾個小時前還在修復streams的問題,所以官方也不敢保證release版本是否有一些比較嚴重的BUG(T T),官方還是建議大家在生產環境不要使用streams,這樣可能會有更好的體驗,扎心了(People not using the streams can have probably a better production-ready

experience with Redis 5)。

筆者接下來還會單獨撰文剖析Streams,敬請關注。你可以把streams當做一個MQ,甚至把streams當做kafka都沒問題!

cluster manager with c

redis終於還是拋棄了ruby,維護過redis3.x和redis4.x的都知道,主要依賴redis-trib.rb腳本,redis5.0徹底拋棄了它,將集羣管理功能全部集成到完全用C寫的redis-cli中。可以通過命令redis-cli --cluster help查看幫助信息。

LOLWUT

redis5.0新增的一個命令,官方對它的描述是:a piece of art inside a database command,翻譯過來可以數據庫命令中的一件藝術品(老外真幽默)。

這個方法源碼註釋如下:

This file implements the LOLWUT command. The command should do something

fun and interesting, and should be replaced by a new implementation at each new version of Redis。源碼鏈接:https://github.com/antirez/redis/blob/5.0.0/src/lolwut5.c,有興趣的同學可以看看redis作者在5.0版本中實現了些什麼好玩的東西。

執行命令截圖如下,這個命令純屬好玩:

10 分鐘不全面瞭解 Redis 5.0 新特性

LOLWUT example

Lua改進

lua腳本能更好的傳播到replicas/AOF。replicas也就是以前的slave,大家都知道redis的slave事件前段時間鬧的沸沸揚揚,現在redis官方都改稱replicas了,並且申明除非爲了API向後兼容,否則不再使用slave這個詞(We no longer use the "slave" word if not for API backward compatibility.)。

lua腳本現在還能支持超時,並且可以在replica中進入BUSY狀態。

動態HZ

以前redis版本的hz都是固定的,5.0將hz動態化是爲了平衡空閒CPU的使用率和響應能力。當然這個是可配置的,只不過在5.0中默認是動態的,其對應的配置爲:dynamic-hz yes

ZPOPMIN&ZPOPMAX

用法:ZPOPMIN|ZPOPMAX key [count]

作用是移除並返回sorted set中count個最低|最高得分,count默認值爲1。

127.0.0.1:6379> ZADD exam 98 "a1" 90 "a2" 78 "a3" 60 "a4" 58 "a5" 50 "a6"
(integer) 6
127.0.0.1:6379> zrange exam 0 -1
1) "a6"
2) "a5"
3) "a4"
4) "a3"
5) "a2"
6) "a1"
# 移除兩個最低得分
127.0.0.1:6379> ZPOPMIN exam 2
1) "a6"
2) "50"
3) "a5"
4) "58"
# 移除一個最低低分(沒有指定COUNT參數,就是1)
127.0.0.1:6379> ZPOPMIN exam
1) "a4"
2) "60"
# 移除3個後,exam只剩下兩個元素
127.0.0.1:6379> zrange exam 0 -1
1) "a3"
2) "a2"
3) "a1"

其他一些比如HyperLogLog實現改進,RDB文件新增存儲LFU和LRU信息,Jemalloc升級到5.1等特性就不一一解讀了。

Redis Enterprise Software新特性

Redis開源許可從AGPL 遷移到將 Apache v2.0 與 Commons Clause 相結合的許可證,下面介紹的這些特性不再是開源軟件,而是源碼可用(source available)。

備註:Commons Clause 的初衷是爲了抵禦一些不良行爲。如那些使用源代碼,卻不對開源代碼維護做出補償的公司。” ---- Commons Clause 開發者兼 FOSSA 創始人 Kevin Wang。

support docker

現在官方版本正式支持在Docker容器上部署和運行Redis集羣了。有了官方的鏡像,你能非常容易並快速部署幾個容器來運行一個可擴展的,高可用的Redis集羣。想要了解更多請戳鏈接:https://redislabs.com/redis-enterprise-documentation/getting-started/docker/。

Redis Labs’ Modules

Redis Labs開發了3個模塊,並且被Redis企業軟件(簡稱RS)認證,這些模塊是:

RediSearch:這個模塊將RS變成了一個基於內存的分佈式全文索引和搜索引擎,感覺好牛逼的樣子(ES表示不服)。

ReJSON:通過模塊名稱就知道,JSON作爲Redis的內置數據類型。

ReBloom: RS具有可擴展的Bloom過濾器,並且是一個數據類型。Bloom過濾器是一種概率性的數據結構,在快速判斷某些元素是否被包含在一個集合這方便表現的非常好。

參考

https://redislabs.com/redis-enterprise-documentation/overview/new-features-redis-enterprise/

https://raw.githubusercontent.com/antirez/redis/5.0/00-RELEASENOTES

相关文章