当前位置:网站首页>Why is redis so fast? Redis threading model and redis multithreading
Why is redis so fast? Redis threading model and redis multithreading
2022-07-27 09:46:00 【Java misty rain】

One 、Redis How fast ?
Redis It is a high-performance memory based operation K-V database , The official test report is that the single machine can support about 10w/s Of QPS

Two 、Redis Why so soon? ?
(1) Completely based on memory , Data is in memory , Most requests are purely memory operations , Very fast , Compared with traditional disk file data storage , Avoid passing through disk IO The cost of reading to memory .
(2) The data structure is simple , It's also easy to manipulate data .Redis The data structure in is specially designed , Each data structure has one or more data structures to support .Redis It depends on these flexible data structures , To improve the performance of reading and writing .
(3) Using single thread , It saves a lot of context switching time and CPU Consume , There is no competitive condition , You don't have to worry about locks , There is no lock release operation , There will be no performance consumption caused by deadlock .
(4) Using a IO Thread model of multiplexing mechanism , Can handle concurrent Links .
Redis be based on Reactor Pattern develops its own network event processor , This processor is called the file event handler file event handler. Since this file event handler is single threaded , therefore Redis It's called a single threaded model , But it uses IO Multiplexing mechanism listens to multiple channels at the same time Socket, And according to Socket Select the corresponding event handler to handle the event on . The structure of the file event handler contains 4 Parts of , The thread model is shown as follows :
Multiple Socket
IO Multiplexing program
File event dispatcher
Event handler ( Command request processor 、 Command reply processor 、 Connect to answer processor )


Multiple Socket Different operations may occur , Each operation corresponds to a different file event , however IO Multiplexers listen to multiple Socket, take Socket The generated events are queued , The event dispatcher takes an event from the queue one at a time , Give the event to the corresponding event handler for processing .
Redis Each call from the client to the server has gone through the sending command , Carry out orders , Three procedures to return the result . Among them, the command execution phase , because Redis Is a single thread to handle the command , Every command that arrives at the server will not be executed immediately , All commands go into a queue , And then it's executed one by one . And the execution order of commands sent by multiple clients is uncertain . But it's certain that no two commands will be executed at the same time , No concurrency issues , This is it. Redis The basic model of single thread .
multiple I/O The reuse model uses select、poll、epoll Can monitor multiple streams simultaneously I/O The power of events , In my spare time , Will block the current thread , When one or more streams have I/O When an event is , Just wake up from the blocking state , Then the program will poll all the streams (epoll Is to poll only those streams that actually emit the event ), And process the ready streams in turn , This avoids a lot of useless operations .
here “ multiple ” Refers to multiple network connections ,“ Reuse ” This refers to reusing the same thread . Using multiple channels I/O Reuse technology can enable a single thread to efficiently handle the network of multiple clients IO Connection request ( Minimize the network IO Time consumption of )
(5)Redis Build it yourself VM Mechanism , Avoid calling system functions , Waste time moving and requesting
3、 ... and 、 Why? Redis A single thread ?
Here we emphasize the single thread , It means that the network request module uses a thread to process , That is, a thread handles all network requests , Other modules still use multiple threads .
So why use single threads ? The official answer is : because CPU No Redis Bottleneck ,Redis The most likely bottleneck is machine memory or network bandwidth . Since a single thread is easy to implement , and CPU Not a bottleneck , So it's natural to adopt a single thread solution .
however , We can't play multi-core by using single thread CPU performance , But we can drive more than one in a single machine Redis Examples to solve this problem
Four 、Redis6.0 The multithreading ?
1、Redis6.0 Why didn't you use multithreading before ?
Redis The maintainability of using single thread is high . The multithreading model is excellent in some ways , But it introduces the uncertainty of program execution order , It brings a series of problems of concurrent read and write , Increased system complexity 、 At the same time, there may be thread switching 、 Even lock and unlock 、 Performance loss caused by deadlock .
2、Redis6.0 Why introduce multithreading ?
because Redis The bottleneck is not in memory , But on the Internet I/O Module bring CPU Time consuming , therefore Redis6.0 Multithreading is used to handle networks I/O This part , make the best of CPU resources , Reduce network I/O Performance loss due to blocking .
3、Redis6.0 How to turn on Multithreading ?
By default Redis Is to turn off multithreading , Can be in conf File for configuration :
io-threads-do-reads yes
io-threads Number of threads
## The official recommended thread count setting :4 The recommended setting for the core machine is 2 or 3 Threads ,8 The recommended setting for kernel is 6 Threads , The number of threads must be less than the number of machine cores , Try not to exceed 8 individual .
4、 In multithreading mode , Whether there is thread concurrency safety problem ?
Pictured , once redis request , To establish a connection , Then get the command of the operation , And then execute the command , Finally, write the result of the response to socket On .

stay redis In multithreading mode , obtain 、 Parse command , And the output results have two processes , Can be configured to be multithreaded , Because it is, after all, the main time-consuming point that we are positioning , But the execution of the command , That's memory operations , Still single threaded . therefore ,Redis The multithreading part is only used to process network data reading and writing and protocol parsing , The execution command is still executed in a single thread sequence , There is no concurrent security problem .
边栏推荐
- 刷题《剑指Offer》day04
- 系统架构之系统参数常量表:
- 习题 --- 快排、归并、浮点数二分
- What age are you still using date
- flash闪存使用和STM32CUBEMX安装教程【第三天】
- 通俗易懂!图解Go协程原理及实战
- Voice live broadcast system - Principles to be followed in developing push notifications
- S switch stacking scheme configuration guide
- ESP8266-Arduino编程实例-ADC
- July training (day 24) - segment tree
猜你喜欢

Nacos配置中心动态刷新数据源

Quick apply custom progress bar

Eureka delayed registration of a pit

Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset

吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)

都什么年代了你还在用 Date

深入浅出详解Knative云函数框架!

S交换机堆叠方案配置指南

Esp8266 Arduino programming example ADC

Nacos configuration center dynamically refreshes the data source
随机推荐
Flash memory usage and stm32subemx installation tutorial [day 3]
swagger-editor
省应急管理厅:广州可争取推广幼儿应急安全宣教经验
July training (day 13) - two way linked list
习题 --- 快排、归并、浮点数二分
圆环工件毛刺(凸起)缺口(凹陷)检测案例
XML概述
在Centos 7安装Mysql 5.7.27后无法启动?(语言-bash)
快应用JS自定义月相变化效果
Esp8266 Arduino programming example ADC
35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction
Nacos is used as a registration center
How to use tdengine sink connector?
为什么微服务一定要有API网关?
食品安全 | 菜板环境很重要,这些使用细节你知道吗?
Why do microservices have to have API gateways?
吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
ESP8266-Arduino编程实例-中断
July training (day 17) - breadth first search
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]