centos8平台:redis6配置启用io多线程(redis6.0.1)

一,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)
原文地址:https://www.cnblogs.com/architectforest/p/12837571.html