Why Redis is single-threaded and why is Redis so fast!

Source: Internet
Author: User
Tags benchmark epoll lua memcached redis server

1. Introduction

Nearly all interviews related to Java will ask the question of caching, the basic one will ask what is the "two eight law", what is "hot data and cold data", the more complicated one will ask about cache avalanche, cache penetration , Cache warm-up, cache update, cache degradation and other issues, these seemingly uncommon concepts are related to our cache server. The commonly used cache servers are Redis, Memcached, etc., and the most commonly used by the author is Redis. One kind.

If you have n’t met the interviewer in the previous test and asked you "Why Redis is single-threaded and why is Redis so fast!" ", Then when you read this article, you should think it is a very lucky thing! If you happen to be a high-profile interviewer, you can also use this question to interview a small partner like "seeing through the autumn water" opposite to test his mastery.

Alright! Step into the topic! Let ’s first discuss what Redis is, why is Redis so fast, and then why Redis is single-threaded?

2. Introduction to Redis

Redis is an open source in-memory data structure storage system, which can be used as: database, cache and message middleware.

It supports multiple types of data structures, such as String, Hash, List, Set, Sorted Set or ZSet and range query, Bitmaps, Hyperloglogs and Geospatial index radius query. The common data structure types are: String, List, Set, Hash, ZSet.

Redis has built-in replication (Replication), LUA scripting (Lua scripting), LRU driver events (LRU eviction), transactions (Transactions) and different levels of disk persistence (Persistence), and through Redis sentry (Sentinel www.xucaizxyl.com) And automatic partition (Cluster) to provide high availability (High Availability).

Redis also provides options for persistence, these options allow users to save their data to disk for storage. According to the actual situation, you can export the data set to disk (snapshot) at a certain time, or append it to the command log (AOF only appends files), he will copy the executed write command to the hard disk when executing the write command . You can also turn off persistence and use Redis as an efficient network data cache function.

Redis does not use tables, and his database will not pre-define or force users to associate different data stored in Redis.

The working mode of the database can be divided into: hard disk database and memory database. Redis stores data in memory, and is not limited by the hard disk I / O speed when reading and writing data, so the speed is extremely fast.

(1) Working mode of the hard disk database:



(2) Working mode of in-memory database:



After reading the above description, do you know some common Redis-related interview questions, such as: What is Redis, what are the common data structure types of Redis, and how is Redis persistent?

3. How fast is Redis?

Redis uses a memory-based KV database using a single-process single-thread model, written in C language, the official data is QPS (number of queries per second) that can reach 100000+. This data is no worse than the same memory-based KV database Memcached that uses a single process and multiple threads! Those interested can refer to the official benchmark program "How fast is Redis?" 》 (Https://redis.io/topics/www.huayyule.com benchmarks)


The horizontal axis is the number of connections, and the vertical axis is QPS. At this time, this picture reflects an order of magnitude. I hope you can describe it correctly during the interview. When you do n’t ask you, the magnitude of your answer is very different!

4, why is Redis so fast

(1). It is completely based on memory. The vast majority of requests are purely memory operations, which are very fast. The data is stored in memory, similar to HashMap, the advantage of HashMap is that the time complexity of search and operation is O (1);

(2). The data structure is simple, and the data operation is also simple. The data structure in Redis is specially designed;

(3). The use of single thread avoids unnecessary context switching and race conditions, and there is no multi-process or multi-thread switching that consumes CPU. There is no need to consider the problems of various locks. There is no lock release operation, no Performance consumption due to possible deadlock;

(4). Use multiple I / O multiplexing model, non-blocking IO;

(5). The underlying model is different. The underlying implementation and communication protocol between the client and the client are different. Redis directly builds the VM mechanism by itself. If the general system calls the system function, it will waste a certain amount of time to move. And request;

The above points are relatively easy to understand. Below we will briefly discuss the multiplex I / O multiplexing model:

(1) Multiplex I / O multiplexing model

The multiplex I / O multiplexing model utilizes the ability of select, poll, and epoll to simultaneously monitor I / O events of multiple streams. When idle, the current thread is blocked. When one or more streams have I / O event, it wakes up from the blocking state, so the program will poll all streams (epoll is only polling those streams that actually emitted the event), and only processes the ready streams in sequence. This approach It avoids a lot of useless operations.

Here "multi-channel" refers to multiple network connections, "multiplexing" refers to multiplexing the same thread. Using multiple I / O multiplexing technology allows a single thread to efficiently process multiple connection requests (reduce the time consumption of network IO as much as possible), and Redis operates data in memory very fast, which means that operations in memory are not It will become a bottleneck that affects Redis performance, mainly because of the above points, Redis has a high throughput.

5. So why is Redis single-threaded

First of all, we must understand that the above analysis is to create a Redis atmosphere! The official FAQ stated that because Redis is a memory-based operation, the CPU is not the bottleneck of Redis. The bottleneck of Redis is most likely the size of machine memory or network bandwidth. Since single-threading is easy to implement, and the CPU will not become a bottleneck, it is logical to adopt the single-threaded solution (after all, using multithreading will have a lot of trouble!)


Can refer to: https://redis.io/topics/faq

Seeing this, you might cry! I thought there would be any major technical points to make Redis use single-threaded so fast. I didn't expect it to be an official seemingly fooling our answer! However, we can clearly explain why Redis is so fast, and because it is already fast in the single-threaded mode, there is no need to use multi-threading!

However, we cannot use the multi-core CPU performance in the single-threaded way, but we can improve it by opening multiple Redis instances on a single machine!

Warning 1: The single thread we have been emphasizing here is that there is only one thread to handle when processing our network requests. There must be more than one thread when a formal Redis Server is running. Here you need to pay attention to it! For example, when Redis is persisted, it will be executed in the form of a sub-process or a sub-thread (specifically whether the sub-thread or sub-process is for readers to study in depth); for example, I check the Redis process on the test server and then find the thread under the process:



The "-T" parameter of the ps command indicates the display threads (Show threads, possibly with SPID column.) The "SID" column indicates the thread ID, and the "CMD" column displays the thread name.

Warning 2: In the last paragraph of the FAQ, it states that the Redis 4.0 version will support multi-threading, but it only performs multi-threading on certain operations! So whether the article is still single-threaded in future versions requires readers to verify!

6, pay attention

1. We know that Redis uses the "single-threaded multiplexed IO model" to achieve high-performance in-memory data services. This mechanism avoids the use of locks, but at the same time this mechanism is more time-consuming in the sun and the like The command will make the concurrency of redis drop. Because it is a single thread, only one operation is in progress at the same time. Therefore, time-consuming commands will lead to a decrease in concurrency, not only read concurrency, but also write concurrency. And a single thread can only use one CPU core, so you can start multiple instances in the same multi-core server to form a master-master or master-slave form. Time-consuming read commands can be performed entirely in the slave.

Redis.conf items that need to be changed:

Pidfile /var/run/redis/redis_6377.pid #pidfile to add the port number

Port 6377 #This must be changed

Logfile /var/log/redis/redis_6377.log #The name of the logfile is also added with the port number

Dbfilename dump_6377.rdb #rdbfile also add the port number

2. "We can't let the operating system load balance, because we know our own programs better, so we can manually assign CPU cores to them without taking up the CPU too much, or let our key processes and one Heap other processes together. ".

CPU is an important influencing factor. Because it is a single-threaded model, Redis prefers large cache and fast CPU instead of multi-core

On a multi-core CPU server, Redis' performance also depends on the NUMA configuration and processor binding location. The most obvious impact is that redis-benchmark will randomly use CPU cores. In order to obtain accurate results, you need to use a fixed processor tool (you can use taskset on Linux). The most effective way is to separate the client and server into two different CPUs to use the third-level cache in colleges.

7z, expansion

The following are several models you should know, I wish you a hand in the interview!

1. Single-process multi-thread model: MySQL, Memcached, Oracle (www.tygj178.com Windows version);

2. Multi-process model: Oracle (Linux version);

3. Nginx has two types of processes, one is called Master process (equivalent to management process), and the other is called Worker process (actual work process). There are two ways to start:

(1) Single process start: At this time, there is only one process in the system, which acts as both the role of Master process and the role of Worker process.

(2) Multi-process startup: At this time, the system has only one Master process and at least one Worker process works.

(3) The Master process mainly performs some global initialization work and manages the work of the Worker; event processing is carried out in the Worker.

Why is Redis single-threaded and why is Redis so fast!


Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.