search
HomeDatabaseRedisWhy does Redis introduce multi-threading?

1. Overview of the problem

The versions after Redis 6.0 abandoned the design of single-threaded model. Redis, which originally used single-threaded operation, also began to selectively use multi-threaded model. At first glance, Redis The author is so awesome, but he can't escape the "law of true fragrance".

If you think about it carefully, this question can actually be split into two main questions:

(1) Why did Redis choose a single-threaded model at the beginning (the benefits of single-threading)?

(2) Why did Redis add multi-threading after 6.0 (in some cases, single-threading has shortcomings, which can be solved by multi-threading)?

In fact, it’s not that the author has not escaped the True Fragrance Theorem, but as time goes by, more and more problems arise. The original design must be somewhat out of date, and changes should be made. OK, with two questions, let’s analyze them carefully.

2. Why did Redis use single thread at the beginning?

Whether it is single thread or multi-thread, it is all to improve the development efficiency of Redis, because Redis is a memory-based database and needs to be processed. A large number of external network requests inevitably require multiple IOs. Fortunately, Redis uses many excellent mechanisms to ensure its high efficiency. So why is Redis designed in single-threaded mode? It can be summarized as follows:

(1) IO multiplexing

Let’s take a look at the top-level design of Redis.

FD is a file descriptor, which means whether the current file is readable, writable or abnormal. Use the I/O multiplexing mechanism to monitor the readable and writable status of multiple file descriptors simultaneously. You can understand it as having the characteristics of multi-threading.

Once a network request is received, it will be processed quickly in memory. Since most operations are purely memory-based, the processing speed will be very fast. That is to say, in single-threaded mode, even if there is a lot of connected network processing, it can still be ignored in high-speed memory processing because of IO multiplexing.

(2) High maintainability

Although the multi-threading model performs well, it will introduce uncertainty in the order of program execution, thus causing problems related to concurrent reading and writing. . Single-threaded mode allows for easy debugging and testing.

(3) Based on memory, the efficiency is still high in single-threaded state

Multi-threading can make full use of CPU resources, but for Redis, because the memory-based speed is quite high, It can handle 100,000 user requests in one second. If 100,000 in one second is not enough, then we can use Redis sharding technology to deliver it to different Redis servers. This cooking method avoids introducing a lot of multi-threaded operations in the same Redis service.

Unless AOF backup is required, this operation basically does not involve any I/O operations because it is memory-based. Since the reading and writing of these data only occur in memory, the processing speed is very fast; using a multi-threading model to handle all external requests may not be a good solution.

Now we know that it can be basically summed up in two sentences. It is based on memory and uses multiplexing technology. The single-thread speed is very fast and the characteristics of multi-threading are guaranteed. Because there is no need to use multithreading.

3. Why introduce multi-threading?

We just mentioned the advantages of single-threading, but now I want to talk about why we need to introduce multi-threading and overcome the discomfort with it. The introduction of multi-threading shows that in some aspects of Redis, single-threading no longer has advantages.

Because the read/write system calls for reading and writing the network take up most of the CPU time during Redis execution, if the network reading and writing is made multi-threaded, the performance will be greatly improved.

Redis's multi-threading is only used for network data reading and writing and protocol parsing, while command execution is still single-threaded. The reason for this design is that we do not want Redis to become complicated due to multi-threading, and we need to control concurrency issues such as keys, lua, transactions, LPUSH/LPOP, etc.

Redis has added some deletion operations that can be processed asynchronously by other threads in the latest versions, which are the UNLINK and FLUSHALL ASYNC## we mentioned above. # and FLUSHDB ASYNC, why do we need these deletion operations, and why do they need to be processed asynchronously through multi-threading?

We know that Redis can use the del command to delete an element. If the element is very large, which may occupy dozens of megabytes or hundreds of megabytes, it cannot be completed in a short time. In this way, it is necessary Multi-threaded asynchronous support.

Deletion work can now be done in the background.

The above is the detailed content of Why does Redis introduce multi-threading?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Redis: Understanding Its Architecture and PurposeRedis: Understanding Its Architecture and PurposeApr 26, 2025 am 12:11 AM

Redis is a memory data structure storage system, mainly used as a database, cache and message broker. Its core features include single-threaded model, I/O multiplexing, persistence mechanism, replication and clustering functions. Redis is commonly used in practical applications for caching, session storage, and message queues. It can significantly improve its performance by selecting the right data structure, using pipelines and transactions, and monitoring and tuning.

Redis vs. SQL Databases: Key DifferencesRedis vs. SQL Databases: Key DifferencesApr 25, 2025 am 12:02 AM

The main difference between Redis and SQL databases is that Redis is an in-memory database, suitable for high performance and flexibility requirements; SQL database is a relational database, suitable for complex queries and data consistency requirements. Specifically, 1) Redis provides high-speed data access and caching services, supports multiple data types, suitable for caching and real-time data processing; 2) SQL database manages data through a table structure, supports complex queries and transaction processing, and is suitable for scenarios such as e-commerce and financial systems that require data consistency.

Redis: How It Acts as a Data Store and ServiceRedis: How It Acts as a Data Store and ServiceApr 24, 2025 am 12:08 AM

Redisactsasbothadatastoreandaservice.1)Asadatastore,itusesin-memorystorageforfastoperations,supportingvariousdatastructureslikekey-valuepairsandsortedsets.2)Asaservice,itprovidesfunctionalitieslikepub/submessagingandLuascriptingforcomplexoperationsan

Redis vs. Other Databases: A Comparative AnalysisRedis vs. Other Databases: A Comparative AnalysisApr 23, 2025 am 12:16 AM

Compared with other databases, Redis has the following unique advantages: 1) extremely fast speed, and read and write operations are usually at the microsecond level; 2) supports rich data structures and operations; 3) flexible usage scenarios such as caches, counters and publish subscriptions. When choosing Redis or other databases, it depends on the specific needs and scenarios. Redis performs well in high-performance and low-latency applications.

Redis's Role: Exploring the Data Storage and Management CapabilitiesRedis's Role: Exploring the Data Storage and Management CapabilitiesApr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis: Understanding NoSQL ConceptsRedis: Understanding NoSQL ConceptsApr 21, 2025 am 12:04 AM

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

Redis: Real-World Use Cases and ExamplesRedis: Real-World Use Cases and ExamplesApr 20, 2025 am 12:06 AM

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis: Exploring Its Features and FunctionalityRedis: Exploring Its Features and FunctionalityApr 19, 2025 am 12:04 AM

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools