Redis interviews make things difficult

Redis is so widely used in Internet technology storage that almost all back-end technical interviewers have to make things difficult for small partners in the use and principle of Redis. As a senior technical interviewer who has attacked hundreds of thousands of [please allow me to exaggerate] in the Internet technology industry, I have seen countless lonely figures leaving in disappointment, and I feel a little guilty, so I offer this article, I hope all readers In the future, the interview will be like a broken bamboo, and you will never fail!

What data structures does Redis have?

String String, Dictionary Hash, List List, Collection Set, Sorted Set SortedSet.

If you are an advanced user of Redis, you need to add the following data structures HyperLogLog, Geo, and Pub/Sub.

If you say that you have played Redis Module, like BloomFilter, RedisSearch, Redis-ML, the interviewer's eyes will start to light up.

Have you used Redis distributed lock, what is it?

First use setnx to grab the lock, and after grabbing it, use expire to add an expiration time to the lock to prevent the lock from forgetting to release.

At this time, the other party will tell you that you answered well, and then ask what happens if the process crashes unexpectedly or restarts maintenance before executing expire after setnx?

At this time, you have to give surprising feedback: alas, yes, this lock will never be released. Then you need to scratch your head, pretend to think for a moment, as if the next result is your own initiative, and then answer: I remember that the set command has very complex parameters, this should be able to setnx and expire at the same time. Synthesize one instruction to use! The other party will show a smile at this time, and he will start to meditate in his heart: Press, this kid is not bad.

Suppose there are 100 million keys in Redis, and 10w keys start with a fixed and known prefix. How to find them all?

Use the keys command to scan out the key list of the specified mode.

The other party then asked: If this redis is providing services for online businesses, what is the problem with using the keys command?

At this time, you have to answer a key feature of redis: redis's single thread. The keys instruction will cause the thread to block for a period of time, and the online service will be suspended until the instruction is executed, and the service can be resumed. At this time, you can use the scan command. The scan command can extract the key list of the specified mode without blocking, but there will be a certain probability of repetition. You can do it once on the client side, but the overall time spent will be longer than the direct use. The keys command is long.

Have you used Redis for asynchronous queues and how did you use them?

Generally, the list structure is used as a queue, rpush produces messages, and lpop consumes messages. When there is no message from lpop, sleep properly for a while and try again.

If the other party asks if it is okay not to sleep? list also has an instruction called blpop, which blocks until a message arrives when there is no message.

If the other party asks whether it can be produced once and consumed many times? Using the pub/sub topic subscriber pattern, a 1:N message queue can be implemented.

If the other party asks what are the disadvantages of pub/sub? In the case of consumers offline, the produced messages will be lost, and professional message queues such as rabbitmq must be used.

If the other party asks how redis implements the delay queue? I guess right now you want to slap the interviewer to death. If you have a baseball bat in your hand, why are you asking so much detail. But you are very restrained, and then replied calmly: use sortedset, use the timestamp as the score, the message content as the key to call zadd to generate the message, and the consumer uses the zrangebyscore command to obtain the data polling N seconds ago for processing.

At this point, the interviewer has secretly given you a thumbs up. But what he didn't know was that at the moment you held up your middle finger, behind the chair.

If there are a large number of keys that need to be set to expire at the same time, what should be paid attention to?

If a large number of key expiration time settings are too centralized, at the time of expiration, redis may experience a short-term freeze. Generally, a random value needs to be added to the time to make the expiration time scattered.

How does Redis do persistence?

bgsave does full image persistence, and aof does incremental persistence. Because bgsave will take a long time, it is not real-time enough, and will cause a lot of data loss during downtime, so aof is needed to cooperate. When the redis instance restarts, it will use the bgsave persistent file to rebuild the memory, and then use aof to replay the recent operation instructions to fully restore the state before the restart.

The other party asked what would happen if the machine suddenly lost power? Depending on the configuration of the aof log sync attribute, if performance is not required, the disk will be synced on every write command without data loss. However, it is unrealistic to sync every time under the requirement of high performance. Generally, timing sync is used, such as 1s1 times, at most 1s of data will be lost at this time.

The other party asked what is the principle of bgsave? You can give two words, fork and cow. Fork means that redis performs the bgsave operation by creating a child process, and cow means copy on write. After the child process is created, the parent and child processes share the data segment, the parent process continues to provide read and write services, and the dirty page data will gradually be separated from the child process. Come on.

What are the benefits of Pipeline and why use Pipeline?

The time of 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 stress testing, it can be found that an important factor affecting the QPS peak of redis is the number of pipeline batch instructions.

Do you understand the synchronization mechanism of Redis?

Redis can use master-slave synchronization and slave-slave synchronization. During the first synchronization, the master node performs a bgsave, and records subsequent modification operations to the memory buffer at the same time. After completion, the rdb file is fully synchronized to the replication node. After the replication node accepts it, the rdb image is loaded into the memory. After the loading is completed, the master node is notified to synchronize the operation records modified during the period to the replica node for replay, and the synchronization process is completed.

Have you used Redis cluster and what is the principle of clustering?

Redis Sentinal focuses on high availability. When the master goes down, it will automatically promote the slave to the master and continue to provide services.

Redis Cluster focuses on scalability and uses Cluster for sharded storage when a single redis memory is insufficient.

Reprinted from: https://mp.weixin.qq.com/s/507jyNbL4xCkxyW6Xk15Xg

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612447&siteId=291194637