Why single-threaded Redis was able to support high-concurrency

Redis high concurrency and fast reasons

  1. is redis memory, very fast read and write speeds of memory based;
  2. The core is based on the non-blocking IO multiplexing mechanism ;
  3. redis is single-threaded, but save a lot of thread context switching time;

Why is single-threaded Redis
  Redis is memory-based bottleneck operation, CPU is not the Redis, Redis bottlenecks are most likely to be a machine the size of memory or network bandwidth . Easy to implement single-threaded.

  Performance
  Performance redis-based memory, ordinary notebook easily handles hundreds of thousands of requests per second.

  Detailed reasons
  1) does not require the consumption of various performance lock
  data structures are not all Redis simple Key-Value, as well as list, hash and other complex structures which are likely to undergo a very fine-grained operations, such as in a very adding back a long list of elements, add or delete an object in the hash them. These operations may need to add a lot of locks, resulting in synchronization overhead is greatly increased.
In short, in the single-threaded case, we will not have to consider various issues lock, lock lock release operation does not exist, not because of a deadlock may occur and cause performance overhead.

  2) single-threaded multi-process program cluster
  single-threaded power is actually very powerful, very high efficiency per core, multi-threaded nature can have higher performance than single-threaded cap, but in today's computing environment, even if the stand-alone multi-threaded cap often can not meet the need for further exploration of multi-server clustered programs that are multithreaded technology is still unobtainable. So single-threaded, multi-process clusters would be a stylish solution.

  3) CPU consumption
  using a single-threaded, to avoid unnecessary context switches and competitive conditions, there is no multi-process or multi-threaded switching lead is consumed CPU.
  However, if the CPU becomes the bottleneck Redis, or do not want to let other server CUP nuclear idle, how to do?
  It can be considered more than a few Redis process, Redis is a key-value database, there is no constraint between is not a relational database, data. As long as the client distinguish which key on which Redis process it.

The advantages and disadvantages of single-threaded Redis
  1. advantage of single-threaded single-process
  code clearer, simpler processing logic
  do not have to consider various issues lock, lock lock release operation does not exist, not because of possible deadlocks caused by consumption performance
  is not the presence of multi-process or multi-threaded switching lead is consumed CPU
   2. drawbacks of single-threaded single-process
  CPU is not the bottleneck Redis, can not play multi-core CPU performance, but can open multiple instances of Redis single to perfect;


IO multiplexing
   redis IO network using multiplexing techniques to ensure that when multiple connections, high throughput systems.

  Multiple - refers to a plurality of socket connections, multiplexing - refers to a multiplex thread. There are three main multiplexing technology: the SELECT, poll, epoll . epoll is the latest multiplexing technology is currently the best.

Here "multiplexing" refers to multiple network connections, "multiplexing" refers to reuse the same thread. Multi-channel I / O multiplexing technique that allows efficient processing a plurality of single thread connection request (IO minimize the time consumed by the network), and the data in memory Redis operation is very fast (within a memory operation will not be here performance bottleneck) , the above two main Redis created with high throughput.

  I / O multiplexing model

  Blocking of I / O model can not meet the demand for high concurrency requires a more efficient I / O model to support a plurality of client Redis (redis-cli), involved here is the I / O multiplexer the model:

Why Redis single-threaded able to support high-concurrency?

In the I / O multiplexing model, the most important function call is SELECT , capable of simultaneously monitoring a plurality of readable and writable file descriptor of the process may be, when one or some of the readable writable file descriptor when, select method returns the number of file descriptors readable and writable.

Guess you like

Origin www.cnblogs.com/liuqing576598117/p/11126685.html