Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Redis interview skills

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Preface Redis is so widely used in Internet technology storage that almost all back-end technical interviewers have to make it difficult for friends in terms of the use and principle of Redis. As a face-to-face offer bully in an Internet company (please allow me to use exaggerated rhetoric), I beat countless competitors, and each time I could only see countless lonely figures leaving in disappointment and felt a little guilty. on a lonely night, I learned from the bitter experience and decided to start writing the interviewer series, hoping to help readers to fight back against the interviewer in the future. Ask your interviewer, the colleague you interviewed with (it doesn't seem to be very good), the crazy harvest factory offer! At the beginning of the interview, a fat-bellied middle-aged man in a plaid shirt came up to you with a scratched mac, looked at his bald hair and thought he must be Nima's top architect. But we are full of poetry and book spirit, and the emptiness is not empty. Hello, young man. Your resume says that you used Redis in your project. Why do you use Redis? In my heart, I can't help scolding, what's the problem? we all use this, but you can't say it. Answer seriously: Hello, handsome and charming interviewer, because the traditional relational database, such as Mysql, is no longer suitable for all scenarios, such as the inventory deduction of seconds, the peak of access to the home page of APP, and so on, which are easy to crash the database, so caching middleware is introduced. At present, the more commonly used caching middleware in the market are Redis and Memcached, but neutralization considered their advantages and disadvantages, and finally chose Redis. As for more detailed comparisons, friends remember to check out the differences between Redis and Memcached, such as the comparison of their advantages and disadvantages and their respective scenarios, which I will write later when I have time. That young man, let me ask you again, what data structures does Redis have? String String, dictionary Hash, list List, collection Set, ordered collection SortedSet. Here I believe 99% of readers can answer the five basic data types of Redis. If we can't answer, we have to make up lessons. We all know that the five types of scenes that are the most suitable are better. However, if you are a medium and advanced Redis user, and you want to highlight the differences between you and other candidates in this interview, you also need to add the following data structures: HyperLogLog, Geo, Pub/Sub. If you still want to get extra points, you say you have played Redis Module, such as BloomFilter,RedisSearch,Redis-ML, and then the interviewer's eyes begin to shine, thinking that this young man has something. Note: when I answered questions related to Redis in the interview, I often mentioned that BloomFilter (Bloom filter) is really used in many scenarios, and it is really fragrant to use, and the principle is easy to understand. If you take a look at the article, you can talk eloquently in front of the interviewer, isn't it? Lower portal ↓ to avoid cache breakdown of the BloomFilter if there are a large number of key need to be set to expire at the same time, what do you need to pay attention to? If a large number of key expiration times are set too centrally, redis may experience temporary stutters at that point of expiration. If it is serious, there will be a cache avalanche, and we generally need to add a random value to the time to spread the expiration time. The e-commerce home page often uses scheduled tasks to refresh the cache, and a large amount of data expiration time may be very concentrated. If the expiration time is the same and a large number of users pour in at the same time, it may cause a cache avalanche. Have you ever used a Redis distributed lock? what is it? First use setnx to scramble for the lock, and then use expire to add an expiration time to the lock to prevent the lock from forgetting to release. At this point, the other person will tell you that you have a good answer, and then ask what happens if the process accidentally crash or restarts maintenance before expire is executed after setnx. At this point you should give surprising feedback: Oh, 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 instruction has very complex parameters, which should be able to combine setnx and expire into one instruction at the same time! At this time, the other party will show a smile, the heart began to say: well, this kid is not bad, starting to have some fun. If there are 100 million key in a Redis, of which 10w key start with a fixed known prefix, how do you find them all? Use the keys directive to scan out the key list for the specified mode. The other party then asked: if this redis is providing services to online business, what is the problem with using the keys instruction? At this point you have to answer one of the key features of redis: the single-threaded nature of redis. The keys instruction will cause the thread to block for a period of time, and the online service will stop until the instruction has been executed before the service can resume. At this time, you can use the scan instruction. The scan instruction can extract the key list of the specified mode without blocking, but there will be a certain probability of repetition. It can be deduplicated on the client side, but the overall time will be longer than using the keys instruction directly. However, the incremental iteration command is not without its drawbacks: for example, using the SMEMBERS command can return all the elements currently contained in the collection key, but for incremental iteration commands such as SCAN, because the key may be modified during the incremental iteration of the key, the incremental iteration command can only provide limited guarantees for the elements returned. Have you ever used Redis as an asynchronous queue? how do you use it? Generally, list structure is used as a queue, rpush produces messages, and lpop consumes messages. When there is no message from lpop, sleep appropriately and try again later. If the other party asks whether it is possible not to use sleep? List also has an instruction called blpop, which blocks until the message arrives when there is no message. What if the other party goes on to ask if it can be produced and consumed more than once? Using the pub/sub topic subscriber mode, you can implement message queuing for 1VR N. If the other party continues to ask pub/su b what are the disadvantages? In the case of consumers offline, the produced messages will be lost, and professional message queues such as RocketMQ will have to be used. If the other party asks TM how to implement the delay queue in Redis? I guess now you want to beat the interviewer to death (the interviewer wants to kill himself. Why do you ask so many things you don't know?), if you have a baseball bat, but you are restrained. Calm the excited heart, and then calmly replied: use sortedset, take the timestamp as the score, the message content as the key call zadd to produce the message, and the consumer uses the zrangebyscore instruction to obtain the data polling before N seconds for processing. At this point, the interviewer has secretly given you a thumbs up. And he has silently given you the aura, but what he doesn't know is that at the moment you put up your middle finger, behind the chair. How does Redis persist? How does the master-slave data interact with each other? RDB persists the image in full, and AOF does incremental persistence. Because RDB will take a long time, not real-time, and lead to a lot of data loss during downtime, AOF is needed to cooperate with it. MT5 uses tutorial www.gendan5.com/mt5.html to rebuild memory using RDB persistence files when the redis instance is restarted, and then use AOF to replay recent instructions to fully restore the state before restart. It is easy to understand here. RDB is understood as a whole table of data, and AOF is understood as a log for each operation. When the server restarts, get all the data in the table first, but it may not be complete. If you play back the log, the data will be complete. However, the mechanism of Redis itself is that when AOF persistence is enabled and the AOF file exists, the AOF file is loaded first; when the AOF is closed or the AOF file does not exist, the RDB file is loaded; after loading the AOF/RDB file city, the Redis starts successfully; when there is an error in the AOF/RDB file, the Redis fails to start and prints the error message and asks what happens if the machine suddenly loses power? Depending on the configuration of the AOF log sync property, if performance is not required, sync the disk as each instruction is written, and no data will be lost. However, it is not realistic to sync every time under the requirement of high performance. Timing sync is generally used, such as 1s1 times. At this time, a maximum of 1 second of data will be lost. The other party asked what is the principle of RDB? Just give me two words, fork and cow. Fork means that redis performs RDB operations by creating child processes, and cow refers to copy on write. After the child process is created, the parent process shares the data segment, and the parent process continues to provide read and write services. The dirty page data will be gradually separated from the child process. Note: when answering this question, if you can still talk about the advantages and disadvantages of AOF and RDB, I think I will give you a like on this question. In fact, there is a big difference between the two, and it involves the problem of data synchronization in Redis clusters, and so on. Friends who want to know can also leave a message. I will write a special article to introduce it. What are the benefits of Pipeline, and why use pipeline? The time for multiple IO round trips can be reduced to one, as long as there is no causal correlation between the instructions executed by pipeline. When using redis-benchmark for pressure testing, it can be found that one of the important factors affecting the QPS peak of redis is the number of pipeline batch instructions. Do you know the synchronization mechanism of Redis? Redis can use master-slave synchronization and slave synchronization. In the first synchronization, the master node does a bgsave, and records the subsequent modification operations to the memory buffer. After the completion, the RDB files are synchronized to the replication node. After the replication node accepts, the RDB image is loaded into memory. After the loading is completed, the synchronization process is completed by informing the master node to synchronize the operation records modified during the period to the replication node for playback. The subsequent incremental data can be synchronized through the AOF log, similar to the binlog of the database. Have you ever used Redis clusters? how to ensure the high availability of clusters? what is the principle of clusters? Redis Sentinal focuses on high availability and automatically promotes slave to master in case of master downtime and continues to provide services. Redis Cluster focuses on scalability and uses Cluster for sharding storage when a single redis runs out of memory. The interview is over, young man, you can, when do you have time to come to work, why not come tomorrow? You pretend to be calm. I'm in such a hurry. I still need to rent a house. Maybe next Monday. Good thought this kid so NB is not a lot of Offer in hand, no, I have to ask hr to give him more money. If you can make it to the end, you can't help but give yourself a like (hint of like, don't like it every time, do you want to visit me for nothing? You guys are so bad, but I like it. Summary in the technical interview, whether it is Redis or any questions, if you can cite practical examples, or directly say that the questions and gains of your development process will give the interviewer a lot of impression, the answer will be more logical, not a little bit east or west, it is easy to make yourself dizzy. Another point is that when I ask you why you use Redis, you don't have to answer questions directly. You can answer like this: Hello, handsome interviewer. First of all, our project DB has encountered a bottleneck, especially in scenarios such as second kill and hot data. DB basically can't handle it, so we need to add cache middleware. At present, some cache middleware on the market have Redis and Memcached, their advantages and disadvantages. Synthesize these, and then combine the characteristics of our project, and finally who we choose in the technology selection. If you answer my question methodically and justifiably and say so many knowledge points outside my question, I will think that you are not only a person who can write code, your logic is clear, you have your own understanding and thinking about technology selection, middleware and project, to put it bluntly, your offer has a chance.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 217

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report