当前位置:网站首页>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 :

  1. Multithreading networks IO;
  2. Client cache ;
  3. Fine grained permission control (ACL);
  4. RESP3 Use of protocol ;
  5. For replication RDB The file is no longer useful , Will be deleted immediately ;
  6. 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

 picture source : Back end Institute

Main thread and IO How does multithreading work together ?

Here's the picture :

Redis Multithreading and IO Threads

Main process

  1. The main thread is responsible for receiving the connection establishment request , obtain socket Put into the global wait read queue ;
  2. The main thread will be readable by polling socket Assigned to IO Threads ;
  3. Main thread blocking waiting IO Thread reads socket complete ;
  4. Main thread execution IO Read and parsed by thread Redis Request command ;
  5. Main thread blocking waiting IO The thread writes the result of instruction execution back to socket complete ;
  6. 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 .

原网站

版权声明
本文为[Code byte]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202190509429307.html