This is the seventh day of my participation in the First Challenge 2022. For details: First Challenge 2022.
This is the sixth installment of the series of articles on Redis.
The interview began
Interviewer: Let’s talk about Redis today
Interviewer: Everyone says that Redis is fast. Why is Redis so fast?
Dabin: Mainly for the following reasons:
- Memory-based: Redis uses memory storage with no overhead on disk IO. Data is stored in the memory, and the read and write speed is fast.
- Single-threaded implementation (pre-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, converting database operations into events without wasting too much time on network I/O.
- Efficient data structures: Redis optimizes the underlying data types for faster speeds.
Interviewer: Well, you said that Redis is single-threaded. Why do you think Redis chose single-threaded?
Emm, ask the Redis author…
Dabin: 1. Single-threaded implementations can avoid excessive context switching overhead. The program always runs in a single thread of the process, there is no multi-threaded switching scenario.
Dabin: 2, avoid the cost of synchronization mechanism: if Redis chooses the multi-threaded model, the problem of data synchronization needs to be considered, it will inevitably introduce some synchronization mechanism, which will lead to more overhead in the process of operating data, increase the complexity of the program at the same time will reduce the performance.
Dabin: 3, simple implementation, easy maintenance: if Redis uses multi-threaded mode, then all the underlying data structure design must consider thread safety, then the implementation of Redis will become more complex.
Interviewer: What are the application scenarios of Redis?
Dabin: Cache hot data to relieve the pressure on the database.
Dabin: Using the atomic increment operation of Redis, you can realize the function of counters, such as counting the number of users’ likes and visits.
Dabin: As a simple message queue, realize asynchronous operation.
Dabin: Speed limiter, can be used to limit the frequency of a user to access a certain interface, such as the second kill scenario used to prevent users from clicking unnecessarily.
Dabin: Friend relationship, using some commands of the set, such as intersection, union, difference set, etc., to achieve common friends, common hobbies and other functions.
Interviewer: Oh, how does Redis implement message queuing?
Monologue: My god, dig a hole for yourself…
Dabin: 1. Use lists. Let producers put tasks into lists using LPUSH and consumers keep pulling tasks out of lists using RPOP.
Dabin: 2. Publish and subscribe mode. Mq-like theme pattern. Only messages published after a subscription can be consumed, and one message can be consumed by multiple subscribers.
Dabin: 3. Delay queue. Using sortedSet, take time stamp as score, message content as key, call Zadd to produce messages, and consumers use ZrangeByScore command to obtain data polling N seconds ago for processing.
Interviewer: What is the principle of master slave replication in Redis?
Dabin: Well, the Redis replication function supports data synchronization between multiple databases. The primary database can perform read and write operations. When data changes in the primary database, data is automatically synchronized to the secondary database. A slave database is typically read-only and receives data synchronized from the primary database.
Dabin: Here’s how master-slave replication works:
- When a slave node is started, it sends one
PSYNC
Command to master node; - If the slave node is first connected to the master node, a full copy is triggered. The master node starts a background thread to start generating a copy
RDB
Snapshot file; - In addition, all new write commands received from the client are cached in the memory.
RDB
After the file is generated, the master node willRDB
The file is sent to the slave node, which sends the file to the slave node firstRDB
fileWrite to the local disk, and then load from the local disk into memory; - The master node then sends the write commands cached in memory to the slave node, which synchronizes the data.
- If the network between the secondary node and the primary node fails and the connection is disconnected, the primary node automatically reconnects to the secondary node. After the connection, the primary node only synchronizes some missing data to the secondary node.
Interviewer: Good. Do you know the deletion policy for expired keys?
Dabin: 1. Passive deletion. If the key has expired, the key will be deleted.
Dabin: 2, active deletion. Periodically clean the key. Each time clean the key will traverse all the DB in turn, and randomly take out 20 keys from the DB. If the key expires, delete it.
Dabin: 3. Clean up when the memory is insufficient. 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.
Interviewer: Great, that’s all for today