Home> Database> Redis> body text

Why does redis use single thread? Why so fast?

青灯夜游
Release: 2021-11-01 10:08:02
forward
2035 people have browsed it

What is the single thread ofredis? Why does redis use single thread? Why is single-threaded Redis so fast? The following article will give you an analysis and I hope it will be helpful to you!

Why does redis use single thread? Why so fast?

1. Basic concept

  • What is the single thread of redis (the core function is on the single thread , not all functions)

    • Redis’s network IO and key-value pair reading and writing are completed by one thread (redis’ core service)

    • Other functions of redis are completed by additional threads

      • Persistence
      • Asynchronous deletion
      • Cluster data synchronization
##[Related recommendations:

Redis video tutorial]

2. Why does redis use single thread

  • Overhead of multi-threading

    • Using multi-threading can increase system throughput (per request) and increase system scalability

    • Unlimited increase in the number of threads leads to a decrease in throughput

        Shared resources are accessed by multiple threads at the same time, such as shared data structures
      • In order to ensure thread safety, resulting in performance sacrifice
      • Coarse-grained locks cause all serialization, and the system throughput rate increases with the increase of threads
  • Therefore, in order to save concurrent resources Management, redis uses a single thread to ensure that all operations are serialized

Why does redis use single thread? Why so fast?

Why does redis use single thread? Why so fast?

3. Single thread Why is redis fast?

    Most operations of redis are efficient data structures in memory
  • redis adopts a multiplexing mechanism to handle a large number of client requests in the network , to achieve high throughput rate

4.socket

    ##socket communication process (network IO processing key-value pair read and write network IO processing )
  • SimpleKV In order to process a Get request
    • needs to listen to the client request (bind/listen)
    • Establish a connection with the client (code) (accept)
    • Read the request from the socket (recv)
    • Parse the request sent by the client (parse)
    • Read the key value data according to the request type (get)
    • Finally return the result to the client, that is, write data back to the socket (send)
  • Potential blocking points
    • accept () When the connection fails to be successfully established, it will always block\
    • recv() When reading data from the client, it will always block\

Why does redis use single thread? Why so fast?

The non-blocking mode of socket
  • ensures that the Redis thread will neither wait at the blocking point like in the basic IO model nor It will not cause Redis to be unable to process the actual arriving connection request or data
  • IO multiplexing mechanism in Linux

Why does redis use single thread? Why so fast?

5 .Multiplexing

    The IO multiplexing mechanism in Linux refers to one thread processing multiple IO streams, select/poll
  • In a single thread, there are multiple sockets and connected sockets listening at the same time
  • The specific implementation
    • FD is Multiple sockets
    • Redis uses the epoll mechanism to allow the kernel to listen to sockets
    • Redis can connect and process multiple clients request, thereby improving concurrency
    • select/epoll provides an event-based callback mechanism, calling the corresponding processing function for different events
    • First, the event Put it into the event queue, without checking whether the request actually occurs, to avoid wasting CPU resources
      • Perform response operations according to the corresponding events

Why does redis use single thread? Why so fast?

6. Summary

    Is redis really single-threaded
  • on the network IO and data reading and writing operations use a thread.
  • Why use single thread
  • To avoid concurrency control problems in multi-threaded development.
    • The multiplexed IO model is closely related.
  • Why is single thread so fast
  • For more programming related knowledge, please visit:
Programming Video

! !

The above is the detailed content of Why does redis use single thread? Why so fast?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!