当前位置:网站首页>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);
RESP3Use 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
pipeliningIt 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
DPDKTo 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
socketPut into the global wait read queue ; - The main thread will be readable by polling
socketAssigned to IO Threads ; - Main thread blocking waiting IO Thread reads
socketcomplete ; - 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
socketcomplete ; - 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 4Summary 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 .
边栏推荐
- Go language installation and uninstallation
- JS mobile phone and computer open different websites
- Vulnhub range the planes: mercury
- Print 1 cute every 100 milliseconds ~ with a running lantern effect
- Applet password input box
- PAT B1061
- Jsonp function encapsulation
- C language PTA -- continuity factor
- Applet Click to return to the top 2 methods
- Installing or uninstalling VMware could not find the network location
猜你喜欢

Google SEO external chain releases 50+ website platform sharing (e6zzseo)

Vulnhub range - darkhole 1

Profile path and name

Can GoogleSEO only do content without external chain? (e6zzseo)

Applet multi image to Base64 upload

2.14(Knight Moves)

PAT B1086

Vulnhub range the planes: mercury

Single chip microcomputer line selection method to store impression (address range) method + Example

Redis core article: the secret that can only be broken quickly
随机推荐
Is it safe for tongdaxin to open an account?
2.15(Multiple of 3 Or 5)
Mail monitoring cloud script execution progress
PAT B1091
Applet password input box
Case: count the most characters and times
2.1 write a program to calculate the sum and average of four integers.
1、 Hikaricp source code analysis of connection acquisition process I
Redis core article: the secret that can only be broken quickly
Embark on a new journey and reach the world with wisdom
Automatic fitting when the applet reaches the top
PHP little knowledge record
PAT B1057
Use of serialize() and serializearray() methods for form data serialization
Browser performance optimization (19)
VMware failed to prompt to lock this profile exclusively
Dependency injection in PHP reflection implementation framework
PHP FPM, workman, spoole, golang simple performance test
打新债网上开户安全吗,需要注意什么
Some pictures of real machine preview development and debugging are not shown