Hello! Hi, I’m Xiaoqi, an unreliable programmer, and Xiaoqi is going to share some technology in a light-hearted and humorous way. If you think you’ve learned something from Xiaoqi’s article, please give Xiaoqi a thumbs up
One, foreword
As a Java programmer, some of the underlying principles of Redis are a skill point that we don’t need to learn to move bricks, but why does Xiaoqi need to talk about it? Is it to waste everyone’s 1 minute of precious time, a person 1 minute, 500,000 people is 1 year, 50 million people is 100 years, earn, small strange with his own success to hang a person (blood earn).
Of course not, and xiaoqi’s article is not so many people read, at most also waste a kidney bar.
Learn the basic principles of Redis because the interviewer is asking! So we have to learn, what? Don’t you learn what isn’t practical? The neighbor xiaoqi can be hard to learn, when the interviewer as long as xiaoqi do not want you.
As for why the interviewer asks about the underlying principles of Redis, well… I’ll leave it up to you the next time you go into an interview and the interviewer asks, “Talk about the fundamentals of Redis.” You: “Hello, interviewer, may I ask why you want to ask the basic principle of Redis? You give me a computer and I will build a library management system for you in five minutes. Won’t it smell good? The interviewer will tell you the answer, and you can type your answer in the comment section to let Xiaoqi and other friends know exactly why you are asking.
Second, the interview
On a sunny Sunday, I went to the park, a strange (don’t ask why is on Sunday, q) is 997, but in order to fill the stomach of the working people, only knowing that mountain tiger, race row), sitting in a strange room, waiting for HR little sister to call the interviewer, at this time my mood as mixed as your friend, will worry that the interviewer asks hard to? What should I do about my knowledge blind spot? Do you want to brag about my relationship with Kiki when we introduce ourselves later?
A handsome, eyes sharp the interviewer came in and saw the sharp, seemed to be able to see through all of his eyes, I don’t think or we’ll be 20 k, 8 k, the interviewer will not fool saw, but I think just before I came to see the small series of boring to learn programming, I have learned the essence of little odd, I immediately came to the confidence, decided to want 30K, not to learn small odd lai do not go (ha ha)
Interviewer: Xiaoqi, right? Did you bring your resume?
I: didn’t take, now color print two piece, my resume five piece, every time interview all want to spend ten piece, my friend said haven’t work first let you pay for the work don’t go.
Interviewer:… So what are you gonna do to get me to hire you
Me: Temperament?
(Instead of calling security, the interviewer brought out my waiting stick from behind the door. I lost my nerve.)
I had to pull out of my backpack the resume THAT I had taken that morning from interviewers at other companies. Morning interviewer: That’s all for today’s interview, go back and wait for the notice! Me: Hello, interviewer. If your company does not plan to hire me, could you please return my paper resume to me? I have an interview this afternoon. Morning interviewer: I said your resume how wrinkled, originally you have been recycling ah! How long have you had this symptom? Me: Half a month… (Only after I handed in my crumpled resume did the interview continue…)
How does Redis implement persistence
Interviewer: I see on your resume that you are proficient in Redis? (Hum, the interviewer gives a contemptuous smile.)
(Seeing the interviewer’s scornful smile, I pulled out my Redis book and shoved it at him.)
I: this book I recite like flow, you casually ask a question, answer not to come up calculate I lose, answer come up you have to apologize to me for your contempt.
(My smile grows confident…)
(The interviewer is looking at the book thoughtfully, and I suspect he’s wondering how well he knows the book.)
Interviewer: Well, how does Redis persist
Me: Redis has two main persistence modes, one is RDB (snapshot) mode and the other is AOF format.
Interviewer: Can you tell me the difference between them and how to configure them
So we can imagine that I’m going to record what a person’s posture looks like, so I can only record it by photographing it, or by describing it, right
I want to know what a person’s posture is, so I take a picture of him, and that’s his posture.
AOF can be understood as the description of actions. I can know the person’s posture by describing each action. For example, the person’s left hand is six, his right hand is seven, his left foot is round and his right foot is kicked, so I can know the person’s current posture by these actions.
Therefore, RDB snapshot is equivalent to saving the data in Redis. When recovering, you only need to take out the photo and restore it according to the posture. And AOF is equivalent to Redis in every execution command recorded down, recovery needs to be based on the command one by one, first left hand six, then right hand seven, then left foot circle, then right foot kick… (If you don’t understand, you can try to understand by following the posture)
RDB is configured in the redis.conf directory. The command format is save [time] [times].
For example, if there are at least 10000 changes in 60 seconds, it will be automatically saved once, that is, take a photo. Then if Redis dies after 500 changes, the 500 changes will not be found.
If executing the save command will affect the normal read and write of Redis, we can use bgSave (copy on write) command to generate RDB snapshot. Bgsave uses a child thread to implement snapshot function, and the main thread continues its read and write task
There would be no RDB use AOF to save the data in the snapshot Redis downtime of risk, because AOF is saved each command, but AOF every command is not only save time, it will cost performance, we can set to perform a save every 1 seconds, so even if it’s lost will only lose 1 second data
You can enable AOF by setting appendOnly to yes in the configuration file
Appendfsync always: saves every new command, slowest but safest. Appendsync everysec: saves the command every second, fast enough to lose 1 second of data when it fails. It never saves, handing the data to the operating system for processing. Faster and less secure, saving commands once per second is recommended
1. AOF rewrite
Interviewer: So many commands in AOF are too troublesome to recover. Is there any way to optimize it
There are too many useless instructions in AOF files, so AOF periodically generates AOF files based on the latest data in memory
For example, if we record a person’s movements and they raise their hand, then they lower their hand, then they raise their hand again, then we can combine the movements into one action: raise the hand, let go, raise the hand, because performing three actions is the same as performing only one action: raise the hand
The frequency of AOF rewrites can be set to auto-aof-rewrite-percentage 100 (rewrites are triggered if the AOF file size increases by 100% since the last rewrite).
Auto-aof -rewrite-min-size 64MB (aof files must be at least 64M before they are rewritten automatically)Aof can be rewritten manually by using the bgrewriteaof command, which also uses a subprocess and does not affect the normal redis command
2, RDB and AOF mixed use
Interviewer: Both RDB and AOF have their advantages and disadvantages. What should we choose
When Redis is started, if both RDB and AOF are configured, AOF is preferred, because AOF is more secure, but the performance is not good, but we can use a mixture of them to achieve better results
Mix RDB and AOF, for example, restore the appearance of the last photo record according to the photo first, and then restore the action recorded after the photo
Aof ‐ USE ‐ RDB ‐ Preamble YES
4. Redis master-slave architecture
Interviewer: Can you talk about the master-slave architecture of Redis and how to build it
Me: Yes, the master-slave architecture of Redis actually uses one Redis node to do the write operation (master node) and multiple Redis nodes to do the read operation (slave node). The master node will synchronize the written data to the slave node to ensure that the data read from the slave node is the latest data
Replicaof = replicaof = replicaof = replicaof = replicaof = replicaof = replicaof = replicaof = replicaof
After the slave node is configured, the slave node is started. At this time, the slave node will get the data from the master node for the first time
5. Redis Sentinel Architecture
Interviewer: Can you talk a little bit about redis sentinel architecture and how to build it
Me: Well, the Sentry architecture is a derivative of the master-slave architecture, because in the master-slave architecture if the master node dies, then we can’t write data, we can only read data from the slave node, which is very inconvenient.
So we have a sentinel cluster to monitor these nodes, and when the master node fails we have a sentinel to elect a slave node as the master node and let the write command continue (let’s use the famous village as an example…). .
Setup: Make a copy of the sentinel.conf file, which is available in Redis by default
Change the port number and the sentinel command in the format: Sentinel monitor < master node name > < port > quorum is a numeric type, meaning how many sentinels consider the master node to be failed before it is truly failed. For example, I configured three sentinels, So what I mean by 2 here is that there are two sentinels that consider the current primary node to have failed.
Interviewer: That’s a good guy. I don’t have any more questions for you. Do you have any more questions for me?
Me: Uh… Could the interviewer give me my paper resume? Could you please not write on my resume? I need to use it in the interview tomorrow.
Interviewer: What other companies to meet, come to me, the conditions open
Me: 100K then (the interviewer picks up his stick again)
Interviewer: If you don’t come, please recommend someone else to come to my office for an interview
I: you first good study Redis, today fortunately only I came, if it is small strange loyal readers came, you will be abused of very miserable. (I left my Redis Design and Implementation to the interviewer, turned around and left behind my handsome figure, while the interviewer was lonely and stupidly sitting there, as if a hundred million had left him…)
Six, summarized
The information about Redis has not been sorted out. The article will continue to be updated later. It is suggested to collect it.
The commands involved in the article must be typed several times like me, only in the process of typing can you find out whether you really master the command.
Give me a thumbs up if you think my article is good.