当前位置:网站首页>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.
边栏推荐
猜你喜欢
PyTorch构建神经网络预测气温(数据集对比,CPU与GPU对比)
超多精美礼品等你来拿!2022年中国混沌工程调查启动
[微服务]多级缓存
Yahoo!Answers - data set
From the physical level of the device to the circuit level
leetcode 11. The container that holds the most water
【实战技能】单片机bootloader的CANFD,I2C,SPI和串口方式更新APP视频教程(2022-08-01)
Jmeter use
The components of the basis of An animation movie clip animation between traditional filling
Image fusion DDcGAN study notes
随机推荐
海外代购系统/代购网站怎么搭建——源码解析
力扣刷题 每日两题(一)
Golang 通道 channel
基于php旅游网站管理系统获取(php毕业设计)
An动画基础之元件的图形动画与按钮动画
BOM系列之sessionStorage
leetcode/字符串中的所有变位词(s1字符串的某个排列是s2的子串)的左索引
How can I get a city's year-round weather data for free?Precipitation, temperature, humidity, solar radiation, etc.
8/2 训练日志(dp+思维+字典树)
Real number rounding and writing to file (C language file)
超多精美礼品等你来拿!2022年中国混沌工程调查启动
Oracle安装完毕(系统盘),从系统盘转移到数据盘
PyTorch框架训练线性回归模型(CPU与GPU环境)
PyTorch构建神经网络预测气温(数据集对比,CPU与GPU对比)
Golang 字典 map
图像融合SDDGAN文章学习
实数取整写入文件(C语言文件篇)
可视化图表设计Cookbook
PyTorch构建分类网络模型(Mnist数据集,全连接神经网络)
Tinymce plugins [Tinymce扩展插件集合]