Home > Database > Redis > Detailed explanation of the reasons why Redis is single-threaded and the three major reasons for high concurrency and speed

Detailed explanation of the reasons why Redis is single-threaded and the three major reasons for high concurrency and speed

藏色散人
Release: 2020-08-11 13:16:24
forward
3059 people have browsed it

The following column Redis Tutorial will introduce to you the reasons why Redis is single-threaded and the three major reasons for high concurrency and fast speed. I hope it will be helpful to friends in need!

Detailed explanation of the reasons why Redis is single-threaded and the three major reasons for high concurrency and speed

##Redis Reasons for high concurrency and speed

1.redis is based on memory, and the memory read and write speed is very fast;

2.redis is single-threaded, eliminating the need for A lot of context switching thread time;
3.redis uses multiplexing technology and can handle concurrent connections. The internal implementation of non-blocking IO uses epoll, using a simple event framework implemented by epoll itself. Read, write, close, and connect in epoll are all converted into events, and then use the multiplexing feature of epoll to never waste any time on io.

The following focuses on the reasons why single-threaded design and IO multiplexing core design are fast.

Why Redis is single-threaded

1.Official answer

Because Redis is a memory-based operation, CPU is not the bottleneck of Redis. Redis is the most bottleneck. It could be the size of the machine's memory or the network bandwidth. Since single-threading is easy to implement and the CPU will not become a bottleneck, it is logical to adopt a single-threaded solution.
2. Performance indicators
Regarding the performance of redis, the official website also has it. An ordinary notebook can easily handle hundreds of thousands of requests per second.
3. Detailed reasons
1) No performance consumption of various locks is required
The data structures of Redis are not all simple Key-Value, but also complex structures such as list and hash. These structures may Very fine-grained operations will be performed, such as adding an element at the end of a long list, adding or deleting an object from the hash. These operations may require adding a lot of locks, resulting in a greatly increased synchronization overhead.
In short, in the case of a single thread, there is no need to consider various lock issues. There is no locking and releasing lock operations, and there is no performance consumption caused by possible deadlocks.
2) Single-threaded multi-process cluster solution
The power of single-thread is actually very powerful, and the efficiency of each core is also very high. Multi-threading can naturally have a higher performance limit than single-threading, but in today's computing In the environment, even the upper limit of single-machine multi-threading often cannot meet the needs. What needs to be further explored is the multi-server clustering solution. Multi-threading technology is still not available in these solutions.
So single-threaded, multi-process cluster is a fashionable solution.
3) CPU consumption
Uses a single thread to avoid unnecessary context switching and competition conditions, and there is no switching caused by multi-process or multi-threading to consume CPU.
But what if the CPU becomes the bottleneck of Redis, or you don’t want other CPU cores of the server to be idle?
You can consider starting several more Redis processes. Redis is a key-value database, not a relational database, and there are no constraints between data. As long as the client distinguishes which keys are placed in which Redis process, it will be fine.

Advantages and disadvantages of Redis single thread

Advantages of single process and single thread

The code is clearer and the processing logic is simpler

There is no need to consider various locks Problem, there is no locking and releasing lock operation, and there is no performance consumption caused by possible deadlock
There is no CPU consumption due to switching caused by multi-process or multi-thread
The disadvantages of single process and single thread
Cannot be used Multi-core CPU performance can be improved by opening multiple Redis instances on a single machine;
IO multiplexing technology
redis uses network IO multiplexing technology to ensure high throughput of the system during multiple connections. quantity.
Multi-channel - refers to multiple socket connections, multiplexing - refers to reusing one thread. There are three main multiplexing technologies: select, poll, and epoll. epoll is the latest and best multiplexing technology available.
Here "multi-channel" refers to multiple network connections, and "reuse" refers to reusing the same thread. The use of multi-channel I/O multiplexing technology allows a single thread to efficiently handle multiple connection requests (minimizing the time consumption of network IO), and Redis operates data in memory very quickly (in-memory operations will not become a problem here). Performance bottleneck), the above two points mainly contribute to the high throughput of Redis.

Redis High Concurrency Quick Summary

  1. Redis is a pure memory database. Generally, it is a simple access operation. Threads take up a lot of time, and the time spent is mainly concentrated on IO, so the reading speed is fast.
  2. Let’s talk about IO again. Redis uses non-blocking IO, IO multiplexing, and uses a single thread to poll the descriptor, converting the opening, closing, reading, and writing of the database into events, reducing It eliminates context switching and competition when switching threads.
  3. Redis adopts a single-threaded model to ensure the atomicity of each operation and reduce thread context switching and competition.
  4. In addition, the data structure also helps a lot. Redis uses hash structure throughout the process, which has fast reading speed. There are also some special data structures that optimize data storage, such as compressed tables, short data Compressed storage, another example, skip tables, use ordered data structures to speed up reading.
  5. Another point is that Redis uses its own event separator, which is relatively efficient. It uses a non-blocking execution method internally and has a relatively large throughput capacity.

The above is a detailed explanation of redis’ high concurrency and fast speed.

The above is the detailed content of Detailed explanation of the reasons why Redis is single-threaded and the three major reasons for high concurrency and speed. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template