Welcome to pay attention to github.com/hsfxuebao, I hope to help you, if you think it can trouble to click on the Star ha

1. Reasons for fast Redis

1.1 Memory Operations

We all know that memory reads and writes are much faster than disk reads and writes. Redis is a database based on memory storage, which saves disk I/O consumption compared to a database where data is stored on disk. MySQL and other disk databases need to establish indexes to speed up the query efficiency, while Redis data stored in memory, direct operation of memory, so it is very fast.

1.2 Efficient underlying data structures

The underlying data structure of Redis has 6 kinds, namely, simple dynamic string (SDS), quicklist (quicklist), ziplist (ziplist), hashtable (hashtable), skiplist (skiplist) and integer array (intset). If this is not clear, refer to the Redis6 series

1.3 Thread Model

1.3.1 Single-threaded vs. multi-threaded

See article # Redis6 Series 1- The Threading model (single thread OR multi-thread)

1.3.2 Context Switch

Because it is a single-threaded model, it can avoid unnecessary switching and multithreading competition, which can save the time and performance consumption of multithreading switching, and single-threaded will not cause deadlock problems

1.4 the IO model

IO multiplex (select, poll, epoll)

1.4.1 File event handler

The Redis service uses the Reactor approach to implement file event handlers (each network connection actually has a file descriptor). Redis developed the network event handler based on the Reactor pattern, which is called the file event handler. It consists of four parts:

  • Multiple sockets

  • IO multiplexing procedures

  • File event dispatcher

  • Event handler.

Redis is called the single-threaded model because the consumption of the file event dispatcher queue is single-threaded, as shown below:

A file event is an abstraction of a socket operation that is generated whenever a socket is ready to perform connection reply, write, read, close, and so on. Because a server typically connects to multiple sockets, it is possible for multiple file events to occur concurrently. The I/O multiplexer listens for multiple sockets and passes those sockets that generated the event to the file event dispatcher.

Connection response processor

Networking. C/acceptTcpHandler function is Redis connection response processor, the CPU is used to connect to the server listening socket to reply the client, specific implementation for the sys/socket. H/acccept function of packaging.

When the Redis server is initialized, the program associates this connection reply handler with the AE_READABLE time that the server is listening for sockets. When a client is connecting to the server using sys/socket.h/connect to listen for sockets, The socket will emit an AE_READABLE event that causes the connection reply handler to execute and perform the corresponding socket reply operation.

Command request handler

Networking. C/readQueryFromClient function is Redis command request processor, the CPU is responsible for the read from the socket client sends the command request content, concrete implementation for unistd. H/read function of packaging.

When a client successfully connects to the server through the connection reply handler, the server associates the AE_READABLE event of the client socket with the command request handler. When the client sends a command request to the server, the socket will generate AE_READABLE events, causing the command request handler to execute. And perform the appropriate socket read operation.

The server will continue to associate the command request handler for the client socket AE_READABLE event the entire time the client connects to the server.

Command reply handler

Networking. C/reply sendReplyToClient function is Redis command processor, the CPU is responsible for the executive command after get the reply from the server through a socket returned to the client, the specific implementation for unistd. H/write function of packaging.

When the server has a command reply that needs to be sent to the client, the server associates the AE_WRITABLE event of the client socket with the command reply processor. When the client is ready to receive the command reply from the server, the AE_WRITABLE event will be generated, causing the command reply processor to execute. And performs the corresponding socket write operation.

When the command reply has been sent, the server disassociates the command reply handler from the AE_WRITABLE event of the client socket.

1.4.2 Select, poll, and epoll

1.5 Virtual Memory Mechanism

Redis directly built its own VM mechanism, unlike the general system will call system function processing, will waste a certain amount of time to move and request.

What is the virtual memory mechanism of Redis?

The virtual memory mechanism is to temporarily swap infrequently accessed data (cold data) from memory to disk, freeing up valuable memory space for other data that needs to be accessed (hot data). The VM function separates hot and cold data so that hot data is stored in the memory and cold data is saved to disks. This avoids the problem of slow access due to insufficient memory.

2. Triad: How fast is Redis?

After successfully installing Redis, Redis comes with a command that can be used to test performance. By running this command, we can simulate a scenario in which N clients send requests simultaneously and monitor how long it takes Redis to process these requests.

According to official documentation, Redis has been benchmarked on over 60,000 connections and still managed to maintain an efficiency of 50,000 q/s under these conditions, the same amount of requests that MySQL would fail to handle would simply crash. For this reason, Redis is often used as a cache to protect the database. 10 w QPS.

Why is Redis so fast? Why is Redis so fast?