Table of Contents
Non-blocking IO
Multiplexing (event polling)
Command Queue
Response Queue
Timing tasks
Home Database Redis This article will take you to quickly understand the thread IO model in Redis

This article will take you to quickly understand the thread IO model in Redis

Dec 21, 2021 am 10:19 AM
redis

Redis is single-threaded, but why is it so fast? One reason is that redis uses non-blocking IO and multiplexing to handle a large number of client connections. The following article will take you to understand the thread IO model in Redis. I hope it will be helpful to you!

This article will take you to quickly understand the thread IO model in Redis

Redis is a single-threaded application. NodeJs and Nginx are both single-threaded. They are all models of high-performance servers. [Related recommendations: Redis Video Tutorial]

The reason why Redis is single-threaded and so fast:

Firstly, because all its data is in memory All operations are memory-level operations, so when using redis, pay attention to instructions with a time complexity of O(n). Because it is single-threaded, if the amount of data is too large, other instructions will be blocked and wait;

The second reason is that redis uses non-blocking IO and multiplexing to handle a large number of client connections.

Non-blocking IO

When we use the read and write methods of the socket, the default is blocking,

That is, calling the read method to pass a parameter n, indicating the maximum number of reads Return after taking n bytes. If there is not a byte, the thread will continue to wait in the read method until data comes or the connection is closed. The read method returns at this time and the thread can execute the following logic,

The write method generally does not block. Unless the write buffer allocated by the kernel for the socket is full, the write method will block until there is free space in the buffer.

The following figure is the detailed process of socket reading and writing.

This article will take you to quickly understand the thread IO model in Redis

Non-blocking IO provides an option Non_Blocking when using sockets. When this option is turned on, the read and write methods will not block, but can read as much as they can. , write as much as you can,

How much you can read depends on the number of data bytes in the read buffer allocated by the kernel for the socket, and how much you can write depends on the data allocated by the kernel on the write buffer of the socket. Number of bytes,

The read and write methods will tell the program how many bytes have been read and written through the return value.

Non-blocking IO means that the thread no longer has to be blocked when reading and writing. Reading and writing can be completed instantly, and the thread can continue to do other things.

Multiplexing (event polling)

Although non-blocking IO is very fast, it also brings a problem. The thread reads the data and returns after reading part of it. It has not finished reading. When will the remaining data continue to be read? , writing data, the buffer is full and has not been written completely. When will the remaining data continue to be written?

When you can continue to read or continue to write, you should give the application a notification to tell the application that you can continue to read or continue to write. The event polling API is used to handle this problem.

select

The operating system provides a select function for the user program. The input is the read and write descriptor list read_fds & write_fds, and the output is The corresponding readable and writable events,

also provide the timeout parameter, the thread can wait for the timeout at most. During this period, if an event comes, the method will return immediately, and the thread will continue processing. If the timeout time is exceeded, The method will also return.

If the event is obtained, the thread can process the corresponding events one by one. After processing, it will continue to call the select api polling, so the thread is actually an infinite loop and keeps selecting. Non-stop processing, back and forth, this endless loop is called an event loop, and a loop is a cycle.

This article will take you to quickly understand the thread IO model in Redis

Event loop pseudocode:

while True
    read_events, write_events = select(read_fds, write_fds, timeout)
    for event in read_events:
        handle_read(event.fd)
    for event in write_events:
        handle_write(event.fd)
    handle_others() # 做其他的逻辑处理,处理定时任务等等
Copy after login

Through the select function we can handle the read and write events of multiple channel descriptors, so systems like select will The function call is called multiplexing API.

The multiplexing API of modern operating systems no longer uses the select system call, but uses epoll (linux) and kqueue (FreeBSD, macosx) instead.

The performance of select will become very poor when the number of descriptors increases. There are slight differences in the use of epoll and select, but they can be understood using the above pseudocode. When an event occurs on the descriptor, the descriptor will be looped. The event is processed. The read operation of the

serversocket object refers to calling accept to accept a new connection from the client. When a connection comes, it is also notified through the read event called by select.

The NIO technology in Java is event polling, and other languages ​​also have this technology.

Command Queue

Redis associates a command queue with each client socket, and the commands sent by the client are processed in first-in, first-out order through the queue.

Response Queue

Similarly, the results returned by Redis are also returned through a queue associated with each client. If the queue is empty, there is no need to obtain write events for the time being,

At this time, the client descriptor will be removed from write_fds, and when the queue has data, the descriptor will be put in. This can avoid finding that there is no data to write when the select system call returns the write event, causing empty space. Polling, useless polling, consumption of machine CPU.

Timing tasks

The server not only needs to respond to IO events, but also needs to handle some other things, such as the application's own timing tasks. If the thread blocks on the select call, waiting for the return of select, this will cause Some scheduled tasks have expired but have not been executed.

Redis’ scheduled tasks are recorded in a data structure called the minimum heap. In this heap, the fastest tasks to be executed are ranked at the top. Each cycle During the cycle, redis will process the tasks in the heap that have reached the time point.

After the processing is completed, the time required for the tasks to be executed in the heap will be recorded. When select is called again, this time will be The value of timeout means that no other tasks will need to be executed during this period. Redis can safely block for this long at most, and then perform corresponding processing after the time is up.

The event processing principles of NodeJs and Nginx are similar to Redis.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of This article will take you to quickly understand the thread IO model in Redis. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to build the redis cluster mode How to build the redis cluster mode Apr 10, 2025 pm 10:15 PM

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to use the redis command How to use the redis command Apr 10, 2025 pm 08:45 PM

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

How to read the source code of redis How to read the source code of redis Apr 10, 2025 pm 08:27 PM

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

How to clear redis data How to clear redis data Apr 10, 2025 pm 10:06 PM

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

How to view all keys in redis How to view all keys in redis Apr 10, 2025 pm 07:15 PM

To view all keys in Redis, there are three ways: use the KEYS command to return all keys that match the specified pattern; use the SCAN command to iterate over the keys and return a set of keys; use the INFO command to get the total number of keys.

How to read redis queue How to read redis queue Apr 10, 2025 pm 10:12 PM

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

How to start the server with redis How to start the server with redis Apr 10, 2025 pm 08:12 PM

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

See all articles