一,linux平台上redis6的安裝
請參見這一篇:
https://www.cnblogs.com/architectforest/p/12830056.html
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,使用redis6的io多線程的好處?
1,reddis6把多線程用在哪里?
redis運行的瓶頸,通常不在cpu,而在內存和網絡I/O
Redis 6 對多線程的啟用,主要用在處理網絡I/O,
流程就是:把監聽到的網絡的事件,分發給work thread做處理,
在處理完之后,由主線程負責執行。
說明:這是我們要注意的地方:
redis6對於命令的執行仍然是由主線程執行,
也就是象以前使用的原子性的命令如rpush/lua腳本仍然具有原子性,
不會因為多線程的引入也失效。
2,性能提升顯著:
Redis讀寫網絡的 read/write 系統調用在 執行期間占用了大部分 CPU 時間,
所以把網絡讀寫做成多線程的方式對性能會有很大提升,
根據測試,在 4個線程 IO 時,性能相比單線程提高一倍,
是redis6中的最可觀的性能提升
三,如何啟用redis6的io多線程
1,什么情況適宜啟用io多線程?
來自官方配置文件的說明:
默認情況多線程是disabled,當server有至少4個核心或更多時可以啟用,
至少留下一個備用的核心。
當設置為多於8個線程時,不會用明顯的性能提升
建議當確實遇到性能問題時而且redis的實例能占用cpu時間的一大部分時
再啟用threaded I/O,這樣會比較有效,
否則沒有啟用這個功能的必要。
原說明:
# By default threading is disabled, we suggest enabling it only in machines # that have at least 4 or more cores, leaving at least one spare core. # Using more than 8 threads is unlikely to help much. We also recommend using # threaded I/O only if you actually have performance problems, with Redis # instances being able to use a quite big percentage of CPU time, otherwise # there is no point in using this feature.
2,編輯redis的配置文件
[root@centos8 conf]# vi /usr/local/soft/redis6/conf/redis.conf
配置指令一
#io-threads: 啟用的io線程數量
io-threads 4
這個值設置為多少?
根據配置文件的說明:
如果你的server有4個核心,嘗試把這個值設置為3
如果有8個核心,嘗試把這個值設置為6
但這個值不建議超過8
附原說明:
# So for instance if you have a four cores boxes, try to use 2 or 3 I/O # threads, if you have a 8 cores, try to use 6 threads
配置指令二:
#讀請求也使用io線程
io-threads-do-reads yes
設置為yes即可
配置文件中的說明:
當I/O threads被啟用時,線程僅用於寫,
如果需要把讀數據和協議解析也啟用線程,
則需要把io-threads-do-reads也設置為yes
作者認為對讀請求啟用io-threads得到的幫助不算太多
原說明:
# When I/O threads are enabled, we only use threads for writes, that is # to thread the write(2) syscall and transfer the client buffers to the # socket. However it is also possible to enable threading of reads and # protocol parsing using the following configuration directive, by setting # it to yes: # Usually threading reads doesn't help much
四,多線程使用中需要注意的兩點
1,在redis運行時通過config set 來使用線程的配置指令不會生效,
當SSL啟用時,多線程也不會生效
原說明:
# NOTE 1: This configuration directive cannot be changed at runtime via # CONFIG SET. Aso this feature currently does not work when SSL is # enabled.
2,如果使用redis-benchmark測試redis的速度 ,
需要確認redis-benchmark是多線程模式,
使用 --threads選項來匹配redis的線程數量,
否則不會看到性能有明顯提升
原說明:
# NOTE 2: If you want to test the Redis speedup using redis-benchmark, make # sure you also run the benchmark itself in threaded mode, using the # --threads option to match the number of Redis theads, otherwise you'll not # be able to notice the improvements.
五,比較啟用線程前和啟用線程后的線程數量
啟用前:包括主線程:共5個線程
[root@centos8 conf]# pstree -p 3882 redis-server(3882)─┬─{redis-server}(3883) ├─{redis-server}(3884) ├─{redis-server}(3885) └─{redis-server}(3886)
我們把線程數量設置為4,重啟redis6的服務
啟用后:包括主線程:共8個線程
[root@centos8 conf]# pstree -p 3623 redis-server(3623)─┬─{redis-server}(3624) ├─{redis-server}(3625) ├─{redis-server}(3626) ├─{redis-server}(3627) ├─{redis-server}(3628) ├─{redis-server}(3629) └─{redis-server}(3630)
看來之前做網絡io處理的只有一個線程,
調整為4之后,增加了3個線程,共4個線程
六,查看redis的版本
[root@centos8 bin]# /usr/local/soft/redis6/bin/redis-server --version Redis server v=6.0.1 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=0
七,查看centos的版本
[root@centos8 bin]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)