Home > Database > Redis > A brief analysis of why Redis is fast? Where are you soon?

A brief analysis of why Redis is fast? Where are you soon?

青灯夜游
Release: 2022-01-13 09:56:24
forward
2589 people have browsed it

Why is Redis fast? Where is Redis? The following article will help you analyze the reasons why Redis is so fast. I hope it will be helpful to you!

A brief analysis of why Redis is fast? Where are you soon?

Redis is a NoSQL database based on key-value pairs. The Value of Redis can be composed of String, hash, list, set, zset, Bitmaps, HyperLogLog It consists of a variety of data structures and algorithms. Redis also provides key expiration, publish and subscribe, transactions, Lua scripts, sentinels, Cluster and other functions. [Related recommendations: Redis Video Tutorial]

Redis executes commands very quickly, and according to the official performance, it can reach 10w qps. So this article mainly introduces where Redis is fast, mainly including the following points:

1. Development language

Now we all use high-level languages To program, such as Java, python, etc. You may think that C language is very old, but it is really useful. After all, the Unix system is implemented in C, so C language is a language that is very close to the operating system. Redis is developed in C language, so the execution will be faster.

In addition, if college students learn C well, it will help you better understand computer operating systems. Don't think that after learning a high-level language, you don't have to pay attention to the bottom layer. The debt you owe will always have to be repaid. Here is a more difficult book to recommend, "In-depth Understanding of Computing Systems".

2. Pure memory access

Redis places all data in memory. Non-data synchronization works normally and does not need to be retrieved from disk. Reading data, 0 IO times. The memory response time is about 100 nanoseconds, which is an important basis for the fast speed of Redis. Let’s take a look at the speed of the CPU first:

A brief analysis of why Redis is fast? Where are you soon?

Take my computer as an example, the main frequency is 3.1G, which means it can execute 3.1*10^9 instructions per second. So the CPU sees the world very, very slowly, the memory is a hundred times slower than it, and the disk is a million times slower than it. Do you think it is faster or not?

I borrowed a picture from "In-depth Understanding of Computer Systems", which shows a typical memory hierarchy. At the L0 layer, the CPU can access it in one clock cycle, and the SRAM-based cache is renewed. They can be accessed in a few CPU clock cycles, and then DRAM-based main memory, which can be accessed in tens to hundreds of clock cycles.

A brief analysis of why Redis is fast? Where are you soon?

3. Single thread

First, single-thread simplified algorithm implementation, concurrent data Structural implementation is not only difficult but also cumbersome to test. Second, a single thread avoids the consumption caused by thread switching and locking and releasing locks. For server-side development, locks and thread switching are usually performance killers. Of course, single threading will also have its shortcomings, which is also Redis's nightmare: blocking. If the execution of a command is too long, it will cause other commands to be blocked, which is very fatal for Redis, so Redis is a database for fast execution scenarios.

In addition to Redis, Node.js is also single-threaded, and Nginx is also single-threaded, but they are both models of high-performance servers.

4. Non-blocking multi-channel I/O multiplexing mechanism

Before this, let me talk about the traditional blocking I/O How it works: When using read or write to read or write a file descriptor (File Descriptor FD), if the data is not received, the thread will be suspended until the data is received.

Although the blocking model is easy to understand, it will not be used when multiple client tasks need to be processed.

A brief analysis of why Redis is fast? Where are you soon?

#I/O multiplexing actually means that the management of multiple connections can be in the same process. Multi-channel refers to network connections, multiplexing is just the same thread. In network services, the role of I/O multiplexing is to notify the business code of multiple connection events at one time. The processing method is determined by the business code.

In the I/O multiplexing model, the most important function call is the I/O multiplexing function. This method can monitor the reading and writing of multiple file descriptors (fd) at the same time. When some of the fds are readable/writable, this method will return the number of readable/writable fds.

A brief analysis of why Redis is fast? Where are you soon?

Redis uses epoll as the implementation of I/O multiplexing technology, and Redis's own event processing model converts epoll's read, write, close, etc. events without wasting too much time on network I/O. Realize monitoring of multiple FD reads and writes to improve performance.

A brief analysis of why Redis is fast? Where are you soon?

Let’s give a vivid example. For example, a tcp server handles 20 client sockets.

A plan: Sequential processing. If the first socket is slow in reading data due to the network card, everything will be messed up after it is blocked.

Plan B: Create a clone sub-process for each socket request. Not to mention that each process consumes a large amount of system resources. The process switching alone is enough for the operating system to be tiring.

C scheme (I/O multiplexing model, epoll): Register the fd corresponding to the user socket into epoll (actually what is passed between the server and the operating system is not the fd of the socket but the data structure of fd_set) , and then epoll only tells which sockets need to be read/written, and only needs to process those active and changing socket fds.

In this way, the entire process will only block when epoll is called, and sending and receiving customer messages will not block.

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of A brief analysis of why Redis is fast? Where are you soon?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template