High concurrency and fast reasons for Redis

1. Redis is based on memory, which has very fast read and write speed;

2. Redis is single-threaded, saving a lot of time for context switching threads;

3. Redis uses multiplexing technology to handle concurrent connections. The internal implementation of non-blocking IO uses epoll, which uses a simple event framework implemented by EPoll + itself. Read, write, close, connect in ePoll are converted into events, and then take advantage of ePoll’s multiplexing features without wasting any time on IO.

Why is Redis single threaded

1. Official answer

Since Redis is a memory-based operation, CPU is not the bottleneck of Redis. The bottleneck of Redis is most likely the size of the machine’s memory or network bandwidth. Since single-threading is easy to implement and the CPU is not a bottleneck, it makes sense to adopt a single-threaded solution.

2. Performance indicators

About redis performance, the official website also has, ordinary notebook easily handle hundreds of thousands of requests per second.

3. Detailed reasons

1) The performance cost of various locks is not required

Redis data structures are not all simple key-values, but also complex structures such as lists and hash, which may carry out very fine-grained operations, such as adding an element after a long list and adding or deleting elements from the hash

An object. These operations may require very many locks, resulting in a significant increase in synchronization overhead.

In short, in the case of a single thread, there is no need to worry about various locks, there is no lock release operation, and there is no performance cost due to possible deadlocks.

2) Single-thread multi-process cluster solution

Single thread is actually very powerful, each core efficiency is very high also, multi-threaded nature is to be able to have higher performance than single thread limit, but in today’s computing environment, even a single multithreaded ceiling also often cannot meet the need, need to be further grope is more server clustering scheme, these schemes can’t be used for a multithreading technology is still in.

So a single-threaded, multi-process cluster is a trendy solution.

3) CPU consumption

Using single thread, avoid unnecessary context switch and competition conditions, there is no multi-process or multi-thread caused by the switch and CPU consumption.

But what if the CPU becomes a Redis bottleneck, or you don’t want to idle other CUP cores on your server?

Multiple Redis processes can be considered. Redis is a key-value database, not a relational database, and there is no constraint between data. As long as the client knows which key is on which Redis process.

Advantages and disadvantages of Redis single thread

1. Single-process single-thread advantage

  • The code is clearer and the processing logic is simpler
  • There is no need to worry about various locks, there is no lock release operation, and there is no performance penalty due to possible deadlocks
  • There are no cpu-consuming switches caused by multiple processes or threads

2. Single process single thread disadvantages

  • It can’t use multi-core CPU performance, but can be improved by opening multiple Instances of Redis on a single machine.

IO multiplexing technology

Redis uses network IO multiplexing technology to ensure high throughput in the case of multiple connections.

Multiplexing – refers to multiple socket connections, multiplexing – refers to the multiplexing of a thread. There are three main techniques for multiplexing: SELECT, Poll, and epoll. Epoll is the latest and best multiplexing technology available.

“Multiplexing” refers to multiple network connections, and “multiplexing” refers to the reuse of the same thread. Multiple I/O is used

Multiplexing allows a single thread to efficiently process multiple connection requests (minimizing network IO time consumption), and Redis is very fast at manipulating data in memory (in-memory operations are not a performance bottleneck here). These two factors contribute to Redis’s high throughput.

Redis high concurrency fast summary

  • Redis is a pure memory database, generally simple access operations, threads occupy a lot of time, time spending is mainly focused on IO, so the reading speed is fast.
  • As for IO, Redis uses non-blocking IO, IO multiplexing, using single threads to poll descriptors, converting database on, off, read, and write to events, reducing context switching and contention during thread switching.
  • Redis uses a single-threaded model, which ensures atomicity for each operation and reduces context switching and contention for threads.
  • In addition, data structure also helps a lot. Redis uses hash structure in the whole process to speed up the reading. There are also some special data structures to optimize the data storage, such as compression table to compress the storage of short data, and, for example, jump table to use ordered data structure to speed up the reading.
  • In addition, Redis adopts its own event separator, which has high efficiency, non-blocking execution mode and large throughput capacity.

This article is published by OpenWrite!