Recently, I have organized my personal study notes into a book, which includes Java basics, data structures, JVM, multi-threading, etc. Due to the limited space, only a few interview questions are shown below.

Friends in need can point to get:Click here to get it… Cipher: JJ

Redis 6.0 multi-threaded serial 13 questions.

1. Were versions prior to Redis6.0 really single-threaded?

Redis processes client requests, including access (socket read), parsing, execution, content return (socket write), etc. by a sequential main thread processing, which is called “single thread”. But strictly speaking, since Redis4.0 it is not single-threaded. In addition to the main thread, it also has background threads handling slower operations, such as cleaning up dirty data, releasing useless connections, deleting large keys, and so on.

2. Why didn’t you use multithreading until Redis6.0?

With Redis, there is almost no CPU bottleneck. Redis is mainly limited by memory and network. On a normal Linux system, for example, Redis can handle a million requests per second using Pipelining, so if the application uses mainly O(N) or O(log(N)) commands, it hardly consumes much CPU.

High maintainability after using single thread. Although multithreaded model has excellent performance in some aspects, it introduces the uncertainty of program execution order, brings a series of problems of concurrent read and write, increases the system complexity, and may cause performance loss caused by thread switching, even locking and unlocking, deadlock. Redis has very high processing performance through AE event model and IO multiplexing, so there is no need to use multithreading. The single-threaded mechanism greatly reduces the complexity of the internal implementation of Redis. Lazy Rehash, Lpush and other “thread-unsafe” commands of Hash can be executed without locking.

3. Why did Redis6.0 introduce multithreading?

Redis keeps all the data in memory, and the memory response time is about 100 nanoseconds. For small packets, the Redis server can handle 80,000 to 100,000 QPS, which is the limit of Redis processing. For 80% of companies, single-threaded Redis is sufficient.

However, with increasingly complex business scenarios, some companies are prone to hundreds of millions of transactions and therefore need larger QPS. The common solution is to partition data in distributed architecture and use multiple servers, but this solution has very big disadvantages, such as too many Redis servers to manage, high maintenance cost; Some commands that apply to a single Redis server do not apply to data partitions; Data partitioning does not solve hot read/write problems; Data skew, reallocation and zoom/zoom become more complex, etc.

From the perspective of Redis itself, the read/write system call of the read/write network takes up most of the CPU time during the execution of Redis, and the bottleneck mainly lies in the IO consumption of the network. Optimization mainly has two directions:

• Improved network IO performance, typically implemented using DPDK instead of the kernel network stack

• Use multiple threads to take advantage of multiple cores, a typical implementation being Memcached.

This way of stack optimization has little to do with Redis, supporting multi-threading is the most effective and convenient way to operate. So in summary, Redis supports multithreading for two main reasons:

• Can make full use of server CPU resources, currently the main thread can only use one core

• Multithreaded tasks can share the load of Redis synchronous IO reads and writes

4. Does Redis6.0 enable multi-threading by default?

Multithreading in Redis6.0 is disabled by default and only the main thread is used. To enable this function, modify the redis.conf configuration file: io-threads-do-reads yes

5.Redis6.0 How to set the number of threads when multi-threading is enabled?

After enabling multi-threading, you also need to set the number of threads, otherwise it will not take effect. Also modify the redis.conf configuration file

There is an official recommendation for setting the number of threads: 2 or 3 for a 4-core machine and 6 for an 8-core machine. The number of threads must be smaller than the number of cores. It is also important to note that the larger the number of threads is not always better, and the official view is that the number of threads above 8 is basically meaningless.

6. What is the performance improvement effect of multi-threading in Redis6.0?

The multithreaded IO feature introduced in Redis 6 more than doubles the performance increase. To obtain unstable I/O, obtain /SET commands with unstable I/O provided nearly double the performance of unstable I/O with unstable I/O.

Redis6.0 multithreading implementation mechanism?

The process is described as follows:

1. The main thread is responsible for receiving the connection establishment request, obtaining the socket and putting it into the global waiting for read processing queue

2. After the main thread processes the read events, it allocates these connections to the I/O threads through the Round Robin (RR)

3. The main thread blocks and waits for the I/O thread to finish reading the socket

4. The main thread executes the request command in a single thread manner. The request data is read and parsed, but not executed

5. The main thread blocks and waits for the I/O thread to write data back to the socket

6. Unbind and clear the wait queue

The design has the following characteristics:

The IO thread is either reading the socket at the same time or writing at the same time

2, the IO thread is only responsible for reading and writing socket parsing commands, not command processing

8. After multi-threading is enabled, will there be thread concurrency safety problem?

As can be seen from the above implementation mechanism, the multi-threaded part of Redis is only used to deal with the reading and writing of network data and protocol parsing, and the execution commands are still executed in single-thread order. So we don’t need to worry about concurrency and thread safety for control keys, LUA, transactions, LPUSH/LPOP, etc.

9. How do I install Redis6.0.1 on Linux?

This is no different from installing any other version of Redis, and the whole process runs without any potholes, so I won’t describe it here. The only thing to note is that the number of threads must be less than the number of CPU cores.

[[email protected] ~]# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Copy the code

10. Compare Redis6.0’s multi-threading model with Memcached’s multi-threading model

Until a few years ago, memcached was a popular caching solution for Internet companies, so the difference between Redis and memcached has become a must-ask question for interviewers. In recent years, memcached has been used less and more. With the addition of multithreading to Redis6.0, however, similar problems may arise, so let’s make a brief comparison for the multithreaded model.

The design has the following characteristics:

The IO thread is either reading the socket at the same time or writing at the same time

2, the IO thread is only responsible for reading and writing socket parsing commands, not command processing

Redis6.0 vs. Memcached Multithreaded model:

Similarities: Both adopt the master-worker thread model

Differences: Memcached executes the main logic in worker threads as well, making the model simpler and implementing true thread isolation in line with our general understanding of thread isolation. Redis gives the processing logic back to the master thread, which increases the model complexity to a certain extent, but also solves problems such as thread concurrency safety.

11. What does the author of Redis say about the new feature “multi-threading”?

In 6.0 RC1, Antirez explained the multithreading feature:

There are two possible ways in which Redis can support multithreading: the first is to open multiple threads in a single Redis instance, as in “memcached,” to increase the number of operations that can be performed per second in simple commands such as GET/SET. This involved I/O, command parsing, etc., so we called it “I/O threading.” The other is to allow slower commands to be executed on different threads to ensure that other clients do not block. We call this threading model “Slow Commands threading”.

After careful consideration, Redis does not use “I/O threading”. Redis is limited to network and memory at runtime, so the main way to improve Redis performance is through multiple Instances of Redis, especially clusters of Redis. Next, we will mainly consider two aspects of improvement:

1. Multiple instances of the Redis cluster can reasonably use the disks of the local instance through arrangement, avoiding rewriting AOF at the same time.

2. Provide a Redis cluster agent, so that users can abstract a cluster when there is no good cluster protocol client.

As a side note, Redis is a memory system like memcached, but not like memcached. Multithreading is complex and a simple data model must be considered. Threads performing LPUSH need to service other threads performing LPOP.

What I’m really looking forward to is “slow operations threading”. In REdis6 or Redis7, “key-level locking” will be provided so that threads can gain full control of keys to handle slow operations.

See: antirez.com/news/126

IO multiplexing is often mentioned in Redis threads.

This is a variant of the IO model, the classic Reactor design pattern, sometimes referred to as asynchronous blocking IO.

Multiplexing refers to multiple socket connections, and multiplexing refers to the reuse of a thread. There are three main techniques for multiplexing: SELECT, Poll, and epoll. Epoll is the latest and best multiplexing technology available. The use of multiplex I/O multiplexing allows a single thread to efficiently process multiple connection requests (minimizing network IO time consumption) and Redis to manipulate data in memory very quickly (in-memory operations are not a performance bottleneck here). These two factors contribute to the high throughput of Redis.

13. Do you know the Redis egg LOLWUT?

This has actually been available since Redis5.0, but forgive me if I just found out. This is how the author describes this feature in LOLWUT: A Piece of Art Inside a Database Command, “a piece of art within database commands.” You can call it a sentiment, you can call it an Easter egg, but I won’t reveal what it is. For those like me who don’t know what it is, see antirez.com/news/123 every time…

Biography: Swing unruly, love life. Java Cultivator (wechat official ID: Java Cultivator), welcome to follow. Access to 2000G of detailed information on the 2020 interview questions