Redis 5单线程 vs 6多线程性能解析

发布于:2025-09-11 ⋅ 阅读:(16) ⋅ 点赞:(0)

Redis 5 单线程和Redis 6多线程得区别

redis 6 之前

首先我们要搞清楚 redis 6之前这个单线程是做了哪些东西 我们就明白其中得道理了自然就水到渠成了,redis 是单线程模型 既接受请求-命令解析-执行-得到结果之后再屁颠屁颠的相应给网络读写,这些事情都是由主线程来执行得。
其次我们要明白 redis得执行get set 时间单位为微妙级得,而网络io请求是毫秒级得,我们要搞清楚redis 是基于内存得 数据是存储在内存条中得而不是硬盘之中的,内存操作远远高于硬盘操作得,所以redis得性能问题不是在本身,而是在网络io这里了 因为网络io是毫秒级 redis get set 操作是微妙级 显然易见得事情 网络io成了 redis得性能得累赘了,道指成网络io拖慢了redis得主要原因也是redis本身得原因 因为redis 是单线程得 redis执行命令得时候只能占用一个cpu核心 完全发挥不出来cpu得全部性能。

redis 6起

而在redis 6起 中得多线程 不要瞎理解 不清楚就多查查资料 首先redis 中是多线程 解析命令-执行命令得是由主线程来执行得 而这些接受请求 和 得到结果后得屁颠屁颠得动作都交给了其他得线程来执行,这里说的其他线程是主线程创建并管理得子线程 来负责管理 接受网络io请求 解析命令 交给主线程执行命令 主线程执行完返回结果给子线程,子线程负责相应给网络io请求。

从字面上看来我们理解了 redis 多线程和单线程得区别 ,那么这个多线程解决了什么问题呢?
在单线程得上面得例子之中我已经说明了单线程什么都要主线程来做,拉长了简单并且重复得动作浪费了大量得时间 完全发挥不出来多核cpu得性能。

在多线程得时候 把这些简单并且重复浪费时间得动作交给子线程来执行 大大提高了 多核cpu使用率。
在原先单线程得时候只能单线程得在一个多核CPU中得某一个内核里面执行 发挥不出来多核cpu得性能 也速度很慢 ,而多线程则是主线程创建了n个子线程来进行执行接受请求 相应结果给网络io 这些子线程可能会执行在某个多核cpu中得某一个内核,当请求来的时候主线程从线程阻塞状态转换为就绪状态 若当前cpu执行算法给出了当前这个主线程了时间片 那么 这个主线程就从就绪状态转换为执行状态 当时间片得时间用完了就会转换为就绪状态 等待时间片得到来,当主线程得任务执行完成后返回结果后 会进入阻塞状态等待新的请求,而子线程就负责来帮主线程来执行这些重复并且浪费时间得动作

redis 6 是否默认开启了多线程

答案是否,是没有默认开启得,在这里的时候要主动去看 去下载redis6 去实际观察看看到底开启没有开启,因为很多人都是看的面试题 有些人认为默认是开启的 这是我从别的博客上看的 我肯定是不信得 我就下载看了看 发现是真的没有开启的
在这里插入图片描述

io-threads-do-reads no    
io-threads 4

这里的 io-threads-doreads no 是关闭得要开启的话要给上yes得值
而 io-threads 4 是要告诉redis要开启多少个线程 若 io-threads 1 则等同于禁用多线程(只用主线程) 在里面是包含一个主线程得哈

# Redis is mostly single threaded, however there are certain threaded
# operations such as UNLINK, slow I/O accesses and other things that are
# performed on side threads.
#
# Now it is also possible to handle Redis clients socket reads and writes
# in different I/O threads. Since especially writing is so slow, normally
# Redis users use pipelining in order to speed up the Redis performances per
# core, and spawn multiple instances in order to scale more. Using I/O
# threads it is possible to easily speedup two times Redis without resorting
# to pipelining nor sharding of the instance.
#
# 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.
#
# 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. In order to
# enable I/O threads use the following configuration directive:
#
# io-threads 4
#
# Setting io-threads to 1 will just use the main thread as usual.
# 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:
#
# io-threads-do-reads no

在这里插入图片描述

这里我们进行分析一下
redis 得配置文件里面已经说明了通过这种方式开启多线程就可以轻松得打倒redis运行速度得两倍,而且还不需要使用管道技术活实例分片,简单理解就是说开启多线程就能轻松得打倒两倍以上得性能。
这里也给我了进行得温馨提示 说 当前服务器有四个核心那就 尽量开启2-3个线程 保证留出其他核心给别的进程使用 不能完全让我们redis霸占。

此文章就到这里就结束了 至于怎么部署安装的话 可自行查阅其他博客。


网站公告

今日签到

点亮在社区的每一天
去签到