Java interview – hanging interviewer series redis Foundation



RedisInternet storage technology is so widely used that almost all back-end technology interviewers have toRedisIn terms of the use and principle of, make 360 ° difficulties for the small partners. As a bully who takes an offer once in a while in an Internet company(Allow me to use exaggerated rhetoric), I defeated countless competitors and could only see countless lonely figures every time. I left disappointed and felt a little guilty. On a lonely night, I learned from the pain and decided to start writingHanging interviewer series, I hope it can help readers to make a 360 ° counterattack to the interviewer, interrogate your interviewer, and interrogate the colleagues interviewed together(It doesn't seem very good), crazy harvest factory offer!

Interview start

A middle-aged man with a big belly and a plaid shirt came to you with a scratched Mac. Looking at his balding hair, he thought he must be NIMA’s top architect! But we are not afraid of poetry.

Hello, young man. Your resume says that redis is used in your project. Why do you use redis?

I can’t help scolding in my heart. What’s the problem? Everyone uses it, but you can’t say it.

Seriously replied:Hello, handsome and charming interviewer, because traditional relational databases such as MySQL can no longer be used in all scenarios, such as inventory deduction of second kill, peak access traffic on the app home page, etc., it is easy to break the database, so cache middleware is introduced. Redis and memcached are commonly used in the market, but their advantages and disadvantages are considered, Finally, redis was selected.

As for more detailed comparison, friends, remember to check the differences between redis and memcached, such as their advantages and disadvantages and their respective scenes. I will write them later when I have time.

Young man, let me ask you again, what data structures does redis have?

String, dictionary hash, list list, set, ordered set sortedset.

Here, I believe 99% of readers can answer the five basic data types of redis. If you can't answer, we'll make up lessons. You know, the five most suitable scenes are better.

However, if you are an advanced user of redis and you want to highlight the differences between you and other candidates in this interview, you also need to add the following data structures: hyperlog, geo, pub / sub.

If you still want to add points, you said you had played redis module, such as bloomfilter, redissearch and redis ml. at this time, the interviewer’s eyes began to shine and thought of this young manThere’s something

Note: when I answered questions related to redis in the interview, I often mentioned that bloomfilter has many use scenarios, and it is really fragrant. The principle is easy to understand. You can talk in front of the interviewer after reading the article. Isn't it fragrant? Lower conveyor door ↓

Bloomfilter, a sharp tool to avoid cache breakdown

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

If the expiration time of a large number of keys is set too intensively, redis may experience a short-term stuck phenomenon at the time point of expiration. In severe cases, cache avalanche will occur. We generally need to add a random value to the time to disperse the expiration time.

The e-commerce home page often uses scheduled tasks to refresh the cache. A large number of data failure times may be very concentrated. If the failure time is the same and a large number of users pour in at the failure time point, it may cause a cache avalanche

Have you ever used redis distributed lock? What is it?

First take setnx to scramble for the lock, and then add an expiration time to the lock with expire to prevent the lock from forgetting to release.

At this time, the other party will tell you that your answer is good, and then ask what happens if the process crashes unexpectedly or needs to restart maintenance before expire is executed after setnx?

At this time, you should give surprising feedback: Alas, yes, this lock will never be released. Then you needGrab your head and pretend to think for a moment, it seems that you think about the next result on your own initiative, and then answer: I remember that the set instruction has very complex parameters. This should be used to synthesize setnx and expire into one instruction at the same time!

At this time, the other party will show a smile and begin to Meditate: Well, this boy is not bad and is beginning to be a little interesting.

If there are 100 million keys in redis, of which 10W keys begin with a fixed known prefix, how can we find them all?

usekeysThe command can scan out the key list of the specified mode.

The other party then asked: if this redis is providing services to online businesses, what would be the problem with using the keys command?

At this time, you should answer a key feature of redis: the single thread 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 is executed. It can be used at this timescanInstructions,scanThe instruction can extract the key list of the specified mode without blocking, but there will be a certain repetition probability. It is OK to remove the duplicate once on the client, but the overall time will be longer than using the keys instruction directly.

However, incremental iteration commands are not without disadvantages: for example, using the smembers command can return all the elements currently contained in the set key, but for incremental iteration commands such as scan, because the key may be modified during incremental iteration of the key, Therefore, the incremental iteration command can only provide limited guarantee for the returned elements.

Have you ever used redis as an asynchronous queue? How do you use it?

Generally, the list structure is used as the queue,rpushProduction messages,lpopConsumer news. When there is no message in lpop, sleep properly and try again later.

If the other party asks, can I not use sleep?

List has another instruction calledblpop, when there is no message, it will block until the message arrives.

If the other party then asks whether it can produce once and consume many times?

Using the pub / sub topic subscriber mode, a 1: n message queue can be implemented.

If the other party continues to ask, what are the disadvantages of pub / sub?

When consumers go offline, production messages will be lost, and professional message queues such as rocketmq have to be used.

If the other party asks how redis implements the delay queue?

I guess now you want to beat the interviewer to death(The interviewer wanted to kill himself. Why did he ask so many questions that he didn't know), if you have a baseball bat in your hand, but you are very restrained. Calm down the excited heart, and then reply with a calm look: use sortedset, take the timestamp as the score, and the message content as the key to call zadd to produce the message. The consumer uses the zrangebyscore instruction to obtain the data polling before N seconds for processing.

Here, the interviewer has secretly given you a thumbs up. And has silently given you a +, but what he doesn't know is that at the moment, you put up your middle finger behind the chair.

How is redis persistent? How do service master-slave data interact?

RDB performs image full persistence and AOF performs incremental persistence. Because RDB takes a long time and is not real-time enough, it will lead to a large amount of data loss during shutdown, so AOF is required. When the redis instance is restarted, the RDB persistence file will be used to rebuild the memory, and then AOF will be used to replay the recent operation instructions to achieve a complete recovery of the state before the restart.

It's easy to understand here. RDB is understood as the total amount of data in a whole table. AOF is understood as the log of each operation. When the server restarts, first get all the data in the table, but it may not be complete. If you play back the log, the data will be complete. However, redis's own mechanism is that when AOF persistence is enabled and there are AOF files, AOF files are loaded first; When the AOF is closed or the AOF file does not exist, load the RDB file; After loading AOF / RDB files, redis starts successfully; When there is an error in the AOF / RDB file, redis fails to start and prints an error message

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, synchronize the disk during each write instruction, and data will not be lost. However, under the requirements of high performance, it is unrealistic to sync every time. Generally, timed sync is used, such as 1S1 times. At this time, 1s of data will be lost at most.

What is the principle of RDB?

Just give two words, fork and cow. Fork means that redis performs RDB operations by creating child processes. Cow means copy on write. After a child process is created, the parent and child processes share data segments. The parent process continues to provide read-write services, and dirty page data will be gradually separated from the child process.

Note: when answering this question, if you can also tell the advantages and disadvantages of AOF and RDB, I think I'm an interviewer. I'll give you praise (implied praise) on this question. In fact, there is a big difference between the two, and it involves the data synchronization of redis cluster, etc. Friends who want to know can also leave messages. I will write a special article to introduce them.

What are the advantages of pipeline? 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 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.

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 makes a bgsave and records the subsequent modifications to the memory buffer. After the synchronization is completed, the master node synchronizes the full amount of RDB files to the replication node. After the replication node accepts it, it loads the RDB image into memory. After loading, notify the master node to synchronize the modified operation records to the replication node for replay to complete the synchronization process. Subsequent incremental data can be synchronized through the AOF log, which is somewhat similar to the binlog of the database.

Have you ever used redis cluster? How to ensure the high availability of the cluster? What is the principle of the cluster?

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. When a single redis is short of memory, it uses cluster for fragment storage.

End of interview

Young man, you can. When do you have time to come to work, or come tomorrow?

You pretend to be calm. I’m in such a hurry. I still need to rent a house. Why don’t you come next Monday.

OK, I wonder if there are a lot of offers for this boy. I have to ask HR to add money to him.

You can’t help but praise yourself when you can last(It's time to praise)。


During the technical interview, whether it's redis or any question, if you can give practical examples, or directly say the questions and achievements of your development process, you will give the interviewer a lot of impression, and the answers should be more logical. Don't be east and West. It's easy to get dizzy.

Another point is that I asked you why you use redis. You don’t answer the question directly as soon as you come up. You can answer this:

Hello, handsome interviewerFirst of all, our project DB has encountered a bottleneck, especially in the scenario of second kill and hot data. DB basically can’t carry it, so we need to add cache middleware. At present, some cache middleware on the market include redis and memcached, their advantages and disadvantages… Combine these, and then combine with the characteristics of our project. Finally, who do we choose in technology selection.

If you answered my questions in such an orderly and reasonable way and said so many knowledge points beyond my questions, I will think you are not just a person who can write code. Your logic is clear, and you have your own understanding and thinking about technology selection, middleware and projects. In short, your offer has a play.

Well, that’s all the content of this article. I’ll keep updating it laterJava interview hanging seriesArticles related to Java technology stack. If you have anything you want to know, you can also leave a message to me after you like it. I will write it as soon as I have time, and we can make progress together.

Author: Ao Bing


Source: Nuggets

The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

This work adoptsCC agreement, reprint must indicate the author and the link to this article