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

What are the key points that are often asked in redis interviews?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "what are the key points often asked in the redis interview". The explanation in the article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the key points often asked in the redis interview?"

12 key points often asked in redis interview

Redis is a hurdle that can not be bypassed in the interview, as long as you have used Redis in your resume, you will certainly not escape.

Xiao Zhang:

Hello, interviewer. I'm here for an interview.

Interviewer:

Hello, Xiao Zhang. I read your resume and have a good command of Redis, so I'll just ask you a few Redis-related questions. First of all, my question is, is Redis single-threaded or multithreaded?

Xiao Zhang:

The threading model used between different versions of Redis is different. Before the Redis4.0 version, the single-threaded model was used, and after version 4.0, multithreading support was added.

Before 4. 0, although we said that Redis was single-threaded, it only said that its network I / O threads as well as Set and Get operations were done by one thread. However, Redis persistence and cluster synchronization are accomplished by other threads.

Multithreading support has been added since 4.0, which is mainly reflected in big data's asynchronous deletion function, such as unlink key, flushdb async, flushall async, etc.

Interviewer:

That's a good answer, so why did Redis choose to use single thread before 4.0? And how fast is it to use single thread?

Xiao Zhang:

The choice of single thread is mainly simple to use, there is no lock competition, all operations can be completed without lock, and there is no performance and time overhead caused by deadlock and thread switching, but at the same time, single thread can not give full play to the performance of multi-core CPU.

As for why single thread is so fast, I think there are mainly the following reasons:

Most of the operations of Redis are done in memory, and the execution efficiency in memory itself is very fast, and it uses efficient data structures, such as hash tables and jump tables.

The use of single thread avoids the competition of multi-thread, saves the time and performance overhead of multi-thread switching, and does not cause deadlock.

The Icano multiplexing mechanism is used to process a large number of client Socket requests, because it is based on the non-blocking Icano model, which enables Redis to communicate efficiently, and the read and write flow of Icano is no longer blocked.

Interviewer:

Yes, so how does Redis achieve data loss?

Xiao Zhang:

Redis data is stored in memory, in order to ensure that Redis data is not lost, it is necessary to store the data from memory to disk, so that the original data can be recovered from disk after server restart, which is called Redis data persistence. There are three ways to persist Redis data.

AOF log (Append Only File, file append): all operation commands are recorded and appended to the file as text.

RDB Snapshot (Redis DataBase): writes memory data at a certain time to disk in a binary way.

Hybrid persistence: Redis 4.0adds hybrid persistence, which integrates the advantages of RDB and AOF.

Interviewer:

Then talk about the implementation principle of AOF and RDB respectively.

Xiao Zhang:

AOF uses the method of logging after writing. Redis first executes commands to write data to memory, and then logs to a file. The AOF log records the operation commands, not the actual data. If you use the AOF method to do fault recovery, you need to execute all the logs once.

RDB adopts the way of memory snapshot, which records the data at a certain time, not the operation, so when using RDB method to do fault recovery, you only need to read the RDB file directly into memory to achieve fast recovery.

Interviewer:

You just mentioned that AOF uses "post-write log", while our usual MySQL uses "pre-write log", so why does Redis execute the command first and then write the data to the log?

Xiao Zhang: his forehead began to sweat. What questions did he ask?

Well, this is mainly because Redis does not check the syntax of the command before writing to the log, so only the successful command is recorded to avoid recording incorrect commands, and writing the log after the command execution will not block the current write operation.

Interviewer:

What is the risk of writing a diary after that?

Xiao Zhang:

Me. I can't do that.

Interviewer:

Well, there are two main risks that can occur in post-journaling:

Data may be lost: if the Redis has just finished executing the command and there is a failure and downtime, there is a risk that the command will be lost.

Other operations may be blocked: the AOF log is actually executed in the main thread, so when Redis writes the log file to disk, it still blocks subsequent operations from being performed.

I also have a question: does RDB block threads when taking snapshots?

Xiao Zhang:

Redis provides two commands to generate RDB snapshot files, save and bgsave. The save command is executed in the main thread and causes blocking. The bgsave command creates a child process for writing to the RDB file, avoiding blocking on the main thread, which is also the default configuration of Redis RDB.

Interviewer:

Can the data be modified when RDB takes snapshots?

Xiao Zhang:

Save is synchronized and blocks client commands, which can be modified when bgsave.

Interviewer:

So how does Redis solve the problem of allowing data modification when bgsave takes snapshots?

Xiao Zhang: why do you ask? I ™will not ah! )

Well, I'm not sure about that.

Interviewer:

Here, it is mainly implemented using the child threads of bgsave, and the specific operations are as follows:

If the main thread performs a read operation, the main thread and the bgsave child process do not affect each other

If the main thread performs a write operation, the modified data makes a copy, and then the bgsave child process writes the copy data to the RDB file, during which the main thread can still modify the original data directly.

It should be noted that the execution frequency of Redis to RDB is very important, because it will affect the integrity of snapshot data and the stability of Redis, so after Redis 4.0, a mixed data persistence mechanism of AOF and RDB is added: write the data to the file in the way of RDB, and then save the subsequent operation commands to the file in AOF format, which not only ensures the speed of Redis restart, but also reduces the risk of data loss.

Xiao Zhang:

Got it. Got it.

Interviewer:

Then why don't you tell me again how Redis achieves high availability?

Xiao Zhang:

There are three main ways to achieve high availability in Redis: master-slave replication, Sentinel mode, and Redis cluster.

Master-slave replication

Synchronize data from a former Redis server to multiple slave Redis servers, that is, a master-slave mode, which is the same as the principle of MySQL master-slave replication.

Sentinel mode

When using Redis master-slave service, there will be a problem, that is, when the master-slave server of Redis goes down, it needs to be restored manually. In order to solve this problem, Redis has added Sentinel mode (because Sentinel mode can monitor master-slave server and provide automatic disaster recovery).

Redis Cluster (Cluster)

Redis Cluster is a distributed decentralized operation mode, which is a Redis cluster scheme introduced in Redis version 3.0. it distributes data on different servers, so as to reduce the system's dependence on a single master node and improve the read and write performance of Redis services.

Interviewer:

Using Sentinel mode, there is a copy of data as a guarantee, and there is Sentinel monitoring in availability. Once master has the opportunity to elect a salve node as a master node, which has met the needs of our production environment, why do we need to use cluster mode?

Xiao Zhang:

Well, in the final analysis, the sentry mode is the master-slave mode. In the master-slave mode, we can expand the read concurrency capability by adding salve nodes, but there is no way to expand the write capacity and storage capacity. The storage capacity can only be the upper limit of the capacity of master nodes. So in order to expand write capacity and storage capacity, we need to introduce cluster mode.

Interviewer:

With so many Master nodes in the cluster, how can redis cluster determine which node to choose when storing?

Xiao Zhang:

It's supposed to be using some kind of hash algorithm, but I don't know.

Interviewer:

Well, that's all for today's interview. You go back and wait for our interview notice.

Xiao Zhang:

OK, thank you, interviewer. Can you tell me how redis cluster implements node selection?

Interviewer:

Redis Cluster uses the class consistent hash algorithm to select nodes. As for what is the consistent hash algorithm, go back and see for yourself.

Redis Cluster divides itself into 16384 Slot (slots). Hash slots are similar to data partitions. Each key-value pair is mapped to a hash slot according to its key. The specific implementation process is divided into two steps.

According to the key of the key-value pair, a value of 16 bit is calculated according to the CRC16 algorithm.

Then use the 16bit value to take the module for 16384, and get the modulus in the range of 0,16383, where each module represents a corresponding numbered hash slot.

Each Redis node is responsible for handling some slots. You have three master nodes ABC. Each node is responsible for the following slots:

This enables the selection of cluster nodes.

Thank you for your reading. The above is the content of "what are the key points often asked in redis interviews?" after the study of this article, I believe you have a deeper understanding of the key points often asked in redis interviews, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Database

Wechat

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

12
Report