Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is Redis single threaded?

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "is Redis single-threaded?". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Preface

Almost all interviews related to Java will ask questions about caching. The basic ones will ask what is "28 Law", what is "hot data and cold data", and more complicated ones will ask questions such as cache avalanche, cache penetration, cache preheating, cache update, cache degradation and so on. These seemingly unusual concepts are related to our cache server. Commonly used cache servers include Redis, Memcached and so on. At present, the most commonly used by the author is Redis.

If you haven't met the interviewer in previous interviews and asked you: why is Redis single-threaded or Redis so fast? Then you should think it is a very lucky thing when you read this article! If you happen to be a high-pressure interviewer, you can also take this question to the opposite side of the interview to test his mastery.

All right! Get to the point! Let's talk about what Redis is, why Redis is so fast, and then why Redis is single-threaded.

II. Brief introduction of Redis

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

It supports many types of data structures, such as string (Strings), hash (Hash), list (List), set (Set), ordered set (Sorted Set or ZSet) and range query, Bitmaps,Hyperloglogs and geospatial (Geospatial) index radius query. The common data structure types are String, List, Set, Hash and ZSet.

Redis has built-in replication (Replication), LUA script (Lua scripting), LRU driven event (LRU eviction), transaction (Transactions) and different levels of disk persistence (Persistence), and provides high availability (High Availability) through Redis Sentinel (Sentinel) and automatic partitioning (Cluster).

Redis also provides persistence options that allow users to save their data to disk for storage. Depending on the actual situation, the dataset can be exported to disk (snapshot) at regular intervals, or appended to the command log (AOF only appends files), and he will copy the write command executed to the hard disk when executing the write command. You can also turn off persistence and use Redis as an efficient network caching function.

Redis does not use tables, and his database does not predefine or force users to associate different data stored in Redis.

According to the storage mode, the working mode of database can be divided into hard disk database and in-memory database. Redis stores data in memory, and when reading and writing data, it will not be limited by the speed of the hard disk, so it is extremely fast.

1. The working mode of hard disk database:

2. Working mode of in-memory database:

After reading the above description, whether you have any understanding of some common Redis-related interview questions, such as: what is Redis, what are the common data structure types of Redis, how Redis is persisted, and so on.

3. How fast is Redis?

Redis adopts memory-based KV database based on single-process and single-thread model, which is written by C language. The official data is QPS (number of queries per second) which can reach 1000000 +. This data is no worse than the same memory-based KV database Memcached using single-process multithreading! If you are interested, you can refer to the official benchmark test: https://redis.io/topics/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 everyone can describe it correctly during the interview. When you don't ask you, the order of magnitude of your answer is very different!

4. Why is Redis so fast

1. Based entirely on memory, most of the requests are purely memory operations, which is very fast. The data is stored in memory, and the advantage similar to HashMap,HashMap is that the time complexity of search and operation is O (1).

2. The data structure is simple and the data operation is simple. The data structure in Redis is specially designed.

3. Single thread avoids unnecessary context switching and competition conditions, and there is no CPU consumption caused by multi-process or multi-thread switching, there is no need to consider all kinds of locks, there is no lock release operation, and there is no performance consumption caused by possible deadlocks.

4. Using the multi-channel Istroke O multiplexing model, non-blocking IO

5. Different underlying models are used, and the underlying implementation between them and the application protocols for communication with clients are different. Redis directly builds its own VM mechanism, because the general system calls system functions, it will waste a certain amount of time to move and request.

The above points are easy to understand. Let's make a simple discussion on the multi-channel Icano reuse model.

(1) Multi-channel Istroke O multiplexing model

The multiplexing model of multiple streams uses the ability of select, poll, and epoll to monitor multiple streams' Icano events at the same time, blocking the current thread when idle, and waking up from the blocked state when one or more streams have Icano events, so the program polls all streams once (epoll polls only those streams that actually emit events) and processes only those that are ready in sequence. This practice avoids a large number of useless operations.

Here, "multiplex" refers to multiple network connections, and "multiplexing" refers to the reuse of the same thread. Using multi-channel IO multiplexing technology allows a single thread to process multiple connection requests efficiently (minimizing the time consumption of network IO), and Redis operates data in memory very fast, that is to say, in-memory operations will not become a bottleneck affecting Redis performance, which mainly makes Redis have high throughput.

Then why is Redis single-threaded

First of all, we have to understand that the above analysis is to create a very fast atmosphere of Redis! According to the official FAQ, because Redis is a memory-based operation, CPU is not the bottleneck of Redis, and the bottleneck of Redis is most likely to be the size of machine memory or network bandwidth. Since single-threading is easy to implement, and CPU won't be a bottleneck, it makes sense to adopt a single-threaded solution (after all, multithreading can be troublesome! ).

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

When you see this, you may cry angrily! I thought there would be any major technical points that made it possible for Redis to use a single thread so quickly, but it was an official answer that seemed to fool us! However, we can clearly explain why Redis is so fast, and because it is so fast in single-threaded mode, there is no need to use multithreading!

However, we cannot achieve multicore CPU performance by using single-threading, but we can improve it by opening multiple Redis instances on a single machine!

Warning 1: here we have been emphasizing the single thread, but there is only one thread to handle our network requests, a formal Redis Server runtime must be more than one thread, here we need to pay attention to! For example, when Redis is persisted, it will be executed as a child process or sub-thread (whether it is a child thread or a child process for readers to study in depth); for example, I look at the Redis process on the test suit weapon, and then find the thread under the process:

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

Warning 2: the last paragraph in FAQ in the figure above describes how multithreading will be supported since Redis version 4.0, but only on some operations! So whether this article is still a single-threaded way in a later version needs to be verified by the reader!

VI. Expansion

Here are some models you should know. I wish you a hand in the interview.

1. Single-process multithreading model: MySQL, Memcached, Oracle (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 working process). There are two ways to start:

(1) single process startup: there is only one process in the system at this time, which acts as both the Master process and the Worker process.

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

(3) the Master process mainly initializes and manages the Worker; event handling is carried out in Worker.

This is the end of the content of "is Redis single-threaded?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report