Source: Old Money high-availability architecture
Guidance: Redis-related knowledge is a frequently asked topic during a programmer's interview. As a veteran technical interviewer who has hit hundreds of thousands of people in the Internet technology industry, the author summarizes the frequently asked questions in the interview process. is well worth reading.
Author Profile: Chan Wenping (Old money), the Internet, distributed high-concurrency technology for ten years veteran, is currently the palm of the technology senior back-end engineers. Proficient in the use of Java, Python, Golang and other computer languages, developed the game, made a website, wrote a message push system and MySQL middleware, implemented open source ORM framework, WEB Framework, RPC framework, etc.
Redis is so widely used in Internet technology storage that almost all of the back-end technology interviewers have to make a variety of challenges to their small partners in the use and rationale of Redis. As a in the Internet technology industry hit hundreds of "Please allow me to exaggerate" senior technical interviewer, saw countless lonely figure disappointed to leave, slightly feel guilty, so offer this article, hope you readers after interview irresistible force, never fail!
What data structures do Redis have?
Strings string, dictionary hash, list of lists, set set, ordered set SortedSet.
If you are an advanced Redis user, you will also need to add the following data structures Hyperloglog, Geo, Pub/sub.
If you say you've played the Redis Module, like Bloomfilter,redissearch,redis-ml, the interviewer's eyes start to shine.
Have you ever used a Redis distributed lock, what is it?
First take SETNX to grab the lock, after the grab, then use expire to lock plus an expiration time to prevent the lock forgot to release.
This time the other person will tell you that you answered well, and then ask if the process unexpectedly crash or restart maintenance before SETNX executes expire.
At this time you have to give surprise feedback: Alas, yes, this lock will never be released. Then you need to catch your own head, pretending to think for a moment, as if the next result is you think it out, and then answer: I remember the set command has very complex parameters, this should be able to setnx and expire synthesis of a command to use! The other side will reveal a smile, the heart began to meditate: "Press, this boy is good."
If there are 100 million keys in Redis, where 10w keys start with a fixed known prefix, if they are all found?
Use the keys command to sweep out the key list for the specified pattern.
The other side went on to ask: if this redis is serving the online business, what's wrong with using the keys command?
At this point you will answer Redis's key feature: Redis's single-threaded. The keys command causes the thread to block for a period of time, and the online service pauses until the instruction is executed before the service can recover. This time you can use the Scan command, scan instructions can be unblocked to extract the specified mode of the key list, but there will be a certain repetition probability, the client to do one time to the weight can be, but the overall time spent than directly with the keys command length.
Do you use Redis as an asynchronous queue?
Generally use the list structure as a queue, Rpush production messages, Lpop consumer messages. When Lpop has no message, try again with a proper sleep.
If the other side of the inquiry can not sleep it? The list also has an instruction called Blpop, which will block until the message arrives when there is no message.
If the other side cross-examine can produce a consumption many times? With the Pub/sub theme subscriber pattern, you can implement 1:N Message Queuing.
If the other side cross-examine pub/sub have any shortcomings? In the case of the consumer offline, the production of the message will be lost, the use of professional Message Queuing such as RABBITMQ.
If the other side asked Redis how to implement the delay queue? I guess now you're trying to beat the interviewer to death. If you have a baseball bat in your hand, how to ask so much detail. But you are very restrained, and then the demeanor of the answer: using SortedSet, take the timestamp as score, the message content as a key call Zadd to produce the message, the consumer with the Zrangebyscore command to get n seconds before the data polling processing.
Here, the interviewer secretly has a thumbs up for you. But what he doesn't know is that now you're putting the middle finger behind the chair.
If you have a large number of keys that need to be set to expire at the same time, what do you generally notice?
If a large number of key expiration times are set too high, Redis may have a short lag when it expires at that point in time. It is generally necessary to add a random value to the time to make the expiration time scattered.
How does Redis do persistence?
Bgsave do the full amount of image persistence, aof do incremental persistence. Because Bgsave will take a long time, not enough real-time, in the downtime will lead to a large number of lost data, so need to aof to use. When a Redis instance restarts, the AOF is preferred to restore the state of memory, and if there is no aof log, the Rdb file is used to recover.
What if I ask aof file too much recovery time? You tell the interviewer that Redis will periodically do aof rewrite, compressing the aof file log size. If the interviewer is not satisfied, and then come up with the killer answer, Redis4.0 after the hybrid persistence of the function, the bgsave of the full amount and aof of the increment to do a fusion process, so that both the efficiency of the recovery and the security of the data. This feature is not even known to many interviewers, and they are sure to impress you.
What if the other side was asking if the machine would lose power? Depending on the configuration of the AoF log Sync property, if performance is not required, sync the disk at each write instruction without losing data. But in the high-performance requirements of every time sync is unrealistic, generally use timing sync, such as 1s1 times, this time the maximum loss of 1s of data.
Pipeline what is the advantage, why use pipeline?
Multiple IO round trips can be reduced to one time, provided there is no causal correlation between the instructions executed by the pipeline. A significant factor that affects the QPS peaks of Redis is the number of pipeline batch instructions that can be found when using Redis-benchmark for pressure measurement.
Do you know about the Redis synchronization mechanism?
Redis can use master-slave synchronization, from synchronization. At the first synchronization, the primary node does a bgsave, and the subsequent modifications are logged to the memory buffer, and the Rdb file is synchronized to the replication node after completion, and the RDB image is loaded into memory after the replication node accepts completion. When the load is complete, the synchronization process is completed by notifying the primary node that synchronization of the modified action records to the replication node is synchronized.
Have you ever used a Redis cluster, and what is the principle of clustering?
The Redis sentinal is focused on high availability and automatically promotes slave to master when Master is down and continues to serve.
Redis cluster focuses on extensibility and uses cluster for shard storage when a single redis memory is low.
If there is no mention of the interview questions, welcome to organize and reply!