2

This issue is the 6 issue of the [ Interview ] series of articles, simulating Redis basic knowledge high-frequency interview questions.

Interview begins

Interviewer : ’s talk about Redis today

Interviewer : said that Redis is fast, so why is Redis so fast?

: Mainly because of the following reasons:

  • is based on memory : Redis uses memory storage without the overhead of disk IO. The data is stored in the memory, and the read and write speed is fast.
  • Single-threaded implementation of (before Redis 6.0): Redis uses a single thread to process requests, avoiding the overhead of thread switching and lock resource contention between multiple threads.
  • IO multiplexing model : Redis uses IO multiplexing technology. Redis uses a single thread to poll descriptors, converts database operations into events, and doesn't waste too much time on network I/O.
  • Efficient data structure : The bottom layer of each data type of Redis is optimized in order to pursue faster speed.

Interviewer : Well, I just mentioned that Redis is implemented by a single thread. Why do you think Redis chooses a single thread?

Monologue: emm, I have to ask the Redis author...

: 1. Single-threaded implementation can avoid excessive context switching overhead . The program always runs in a single thread in the process, and there is no scenario of multi-thread switching.

: 2. avoids the overhead of the synchronization mechanism : If Redis chooses a multi-threaded model and needs to consider the problem of data synchronization, some synchronization mechanisms will inevitably be introduced, which will lead to more overhead, increasing program complexity and reducing performance.

: 3. is simple to implement and easy to maintain : If Redis uses multi-threading mode, then all the design of underlying data structures must consider thread safety issues, then the implementation of Redis will become more complicated.

Interviewer : What are the application scenarios of Redis?

: caches hot data to relieve the pressure on the database.

: Using the atomic auto-increment operation of Redis, the functions of the counter can be realized, such as counting user likes, user visits, etc.

: used as a simple message queue to realize asynchronous operation.

: speed limiter , which can be used to limit the frequency of a user accessing a certain interface. For example, the spike scene is used to prevent users from quickly clicking to bring unnecessary pressure.

: Friendship , using some commands of sets, such as intersection, union, difference, etc., to achieve common friends, common hobbies and other functions.

Interviewer : Oh, how does Redis implement message queues?

Monologue: Damn, I dug a hole for myself...

: 1. Use the list, let the producer put the task into the list using the LPUSH command, and the consumer will continue to use RPOP to remove the task from the list.

: 2. Publish-subscribe mode. Similar to MQ's topic mode. Only messages published after subscription can be consumed, and a message can be consumed by multiple subscribers.

: 3. Delay queue. Use sortedset, take the timestamp as the score, the message content as the key, and call zadd to produce the message. The consumer uses the zrangebyscore command to obtain the data polling before N seconds for processing.

Interviewer : talk about the principle of Redis master-slave replication?

: Well, the replication function of Redis is to support data synchronization between multiple databases. The master database can perform read and write operations, and when the data of the master database changes, the data will be automatically synchronized to the slave database. The slave database is generally read-only, and it will receive data synchronized from the master database.

: The following is the principle of master-slave replication:

  1. When starting a slave node, it will send a PSYNC command to the master node;
  2. If the slave node connects to the master node for the first time, a full replication will be triggered. At this point, the master node will start a background thread and start generating a RDB snapshot file;
  3. At the same time, all write commands newly received from the client client are cached in memory. After the RDB file is generated, the master node will send the RDB file to the slave node, and the slave node will first write the RDB file to the local disk, and then load it from the local disk into the memory ;
  4. Then the master node will send the write command cached in memory to the slave node, and the slave node will synchronize the data;
  5. If the network between the slave node and the master node fails and the connection is disconnected, it will automatically reconnect. After the connection, the master node will only synchronize some of the missing data to the slave node.

Interviewer : Yes, do you know the deletion strategy for expired keys?

: 1. passively deletes . When accessing the key, if it is found that the key has expired, the key will be deleted.

: 2. actively deleted . Clean up keys regularly. Each time you clean up, all DBs will be traversed in turn, and 20 keys will be randomly taken out from the DB. If they expire, they will be deleted. If 5 keys have expired, then continue to clean up this db, otherwise, start to clean up the next db.

: 3. Clear when the memory of is not enough. Redis has a maximum memory limit. The maxmemory parameter can be used to set the maximum memory. When the used memory exceeds the set maximum memory, the memory will be released. When the memory is released, the memory will be cleaned up according to the configured elimination strategy.

Interviewer : Very good, let's go here today


程序员大彬
468 声望489 粉丝

非科班转码,个人网站:topjavaer.cn