We often hear that Redis It's single threaded , There will be questions : Why single threaded Redis That fast ?

Here's something to understand :Redis A single thread , Mainly refers to Redis Network of IO And key value pairs are read and written by one thread , This is also Redis The main process of providing external key value storage service . but Redis Other functions of , Like persistence 、 Delete asynchronously 、 Cluster data synchronization, etc , It's all done by extra threads .

We know that multithreading can improve concurrent performance , What then? Redis Will use single thread , Instead of multithreading ? Why is single thread so fast ?

Let's learn about it Redis The reason for single threading .

Why single threaded ?

Using multithreading , Although it can increase system throughput , Or increase system scalability , But there are also costs .

Redis The data is in memory , Is Shared , If multithreading is used, there will be competition for shared resources , We need to introduce mutex to solve , Make parallel to serial . In the end, the system throughput does not increase with the number of threads .

in addition , Multithreading development requires fine design , It will increase the complexity of the system , Reduce the ease of debugging and maintainability of the code . To avoid these problems ,Redis Single threaded mode .

Single thread Redis Why so fast ?

Generally speaking , The processing power of single thread is much worse than that of multithreading , that Redis But it can use the single thread model to achieve hundreds of thousands of processing power per second , Why is that ?

One side ,Redis Most operations are done in memory , And use efficient data structure , For example, hash tables and jump tables . On the other hand ,Redis Adopted Multiplexing mechanism , Make it on the Internet IO A large number of client requests can be processed simultaneously in the operation , Achieve high throughput .

Before learning multiplexing , We need to understand that network operations are based on IO Models and potential blockpoints .

basic IO Models and choke points

With Get Request as an example , In order to deal with a Get request :

  1. Need to listen for client requests (bind/listen)
  2. Establish a connection with the client (accept)
  3. from socket Read request in (recv)
  4. Parse client send request (parse)
  5. Read the key value data according to the request type (get)
  6. Finally, the result is returned to the client , That is to socket Write back the data in (send).

The figure below shows the process , among ,bind/listen、accept、recv、parse and send It belongs to the Internet IO Handle , and get Attribute key value data operation .

But the network here IO In operation , There's a potential choke point , Namely accept() and recv().

  • When Redis Listening to a connection request from a client , But when the connection has not been successfully established , Will be blocked. accept()
  • When Redis adopt recv() When reading data from a client , If the data has not arrived ,Redis It's going to be blocked all the time recv()

This leads to Redis The whole thread is blocked , Unable to process other client requests , Efficiency is very low . however , Fortunately, ,socket The network model itself supports non blocking mode .

Non-blocking mode

Socket The network model can be set in non blocking mode .

This will guarantee Redis Threads are not like basic IO The model has been waiting at the blocking point , It will not lead to Redis Unable to process actual incoming connection requests or data .

Here's the multiplexing mechanism .

Multiplexing based high performance I/O Model

Linux Of IO Multiplexing mechanism refers to the processing of multiple by one thread IO flow , That is to say select/epoll Mechanism .

stay Redis Running single thread , This mechanism allows the kernel to , There are multiple listening sockets and connected sockets at the same time .

In order to inform when the request arrives Redis Threads ,select/epoll Provides Event based callback mechanism , namely For the occurrence of different events , Call the corresponding processing function .

The workflow of callback mechanism :

  1. select/epoll Once I hear that FD There is a request to arrive on , The corresponding event will be triggered , And put it in an event queue .
  2. Redis Single thread can process the event queue , There's no need to keep polling for requests , avoid CPU Waste of resources .

because Redis The event queue is being processed all the time , So it can respond to client requests in time , promote Redis Response performance of .

however , It should be noted that , On different operating systems , The multiplexing mechanism is also applicable .

expand

stay “Redis basic IO Model ” In the figure , What are the potential performance bottlenecks ?

Redis Single threaded processing IO Request performance bottlenecks mainly include 2 In terms of :

1、 Any request in server Once it happens, it takes time , Will affect the whole server Performance of In other words, the subsequent requests have to wait for the previous time-consuming request to be processed , To be dealt with by oneself .

Time consuming operations include :

  • operation bigkey: Write a bigkey It takes more time to allocate memory , Again , Delete bigkey Freeing memory can also take time
  • Using overly complex commands : for example SORT/SUNION/ZUNIONSTORE, perhaps O(N) command , however N It's big , for example lrange key 0 -1 Query the full amount of data at a time
  • A lot of key Concentration expired :Redis The expiration mechanism is also executed in the main thread , A lot of key The expiration of a set can result in the processing of a request , Time consuming to delete expired key, It takes longer
  • Elimination strategy : The stroll strategy is also executed in the main thread , When memory exceeds Redis After the memory limit , Each write needs to be eliminated key, It will be It takes longer .
  • AOF The brush plate is on always Mechanism : Every time you write, you need to brush this operation to disk , Writing to disk is much slower than writing to memory , Will slow down Redis Performance of
  • Master slave total synchronous generation RDB: Although fork Child processes generate data snapshots , but fork This moment will also block the whole thread , The larger the instance , The longer the blocking time

terms of settlement :

  • Business people are needed to avoid
  • Redis stay 4.0 Launched lazy-free Mechanism , hold bigkey Time consuming operations to free memory are executed in asynchronous threads , Reduce the impact on the main thread

2、 When the amount of concurrency is very large , Single thread read-write client IO Data has performance bottlenecks , Although IO Multiplexing mechanism , But read and write client data is still synchronized IO, Only a single thread can read the client data in turn , Can't make use of CPU Multicore .

terms of settlement :

  • Redis stay 6.0 Multithreading has been introduced , It can be used in high concurrency scenarios CPU Multi core multi thread read and write client data , Further improve server performance
  • Of course , The client side only reads and writes , The real operation of each command is still single threaded

Reference material

Redis The basic chapter ( Two ) High performance IO More articles on the model

  1. High performance IO Model analyses

    High performance IO Model analyses Server side programming often needs to construct high-performance IO Model , common IO There are four models : (1) Synchronous blocking IO(Blocking IO): Traditional IO Model . (2) Synchronous nonblocking IO(Non-blocking  ...

  2. High performance IO Model analyses ( Color pictures explain )good

    Server side programming often needs to construct high-performance IO Model , common IO There are four models : (1) Synchronous blocking IO(Blocking IO): Traditional IO Model . (2) Synchronous nonblocking IO(Non-blocking IO): Created by default s ...

  3. High performance IO Model analyses ( turn )

    from :http://www.cnblogs.com/fanzhidongyzby/p/4098546.html It's the explanation I've seen so far IO The clearest model of the article , Of course! , If you want to know more about it, you should continue to read the blue book ...

  4. 【 collection 】 High performance IO Model analyses

    Server side programming often needs to construct high-performance IO Model , common IO There are four models : (1) Synchronous blocking IO(Blocking IO): Traditional IO Model . (2) Synchronous nonblocking IO(Non-blocking IO): Created by default s ...

  5. [ Reprint ] High performance IO Model analyses

    Reprinted from http://www.cnblogs.com/fanzhidongyzby/p/4098546.html Server side programming often needs to construct high-performance IO Model , common IO There are four models : (1) Synchronous blocking IO(B ...

  6. turn High performance IO Model analyses

    High performance IO Model analyses from :http://www.cnblogs.com/fanzhidongyzby/p/4098546.html Server side programming often needs to construct high-performance IO Model , common IO There are four models : ( ...

  7. 【 Reprint 】 High performance IO Model analyses

    Server side programming often needs to construct high-performance IO Model , common IO There are four models : (1) Synchronous blocking IO(Blocking IO): Traditional IO Model . (2) Synchronous nonblocking IO(Non-blocking IO): Created by default s ...

  8. php The basic chapter - Two dimensional array sorting array_multisort

    original text :php The basic chapter - Two dimensional array sorting array_multisort Yes 2 Dimensional array or multidimensional array sorting is a common problem , stay php We have a special multidimensional array sorting function in , Here is a brief introduction : array_multisort(a ...

  9. docker+k8s Based on two

    Docker+K8s The basic chapter ( Two ) docker Resource control A:docker Resource constraints Kubernetes The foundation of A:DevOps Introduction to B:Kubernetes An overview of the architecture C:Kubernetes ...

  10. NIO Related basic part II

    Reprint please indicate the original source , thank you ! Part 1 NIO Related basic article 1 , It mainly introduces some basic concepts and buffers (Buffer) And channel (Channel), This article continues NIO Related topics , Mainly file lock . And more importantly Selecto ...

Random recommendation

  1. New Features In SNMPv3 - SNMP Tutorial

    30.12 New Features In SNMPv3 We said that version 3 of SNMP represents an evolution that follows and ...

  2. Google Detailed explanation of the latest screen capture case

    Google from Android 5.0 Start , A screen capture example is given ScreenCapture, In the same version of examples Of Media You can find . It provides a very meaningful reference for the small partners who need to develop mobile phone or tablet screen capture applications ...

  3. 【HDOJ】3696 Farm Game

    SPFA Find the shortest path . When you see a picture, pay attention to the reverse construction . /* 3696 */ #include <iostream> #include <queue> #include <vect ...

  4. how : Control the namespace prefix (C#) (LINQ to XML)

    Visual Studio 2010 This topic describes the serialization of XML How to control the namespace prefix when creating a tree . In many cases , There is no need to control the namespace prefix . however , some XML Programming tools need specific control of the namespace prefix .  for example , you ...

  5. Use IntelliJ IDEA Happy programming (1)

    Preface I love to talk to people about things , occasionally , In the company , To discuss such issues requires demonstration code . One of the most common situations is ( According to my memory, it has surpassed at least 10 Time ), People will interrupt your presentation , Throw a question : wait , What you just did is ...

  6. jquery Of $(selector).each(function(index,element)) and $.each(dataresource,function(index,element)) The difference between

    $(selector).each(function(index,element)) Definition and Usage each() Method specifies the function to run for each matching element . $(selector).each(function ...

  7. object-oriented 【day07】: Attributes of a class ( 5、 ... and )

    Content of this section summary Public attribute One . summary We talked about the private properties of a class , Now let's talk about the public properties of a class , It's easy to get confused here , Some people think that , stay __init__() In the construction method , Except for private properties , Everything else is public , Actually, this is ...

  8. git Use the pits you encounter

    Submit a complete project to github The last steps and precautions Git The installation of . First step :mkdir/cd We need to create a local version library first ( It's actually a folder ). You can right-click the new folder , You can also right-click to open ...

  9. Mac OSX Pass under the system ProxyChains-NG Realize the agent under the terminal

    Project home page :https://github.com/rofl0r/proxychains-ng Official statement : proxychains ng (new generation) - a preloader wh ...

  10. FPM Package customization completed ( Waiting for implementation Milepost 1 and 2) 2018 year 4 month 13 Japan 2:18:32

    Early environmental preparation : close SELINUX  :   setenforce 0 close SELINUX  :   sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' / ...