当前位置:网站首页>Multithreading in Redis 6
Multithreading in Redis 6
2022-08-03 13:14:00 【Cloud full of notes】
Fill in the title here
1. Multithreading for Redis 6
I was talking to my colleague about Redis yesterday. He asked me how I was doing with Redis. I said I was doing okay, and then he suddenly asked "Do you know that Redis now supports multithreading?", I was stunned for a while,Isn't Redis always single-threaded, why does it suddenly support multi-threading? I feel like a second, so hurry back and check the relevant information, or I won't dare to say that I know Redis in the future
1.1. Redis Knowledge Review
Let's review the relevant knowledge of Redis single-threaded mode first. We all know that the main reason why Redis is fast is because of the epool I/O multiplexing model. Let's briefly discuss:
- First of all, Redis runs in a single thread, and all operations are performed linearly in order, but since read and write operations are blocked waiting for user input or output, I/O operations are oftenIt cannot be returned directly, which will cause the I/O blocking of a file to cause the entire process to be unable to provide services to other clients, and I/O multiplexing appears to solve this problem.
- Multi-channel I/O multiplexing model is to use select, poll, epoll to monitor the I/O events of multiple streams at the same time. When idle, the current thread will be blocked. When there are one or moreWhen the stream has an I/O event, it wakes up from the blocking state, so the program polls all the streams (epoll is only polling those streams that actually emit events), and only processes the ready streams in sequence,This approach avoids a lot of useless operations.
- Here, "multiplexing" refers to multiple network connections, and "multiplexing" refers to multiplexing the same thread.The use of multi-channel I/O multiplexing technology allows a single thread to efficiently process multiple connection requests (minimize the time consumption of network IO), and Redis operates data in memory very fast, that is, in-memory operations are notIt will become a bottleneck affecting the performance of Redis, mainly due to the above points that make Redis have a high throughput.In fact, the interior is mainly implemented through two-way queues and red-black trees (the pictures are very detailed, and the knowledge you don’t understand can be found directly on Baidu):
1.2. Redis single thread
For a request operation, Redis mainly does 3 things: read data from the client, execute Redis commands, and write back the data to the client (if it is more accurate, it actually includes the analysis of the protocol).So the main thread actually executes these three things in series, because it is based on memory, so the execution speed is very fast:
Pros vs Cons:
- Advantages: a. No lock problem b. Avoid CPU switching between threads
- Disadvantages: a. Single thread cannot take advantage of multiple CPUb. Serial operation, a certain operation "problems" will "block" subsequent operations
1.3. Redis multithreading
Optimization of Redis multi-threading: Because network I/O occupies most of the CPU time during Redis execution, the network I/O part is extracted separately and made into a multi-threaded method.The multi-threading mentioned here is actually to process the two things done in a single thread of Redis, "reading data from the client and writing data back to the client" (also known as network I/O), and processing them into multi-threading, but the "execute Redis command" is still executed serially in the main thread, and this logic remains unchanged.
Some students may ask, if the main thread and multiple I/O threads process the "queue" in the figure at the same time, is there a relationship of lock competition? Here is a clever design, that is, when epoll acquiresWhen the socket is connected, all the events will be thrown into the queue first. For example, if N events are thrown, the main thread will be in a busy-waiting (spinlock effect) state.Then multiple I/O threads start to perform network I/O in parallel, and perform protocol analysis on the data. When all the queues are processed, the main thread will serially "execute Redis commands" for the requests in the queue, and then empty the queue.So the whole execution process is summarized: the main thread executes the request into the queue -> the I/O thread performs network reading in parallel -> the main thread executes Redis commands serially -> the I/O thread performs network writing in parallel -> the main thread clears the queue, andReceive the next batch of requests.
Pros vs Cons
- Advantages: a. Improve response speed, make full use of CPU
- Disadvantages: a. Increased code complexity
1.4. Summary
- Redis multiplexing technology, supports epoll, kqueue, selector;
- Version 5.0 and earlier, there is only one thread for processing client requests, serial processing;
- Version 6.0 introduces worker Thread, which only handles network IO reading and writing, and core IO is responsible for serially processing client instructions.
边栏推荐
猜你喜欢
Oracle is installed (system disk) and transferred from the system disk to the data disk
IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons
Image fusion GAN-FM study notes
HCIP第十五天笔记(企业网的三层架构、VLAN以及VLAN 的配置)
An工具介绍之骨骼工具
图像融合SDDGAN文章学习
Jmeter使用
leetcode16最接近的三数之和 (排序+ 双指针)
力扣刷题 每日两题(一)
An introduction to basic tools for selecting line tools (package church)
随机推荐
Golang 数组和切片
Oracle安装完毕(系统盘),从系统盘转移到数据盘
Yahoo! Answers-数据集
博客记录生活
PolarFormer: Multi-camera 3D Object Detection with Polar Transformers 论文笔记
如何让history历史记录前带时间戳
[微服务]多级缓存
Yahoo!Answers - data set
An动画基础之散件动画原理与形状提示点
超多精美礼品等你来拿!2022年中国混沌工程调查启动
有趣的opencv-记录图片二值化和相似度实现
An基本工具介绍之选择线条工具(包教会)
leetcode/字符串中的所有变位词(s1字符串的某个排列是s2的子串)的左索引
leetcode16 Sum of the closest three numbers (sort + double pointer)
可视化图表设计Cookbook
An工具介绍之3D工具
An动画优化之补间形状与传统补间的优化
An工具介绍之形状工具及渐变变形工具
利用pgsql插件PostGIS 实现地理坐标系数据转换
新评论接口——京东评论接口