preface
Redis can execute commands very fast, according to the official performance can reach 10W + QPS. So this article mainly introduces exactly where Redis fast.
Development of language
Now we all program in high-level languages such as Java, Python, etc. You might think C is ancient, but it’s really useful, because Unix systems are built in C, so C is very close to the operating system. Redis is developed in C, so execution is faster.
Pure memory access
Redis keeps all data in memory, and there is no need to read data from disk without data synchronization working properly, 0 IO. The memory response time is about 100 nanoseconds, which is an important basis for Redis’ speed. Let’s take a look at the CPU speed:
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. Memory is a hundred times slower. Disk is a million times slower.
Borrowed a figure of the deep understanding of computer systems, shows a typical memory hierarchy, in L0 layer, access to CPU can be in one clock cycle, based on SRAM cache spring renewal, can be in a couple of access to the CPU clock cycle, and is based on DRAM main memory, can be in the tens to hundreds of clock cycles to access them.
Single thread
The implementation of single thread simplified algorithm, concurrent data structure implementation is not only difficult and difficult to test.
Single-threading avoids the cost of thread switching and locking and releasing locks, which are often performance killers for server-side development.
Of course, single threading has its own downside, which is the nightmare of Redis: blocking. If one command is executed too long, it will block other commands, which is fatal to 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 both are examples of server performance.
Non-blocking multiplex I/O multiplexing mechanism
Here, “multiplexing” refers to multiple network connected clients, and “multiplexing” refers to the reuse of the same thread (single process). I/O multiplexing actually uses a thread to check the ready state of multiple sockets, and manages the processing of multiple I/O streams in a single thread by tracking the state of each Socket (I/O stream). Redis’s I/O multiplexing model is shown below:
The I/O multiplexing model takes advantage of the ability of select, poll, and epoll functions to monitor multiple I/O streams at the same time. These functions poll all I/O streams once (ePoll polls only the streams that actually emit events), processing and returning data in turn.
A stream in the ready state allows a single thread to efficiently process multiple connection requests, minimizing network IO time consumption, and Redis operates in memory, which is very fast. As a result, Redis has high throughput.
Follow wechat public account: IT elder brother
Java actual combat project video tutorial: you can get 200G, 27 sets of actual combat project video tutorial
Reply: Java learning route, you can get the latest and most complete a learning roadmap
Re: Java ebooks, get 13 must-read books for top programmers
Java foundation, Java Web, JavaEE all tutorials, including Spring Boot, etc
Reply: Resume template, you can get 100 beautiful resumes