Hello, interviewer | Ali is on two sides, and he is numb after being asked. .

This issue is the 21st issue of the [ Hello, Interviewer ] series of articles , and it is being updated continuously...

The "Hello, Interviewer" series has currently serialized 20 articles. It is said that friends who have read this series have all received offers from Dachang~

  • Welcome to star + subscription, and it is being updated continuously. . . Committed to creating a core interview strategy for school recruitment~
  • Java school recruitment interview website: www.java2top.cn

Test site quick check

This test involves, JVM, MySQL高级,Redis持久化

本期题改编自 ——2023届春招 阿里 实习 二面

interview site

ding ding ding...

Interviewer : "Hi, I'm XX interviewer, is this Xiaolong?"

Xiaolong : "Hi, interviewer, I'm Xiaolong"

Interviewer : "Okay, are you free now, let's start the interview"

Xiaolong : "Yeah, I'm ready"

other questions (self-introduction, project omitted)

Interviewer : "I see that you are familiar with your resume JVM. Will system.gc() trigger gc? What does it have to do with full gc?"

Monologue : "wc, I really haven't paid attention to this issue at ordinary times"

Xiaolong : " system.gc()Fullgc can be displayed and triggered to reclaim the old generation and the new generation, and release the memory occupied by useless objects. The bottom layer is called through Runtime . getRuntime() .gc()."

Xiaolong : "But this display call comes with a disclaimer. It just reminds the garbage collector of the JVM to execute Full Gc. Whether to execute it or not depends on the collector."

Interviewer : "Okay, do you understand Redis persistence?"

Xiaolong : "enen, that's how it is. Redis provides a persistence method to ensure that data can be recovered after an outage. RDB and AOF can be used for persistence. After Redis4.x, it supports RDB and AOF hybrid persistence."

Interviewer : "What about the AOF persistence mechanism?"

Xiaolong : "To put it simply, the AOF file saves the write operation commands in the agreed format, and the data can be recovered if the commands are executed sequentially in case of downtime; and it adopts the "log after write " mode, that is, Redis executes the commands first to write to the memory , and then write the log;”

Interviewer : "Why use post-logging?"

Xiaolong : "Well, first of all, Redis executes the command and writes the memory first, so that the command can be pre-checked to prevent the wrong command from being recorded in the log; at the same time, when recording the AOF log, there is no need to check the syntax, so the post-write log can also avoid additional overhead .”

Xiaolong : "Also, since the log is written after the command is executed, it will not block the main process's write operation."

Interviewer : "Just now you said that AOF is always appending commands. As time goes by, won't this AOF file become bigger and bigger? How does Redis handle it?"

Xiaolong : "Redis provides an AOF rewriting mechanism in order to prevent AOF files from getting bigger and bigger."

Monologue : "The classic analysis of Xiaolong's interview is fully recorded in [Interview Notes] , which is summarized in detail."

Interviewer : "What is the rewriting mechanism like?"

Xiaolong : "To put it simply, first of all, you need to know the write operation commands stored in the AOF, but the possible effects of many execution commands can be realized in the end with only a few commands."

Xiaolong : "Rewriting means replacing redundant commands with new ones, then recording the new commands into a new AOF file, and finally replacing the old AOF file."

Interviewer : "If there is a large amount of writing, the thread will be blocked for a long time. Redis is single-threaded, so it cannot process the client's request command. What should I do?"

Xiaolong : "The main process rewrites the aof log by forking a bgrewriteaof child process. Due to the rewriting of the newly opened process in the background, the main process can continue to process command requests and avoid blocking the main process."

Xiaolong : "However, the operation of forking a child process is also implemented by the main process. Since the data structure such as the page table of the parent process needs to be copied, if the page table is too large, the main thread may be blocked."

Interviewer : "Then if the parent process processes a new command when you are writing the original data, will it cause data inconsistency between the child and parent processes?"

Monologue : "This is a pretense~"

Xiaolong : "When the sub-process rewrites the log, if new data is written, the main process will write the command to the " AOF buffer " and " AOF rewrite buffer " after executing the command"

Xiaolong : "Then, after the child process is rewritten, it will asynchronously send a signal to the main process;"

Xiaolong : "When the main process receives the information, it appends all the content in the AOF rewrite buffer to the new AOF file, and finally modifies the file name, atomically switches the old file, and completes the rewrite to ensure data consistency. "

Interviewer : "Then how to open AOF?"

Xiaolong : "It can be triggered manually or by file configuration."

Interviewer : "How about that?"

Xiaolong : "You can enable AOF persistence by configuring the redis.conf file appendonly yes. At the same time, you need to jointly configure the synchronization strategy and trigger timing. However, the rewriting of AOF requires fork, which is a heavyweight operation and may cause blockage to Redis."

Xiaolong : "So in order not to affect the response of the Redis main process, we need to reduce the frequency of fork as much as possible to reduce blocking. You can manually trigger AOF rewriting through bgrewriteaof."

Interviewer : "Ok, I see that you have written MySQL in your resume, what is MySQL two-paragraph submission?"

Xiaolong : "In fact, the so-called two-phase is to divide a thing into two phases (prepare, commit) to commit, but actually enabling two-phase commit is only to ensure the security and consistency of redolog and binlog log data."

Xiaolong : "If you don't need to use binlog for log backup at all, you don't need two-phase commit for master-slave replication, because only ensuring that the redolog is successfully written to the disk can guarantee the recovery of the crash."

Interviewer : "Can you tell me how to submit it?"

Xiaolong : "You can make a complete analysis. If you need to update a piece of data now, you must know that you will judge the authority, analyze the syntax of the word, and the optimizer will generate the best execution plan. It is ready to execute. Before execution, it will also record the undolog for rollback. Then start calling the storage engine to execute SQL."

Xiaolong : "If you choose the InnoDB engine at this time, you will check whether there is any relevant data in the buffer pool. If not, the data will be loaded into the buffer pool for update;"

Monologue : "Of course, it also involves details such as unique index and ordinary index. I won't go into details here. For details, please refer to 【Interview Notes】 "

Xiaolong : "After the last memory data is updated, the update operation will be recorded in the redo log, and the redo log is in the prepare state, telling the executor to complete the execution and submit the transaction."

Xiaolong : "The executor produces the binlog of this operation and writes the binlog to the disk. The executor calls the engine transaction commit interface, and the engine changes the newly written redo log to the commit state, and finally the update is completed;"

Xiaolong : "Now the whole process is completed, and the final transaction is submitted in two stages."

Interviewer : "Ok, have you ever used explain, will the explain statement execute SQl?"

Xiaolong : "explain only goes to the optimizer, and the number of rows to be scanned is only an estimate, and it is not actually executed. It is just an execution plan that generates information about SQL execution from the optimizer, but if the query includes in the FROM clause subquery, MySQL will execute the subquery"

Monologue : "As expected of me, I am a real man! [Interview Notes] With the [Interview Notes] in hand, I don't have to worry about an offer from a big factory."

knowledge summary

In this issue, we will restore the two sides of Ali through interview simulation. Subscribe + star to continue to follow updates.

interview focus

Redis 持久化机制MySQL undoLog 日志

Guess you like

Origin blog.csdn.net/qq_43666365/article/details/130540098