Interviewer: What are the methods of Redis cluster, and what is the principle of leader election?

Hello! Hello everyone, I'm Xiaoqi, an unreliable programmer.
Xiaoqi intends to share some techniques in a relaxed and humorous way. If you think you have learned something through Xiaoqi's article, then give Xiaoqi a like.
The article is continuously updated, you can search for [Xiaoqi JAVA Interview] on WeChat to read it as soon as possible, and reply to [Information] and there are more benefits I have prepared for you!

I. Introduction

As a Java programmer, some of the underlying principles of Redis are a skill point that we can move bricks without having to learn , but why does Xiaoqi talk about it? Is it just to waste 1 minute of everyone 's precious time, 1 minute for one person, 500,000 people is 1 year , 50 million people is 100 years , earning, Xiaoqi successfully hooked up with one person (blood earning).

Of course not, and Xiaoqi's article is not read by so many people, so it's a waste of a kidney at most .

Learning the underlying principles of Redis is because the interviewer wants to ask ! , so we have to learn, what? You don't learn if it's not practical? That neighbor, Xiaoqi, is going to study hard, and the interviewer only wants Xiaoqi not to want you.
insert image description here

As for why you asked why the interviewer asked about the underlying principles of Redis, this. . . I leave this opportunity to you. Next time you interview, the interviewer asks: "Tell me about the underlying principles of Redis". You: "Hello, interviewer, why are you asking about the underlying principles of Redis? You give me a computer, and I 'll set up a library management system for you in five minutes . Isn't it delicious? Let's see the real chapter on the keyboard." At this time, the interviewer will tell you the answer, you can type the answer in the comment area, and let Xiaoqi and many friends know why you are asking?

insert image description here

2. Interview

On a sunny Sunday, I came to an unfamiliar park (don't ask why it's a Sunday, it's 997, but in order to fill the stomach of migrant workers, I can only know that there are tigers in the mountains, and I tend to walk in the mountains), sitting in the unfamiliar In the conference room, I was waiting for Miss HR to call the interviewer. At this time, my mood was as mixed as your friends. I was worried that it would be difficult for the interviewer to ask? What should I do when asked about my knowledge blind spot? When I introduce myself for a while, do you want to talk about my relationship with Xiaoqi?

A handsome interviewer with sharp eyes walked in and saw his sharp eyes that seemed to be able to see through everything. I was thinking, otherwise I would not want 20k for a while, but 8k. It's so confusing, but I remembered that I just watched Xiaoqi's fun learning programming series before I came. I have completely learned the essence of Xiaoqi. I immediately got the confidence and decided to ask for 30k for a while, and learn Xiaoqi if I don't give it. Don't go (haha)

Interviewer: Xiaoqi, right, have you brought your resume?

Me: I didn't bring it. Now I have two pieces for color printing, and five resumes for me. Each interview costs ten dollars. My friend said that you should not go to a job that requires you to pay before you get a job.

Interviewer: . . . Then what do you rely on to conquer me and let me hire you

Me: Temperament?
Please add image description

(At this time, the interviewer did not call the security guard, but took out the stick that had been waiting for me for a long time from behind the door, and I was instantly cowardly)

insert image description here

I had to take out from my backpack the resume I asked for from other company interviewers in the morning, and this is the situation in the morning.

Interviewer in the morning: That's it for today's interview, go back and wait for the notice!

Me: Hello interviewer, if your company does not plan to admit me, can you return my paper resume to me? I have another interview in the afternoon.

The interviewer in the morning: I said why your resume is wrinkled, it turns out that you have been recycling it! How long has this symptom been present?

Me: It 's been half a month . . .

(The interview went on after I handed my crumpled resume to the interviewer...)

3. Redis Sentinel Cluster

Interviewer: I see that you are proficient in Redis on your resume? (Hmph, the interviewer smiles contemptuously)

(Looking at the interviewer's contemptuous smile, I couldn't help but take out my Redis book and push it to him)

Me: I have recited this book by heart. If you ask any question, if you can’t answer it, I will be considered a loser. If you answer it, you must apologize to me for your contempt.

insert image description here

(My smile gradually becomes more confident...)

insert image description here

(The interviewer is looking at the book thoughtfully at this time, I suspect he must be thinking about how well he knows the book)

Interviewer: Okay, let’s talk about what clustering methods Redis has?

Me: Redis mainly has two clustering methods, Sentinel cluster and Cluster (high availability) cluster.

Interviewer: Can you talk about the difference between the two and how to configure and use them?

Me: I already briefly talked about the sentinel mode in the last interview, and now I will take a brief look at it.
insert image description here

Here we can see that the client can only connect to one sentinel cluster, which means that the client can only write data through the address told by the sentinel cluster when writing data.

If the master node hangs, the sentinel elects a slave node to become the master node. During this period, the client's access is blocked, because the master node is being elected, and it is still unknown who is the master node and how to insert data.

So in order to solve this problem, we can use the high-availability cluster mode Cluster mode, which can also be called a cluster of multiple master-slave nodes (the master-slave nodes were mentioned in the previous chapter) mode cluster

4. Redis High Availability Cluster Cluster Mode

Interviewer: Can you talk about the specific process of the high-availability cluster mode and why it can solve the problem of waiting for elections?

Me: (Can I say no...)

insert image description here

It can be seen from the figure that our client can access multiple master nodes through the access portal. If one of the master nodes hangs, the other master nodes can work normally without being affected.

Interviewer: With so many master nodes, how do I know which master-slave structure I should put a data into?

Me: (Can you just put one at random... No, when you randomly put one and take it, you don't know which master-slave structure to go to, so you can't look it up in every master node)

Me: He will perform a hash calculation based on the key to be set. After the calculation, he will know which master node to store the value in. When he obtains it, he will also perform a hash calculation according to the key, and then he will know which master node to take. .

insert image description here

Interviewer: At this time, an age calculation hash is 150 which master node should be inserted

Me: It must be the second master node. The redis cluster adopts the sharding mode, and puts all data shards into multiple master nodes, which is convenient and horizontal expansion.

Interviewer: If the master node fails, the sentinel cluster will elect a master node in sentinel mode. In your high-availability mode, if a master node fails, how do you elect?

Me: (It should be according to the size...)

insert image description here

Interviewer: You are looking for a fight, right? The data is so big and small. . .

insert image description here

V. Principle of Leader Election

Me: At this time, other master nodes are needed to support the election. Let me describe it with a picture.

insert image description here

Me: We can think of these three master-slave nodes as three countries . If the leader of a country dies, then one of its forces will find other country leaders to support itself as the leader.

Me: Of course, the leaders of other countries will not see which of the two forces in your country is good or bad, but whoever finds me first will support you.

Me: And there are three countries in the picture, and the number of votes to support them must exceed more than half of the total number of countries, that is, at least two votes are required, so the redis cluster node must have at least three master-slave structures, otherwise two cannot be elected. The largest number of votes is not enough for more than half of the two countries.

insert image description here

Interviewer: Don't say that your kid is using this picture to speak vividly

Me: Are you talking nonsense? It’s a dick to learn programming if it’s not fun. . .

Interviewer: How do the Taliban and the regular army determine who will send the request first? Whoever sends the message first will win?

Me: The two small forces did not immediately send a request for support when the leader hangs up, but there is a certain delay time. This delay time is calculated according to a formula, which contains random numbers, and is calculated according to the size of SLAVE_RANK, SLAVE_RANK The size represents the Rank of the total amount of data synchronized by the slave node from the master node. The smaller the Rank, the newer the replicated data, and it can be the master node.

Delay formula: DELAY = 500ms +random(0 ~ 500ms) + SLAVE_RANK * 1000ms

Interviewer: If the network is shaking now, and the slave node cannot connect to the master node for a moment, he will start sending messages. After the network is restored, there will be two master nodes (commonly known as split-brain, the master node is equivalent to a master-slave structure. brain, there can only be one) point?

Me: Then we can configure the connection time to make him longer, and configure it through cluster-node-timeout. If it is configured for 5 seconds, then the slave node will not be able to connect to the master node within 5 seconds. Request support of information.

Interviewer: What if 5 seconds is not enough, what if my network jitters badly?

Me: Are you an auntie in the cafeteria? Your hands are shaking so badly. . .

insert image description here

Me: The situation you mentioned belongs to the fact that two master nodes are writing new data in an instant. Then after the network is restored, only one node will be used as the master node, and the other nodes will become slave nodes to synchronize. Some data is lost.

insert image description here
insert image description here

Me: Here we will find that the age 20 data is lost , so what should we do? We can configure min-replicas-to-write [number] , which is the number of slave nodes that write data at least synchronized.

If I configure 1 here, then if I write a value and at least one slave node synchronizes to this value, the write will be successful, otherwise the write will fail.

In this way, the above problems can be solved. When the network jitters the master node and cannot synchronize data to the slave node, the writing fails, and data loss will not be caused. However, this configuration greatly affects performance and is not recommended.

Interviewer: If all the master-slave structures in the redis cluster are down, can the redis cluster still be used?

Me: We can configure cluster-require-full-coverage to no , so that only one master-slave structure hangs up without affecting the use of other master-slave structures. If yes, a master-slave structure hangs the entire redis cluster and cannot be used. .

Interviewer: Why does Redis cluster need at least three master nodes, and the recommended number of nodes is an odd number?

Me: We said above that if there are two master nodes, then one of the master nodes hangs, and there is only one remaining master node , which has not reached more than half of the total number , and voting is not easy. The hung master node There is still no new master node to be elected.

In addition, if there are 4 master nodes, then he can only hang one master node at most like three master nodes. If two master nodes are hung at the same time, then the remaining live master nodes cannot exceed half of the total number, so according to In terms of cost performance, odd-numbered masternodes are more cost-effective, but if your company is a local tyrant, you can get as many masternodes as you want. . .

insert image description here

Interviewer: This guy is amazing, I don't have anything to ask here, what more questions do you have to ask (the interviewer's eyes are shining)

me: uh. . . Can the interviewer give me my paper resume? Can I not write and draw on my resume? I will use it for my interview tomorrow.

Interviewer: What other companies are you looking for? Just come to me. The conditions are open to you.

Me: 100k then (at this time the interviewer picked up the stick he prepared again)

Interviewer: If you don't come, give me a recommendation and let someone else come to me for an interview

Me: You should study Redis first. Fortunately, I am the only one here today. If Xiaoqi's loyal readers come, you will be abused miserably. (I left my "Redis Design and Implementation" to the interviewer, turned around and left a handsome back, and the interviewer sat there blankly, as if a hundred million had left him...)

6. Summary

The information about Redis here has not been sorted out, and the article will continue to be updated later. It is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

If you think my article is not bad, please like it. In addition, you can search [Xiaoqi JAVA Interview] on WeChat to read more good articles and get the information I have prepared for you.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324080348&siteId=291194637