当前位置:网站首页>Why is redis so fast? Is redis single threaded or multi-threaded?
Why is redis so fast? Is redis single threaded or multi-threaded?
2022-06-29 14:14:00 【Full stack programmer webmaster】
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 Internally used file event handler file event handler, This file event handler is single threaded , therefore Redis It's called a single threaded model , He USES IO Multiplexing mechanism listens to multiple channels at the same time Socket, according to Socket Select the corresponding event handler to handle the event on . The structure of the file event handler contains 4 Parts of :
- Multiple Socket
- IO Multiplexing program
- File event dispatcher
- Event handler ( Connect to answer processor 、 Command request processor 、 Command reply processor )
Multiple Socket Different operations may occur , Each operation corresponds to a different file event , however IO Multiplexers listen to multiple Socket, Will 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 .
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) Using the underlying model is different , The underlying implementation and application protocols for communicating with clients are different between them ,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 .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/100025.html Link to the original text :https://javaforall.cn
边栏推荐
- 直觉与实现:Batch Normalization
- Tiktok's global short video dominance may be reversed by YouTube
- mysql多表查询
- numpy数组创建
- 28000 word summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little floating (Part 2)
- 昨天面试居然聊了半个多小时的异常处理
- Redis的事务机制
- Seekg() [easy to understand]
- 人不成熟的特征
- “死掉”的诺基亚,一年躺赚1500亿
猜你喜欢

go-zero微服务实战系列(七、请求量这么高该如何优化)

微信小程序:全新獨家雲開發微群人脈
一次mysql的.ibd文件过大处理过程记录

Intuition and Implementation: batch normalization

VQA不只需要图片,还需要外部知识!华盛顿大学&微软提出提出REVIVE,用GPT-3和Wikidata来辅助回答问题!...

Stable currency risk profile: are usdt and usdc safe?

吐血整理:一份不可多得的架构师图谱!

现场快递柜状态采集与控制系统

ANSVC无功补偿装置在河北某购物广场中的应用

Dynamics 365Online Lookup查找字段多选
随机推荐
Redis的事务机制
"Dead" Nokia makes 150billion a year
Seekg() [easy to understand]
用手机在指南针上开户靠谱吗?这样炒股有没有什么安全隐患
BYD has three years left
布隆过滤器Bloom Filter简介
I talked about exception handling for more than half an hour during the interview yesterday
ANSVC无功补偿装置在河北某购物广场中的应用
Introduction to esp8266: three programming methods "suggestions collection"
【blackduck】jenkins下配置指定的synopsys-detect扫描版本
Wechat applet: new and exclusive cloud development wechat group contacts
内网穿透(nc)
嵌入式开发:硬件在环测试
Matlab fmincon precision, fmincon and quadprog error
一次mysql的.ibd文件过大处理过程记录
Wechat applet: Yunkai publishes white wall wechat applet source code download server free and domain name support traffic main revenue
Introduction to veeambackup & replication
28000 word summary of callable and future interview knowledge points. After reading it, I went directly to ByteDance. Forgive me for being a little floating (Part 2)
【黑马早报】中公教育市值蒸发逾2000亿;新东方直播粉丝破2000万;HM关闭中国首店;万科郁亮称房地产已触底;微信上线“大爆炸”功能...
Uncover the practice of Baidu intelligent test in the field of automatic test execution