当前位置:网站首页>New features of redis 6.0: take you 100% to master the multithreading model
New features of redis 6.0: take you 100% to master the multithreading model
2022-06-25 19:58:00 【Code byte】
Redis The official in the 2020 year 5 Officially launched in 6.0 edition , Provides a lot of exciting new features , So much attention .
Old and wet , What features are provided ? See if I can get a raise ?
The main features are :
- Multithreading networks IO;
- Client cache ;
- Fine grained permission control (ACL);
RESP3
Use of protocol ;- For replication RDB The file is no longer useful , Will be deleted immediately ;
- RDB Files load faster ;
One of the concerns is 「 Multithreading model + Client cache 」, We have to master the new features and principles , To determine when to use 6.0 edition , How to use it better and faster , Don't step on a hole .
This article begins with Redis Multithreading model Start , As for client caching 、 Wait and listen .
Last , Click on the card below to follow 「 Code byte 」 Can I get a raise .
Old and wet ,Redis 6.0 Why not use multithreading before ?
Official reply :
Use Redis when , Almost nonexistent CPU A situation that becomes a bottleneck , Redis Mainly limited by memory and network .
In an ordinary Linux On the system ,Redis By using
pipelining
It can be processed per second 100 Million requests , So if the application mainly uses O(N) or O(log(N)) The order of , It hardly takes up too much CPU.After using single thread , High maintainability . 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 .
Redis adopt AE Event model and IO Multiplexing and other technologies , Processing performance is very high , So there's no need to use multithreading .
The single thread mechanism makes Redis The complexity of the internal implementation is greatly reduced ,Hash The inertia of Rehash、Lpush wait 『 Thread unsafe 』 All commands can be executed without lock .
stay 《Redis Why so soon? ?》 Code brother has a detailed introduction to the principle of fast .
Redis 6.0 Previously, single thread meant Redis There's only one thread working ?
no ,Redis When processing client requests , Including access to (socket read )、 analysis 、 perform 、 Content return (socket Write ) And so on are handled by a sequential serial main thread , That's what's called 「 Single thread 」.
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 will go into a Socket In line , When socket Readability is executed one by one by the single threaded event dispenser .
Besides , Some command operations can be performed by background thread or subprocess ( Like data deletion 、 Snapshot generation 、AOF rewrite ).
Old and wet , that Redis 6.0 Why introduce multithreading ?
With the improvement of hardware performance ,Redis The performance bottleneck may appear in the network IO Read and write , That is to say : The speed of single thread processing network read and write can't keep up with the speed of underlying network hardware .
Read and write on the Internet read/write
The system call takes up Redis Most of the execution period CPU Time , The bottleneck lies in the network IO Consume , Optimization has two main directions :
- Improve the Internet IO performance , Typical implementations, such as using
DPDK
To replace the kernel network stack . - Use multithreading to make the most of multicore , Improve the parallelism of network request reading and writing , Typical implementations are
Memcached
.
Add support for user mode network protocol stack , Need modification Redis Source and network related parts ( For example, modify all network transceiver request functions ), This leads to a lot of development work .
And new code may introduce new Bug, Causing system instability .
therefore ,Redis Adopt multiple IO Thread to handle network requests , Improve the parallelism of network request processing .
It should be noted that ,Redis many IO The thread model is only used to handle network read and write requests , about Redis Read and write commands for , Still single threaded .
This is because , Network processing is often the bottleneck , Performance can be improved by multithreading parallel processing .
And continue to use a single thread to execute read and write commands , There's no need to make sure that Lua Script 、 Business 、 And so on , Easier to implement .
The architecture is as follows :
Main thread and IO How does multithreading work together ?
Here's the picture :
Main process :
- The main thread is responsible for receiving the connection establishment request , obtain
socket
Put into the global wait read queue ; - The main thread will be readable by polling
socket
Assigned to IO Threads ; - Main thread blocking waiting IO Thread reads
socket
complete ; - Main thread execution IO Read and parsed by thread Redis Request command ;
- Main thread blocking waiting IO The thread writes the result of instruction execution back to
socket
complete ; - The main thread empties the global queue , Waiting for subsequent requests from the client .
Ideas : Set the main thread IO Read and write tasks are split up and processed by a group of independent threads , Make more than one socket Read and write can be parallelized , however Redis The command is still executed serially by the main thread .
How to turn on Multithreading ?
Redis 6.0 Multithreading is disabled by default , Use only the main thread . To open it, you need to modify redis.conf
The configuration file :io-threads-do-reads yes
.
Old and wet , The more threads, the better ?
Of course not. , Setting the number of threads , There's an official suggestion :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 .
The larger the number of threads, the better , Officials think it's more than 8 It doesn't make any sense .
in addition , When multithreading is turned on , You also need to set the number of threads , Otherwise it won't work .
io-threads 4
Summary and reflection
With the rapid development of Internet , The Internet business system has to deal with more and more online traffic ,Redis The single thread mode of will cause the system to consume a lot of CPU Time on the Internet I/O So as to reduce the throughput , To improve Redis There are two directions to the performance of :
- Optimize the network I/O modular
- Improve the reading and writing speed of machine memory
The latter depends on the development of hardware , There is no solution for the moment . So we have to start with the former , The Internet I/O The optimization can be divided into two directions :
- Zero copy technology or DPDK technology
- Take advantage of multi-core
Model defects
Redis The multithreaded network model of is actually not a standard Multi-Reactors/Master-Workers Model ,Redis In the multithreading scheme of ,I/O The thread task is just through socket Read the client request command and parse , But I didn't really carry out the orders .
Finally, all client commands need to go back to the main thread to execute , Therefore, the utilization rate of multi-core is not high , And every time the main thread must be busy polling after the task is assigned, waiting for all I/O After the thread completes the task, it can continue to execute other logic .
in my opinion ,Redis The current multithreading scheme is more like a compromise : It keeps the compatibility of the original system , And we can use multi-core to improve I/O performance .
边栏推荐
- Is it safe to open an online account for new bonds? What should be paid attention to
- 通过启牛学堂开的股票账户可以用吗?资金安全吗?
- Web container basic configuration
- Applet wx Request encapsulation
- Is it safe to open a new bond securities account
- Server journey from scratch - Yu Zhongxian integrated version (IP access server, LNMP compilation and installation, Lua environment and socket expansion)
- 2.2 step tariff
- JS get the parameters in the URL link
- DataX script task development record
- System optimization method
猜你喜欢
What should I pay attention to in GoogleSEO content station optimization?
200 OK (from memory cache) and 200 OK (from disk cache)
Wechat applet swiper simple local picture display appears large blank
Paddleocr learning (II) paddleocr detection model training
Single chip microcomputer line selection method to store impression (address range) method + Example
Trend ea- fixed stop loss and profit per order
Record Baidu search optimization thinking analysis
2.14(Knight Moves)
ECS 7-day practical training camp (Advanced route) -- day03 -- ecs+slb load balancing practice
Mail monitoring cloud script execution progress
随机推荐
PAT B1059
Uni app through uni Navigateto failed to pass parameter (pass object)
Web container basic configuration
Using flex to implement the Holy Grail layout is as simple as that
Is it safe for tongdaxin to open an account?
System optimization method
Single chip microcomputer line selection method to store impression (address range) method + Example
Vulnhub range the planes:earth
PAT B1086
2、 Hikaricp source code analysis of connection acquisition process II
Android Development Notes - Quick Start (from sqllite to room licentiousness) 2
From now on, I will blog my code
ActiveMQ--CVE-2016-3088
请问同花顺开户安全吗?
Suddenly found that the screen adjustment button can not be used and the brightness can not be adjusted
Yum command
Pdf file download (the download name is the same as the file name)
2.6 finding the sum of the first n terms of factorial sequence
Principles of MySQL clustered index and non clustered index
Jsonp function encapsulation