当前位置:网站首页>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
边栏推荐
- 微信小程序:万圣节头像框生成工具
- Problems in replacing RESNET convolution of mmdet with ghost convolution group
- golang7_TCP编程
- MySQL数据库:drop、truncate、delete的区别
- leetcode:226. Flip binary tree
- Thinkpad VMware 安装虚拟机出现此主机支持 Intel VT-x,但 Intel VT-x 处于禁用状态(问题解决方法)
- By proxy, by buyout, the wild era of domestic end-to-end travel is waiting for the next "eternal robbery"
- go-zero微服务实战系列(七、请求量这么高该如何优化)
- VeeamBackup&Replication简介
- Industry analysis - quick intercom, building intercom
猜你喜欢

win10安装Monggodb的基本使用教程

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

BYD has three years left

微信小程序:(更新)云开发微群人脉
![[document translation] camouflaged object detection](/img/30/73a927c05173a95cc5a5d51e182e3b.png)
[document translation] camouflaged object detection

I talked about exception handling for more than half an hour during the interview yesterday

Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout

Wechat applet: Halloween avatar box generation tool

"Dead" Nokia makes 150billion a year

疯狂的数字藏品,下一个造富神话?
随机推荐
【VEUX开发者工具的使用-getters使用】
测试用例设计方法之等价类划分方法
Game development of contract quantitative trading system (ready-made case analysis)
goby全端口扫描
Recruiting talents and seeking development | Jincang of the National People's Congress won the "best employer school recruitment case Award"
传输层 选择性确认 SACK
Installation and removal of cover for CPU protection on desktop motherboard
【黑马早报】中公教育市值蒸发逾2000亿;新东方直播粉丝破2000万;HM关闭中国首店;万科郁亮称房地产已触底;微信上线“大爆炸”功能...
【置顶】博客使用须知,公告板,留言板,关于博主
嵌入式开发:硬件在环测试
布隆过滤器Bloom Filter简介
Introduction to esp8266: three programming methods "suggestions collection"
微信小程序:全新独家云开发微群人脉
go-zero微服务实战系列(七、请求量这么高该如何优化)
动荡的中介生意,不安的租房人
Wechat applet: Halloween avatar box generation tool
数字IC手撕代码--交通灯
瑞达期货可以开户吗?安全可靠吗?
强大、优秀的文件管理软件评测:图片管理、书籍管理、文献管理
MySQL数据库:drop、truncate、delete的区别