What data structures Redis has
String, Hash, List, Set, SortedSet.
I’m sure 99% of readers will be able to answer the five basic data types of Redis. If you can’t answer the question, we have to make up for the lesson. You know the five types of best scenarios are better.
However, if you are an advanced Redis user, and you want to differentiate yourself from other candidates in this interview, you need to add the following data structures: HyperLogLog, Geo, Pub/Sub.
If you want to add a bonus point, say you’ve also played with Redis Modules like BloomFilter, RedisSearch, and Redis-ML
If a large number of key expiration times are set too centrally, Redis may experience a temporary lag at that point in time. Severe caching avalanches occur, and we generally need to add a random value to the time to spread out the expiration time.
E-commerce home pages often use scheduled tasks to refresh the cache. A large number of data may have a concentrated expiration time. If the expiration time is the same and a large number of users flood in at the expiration time, the cache avalanche may occur
Redis distributed lock
Use setnx to fight for locks, and use expire to add an expiration time to locks in case they are forgotten to release.
What happens if a process crashes unexpectedly or needs to restart maintenance after executing EXPIRE after setnx?
The set directive has very complex parameters. This directive can be used by combining setnx and EXPIRE together.
How does Redis implement delay queue?
With sortedSet, time stamp is used as score, message content is used as key to call Zadd to produce messages, and consumers use ZrangeByScore command to obtain data polling N seconds ago for processing.
How is Redis persisted? How does master/slave data interact?
RDB does mirrored full persistence and AOF does incremental persistence. Because RDB takes a long time and is not real-time enough, a large amount of data will be lost during downtime, so AOF is needed to cooperate with RDB. When the Redis instance is restarted, the memory is rebuilt using the RDB persistence file, and the AOF is used to replay the recent operation instructions to achieve a full restoration of the pre-restart state.
RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB = RDB However, the mechanism of Redis itself is that when AOF persistence is enabled and AOF files exist, AOF files are loaded first. When AOF is closed or the AOF file does not exist, load the RDB file. After loading AOF/RDB file city, Redis starts successfully. Redis fails to start and prints error messages when there are errors in the AOF/RDB file
What happens if the machine suddenly loses power?
Depending on the configuration of the SYNC property of the AOF log, if performance is not required, sync the disk with each write instruction and data will not be lost. However, it is unrealistic to sync every time due to high performance requirements. Sync is usually timed, such as 1S1 times, and at most 1s of data will be lost.
What is the principle of RDB?
The fork and the cow. Fork means that Redis creates a child process to perform RDB operations, cow means copy on write. After the child process is created, the parent process shares data segments, and the parent process continues to provide read and write services. The dirty page data is gradually separated from the child process.
What are the benefits of pipelines and why do you use them?
Multiple IO round trips can be reduced to one, provided that there is no causal correlation between the instructions executed by the pipeline. When using Redis-Benchmark for pressure measurement, it can be found that an important factor affecting the PEAK QPS of Redis is the number of pipeline batch instructions.
Redis synchronization mechanism
Redis can use master slave synchronization and slave slave synchronization. During the first synchronization, the primary node performs a BGSave and records subsequent modifications to the memory buffer. After the synchronization is complete, the RDB file is fully synchronized to the replication node, and the replication node loads the RDB image to the memory. After the loading is complete, the master node is notified to synchronize the modified operation records to the replication node for replay. Subsequent increments are synchronized through an AOF log, similar to a database’s binlog.