0
0
Redisquery~10 mins

AOF rewrite process in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - AOF rewrite process
Start AOF Rewrite
Fork Child Process
Child: Create New AOF File
Parent: Continue Accepting Commands
Parent: Buffer New Commands
Child: Write Current DB State to New AOF
Child: Finish Writing New AOF
Child: Notify Parent
Parent: Append Buffered Commands to New AOF
Parent: Replace Old AOF with New AOF
End AOF Rewrite
The AOF rewrite process forks a child to create a compacted AOF file while the parent continues to serve clients, then merges buffered commands and replaces the old AOF.
Execution Sample
Redis
1. fork child process
2. child writes DB snapshot to new AOF
3. parent buffers new commands
4. child finishes and signals parent
5. parent appends buffered commands
6. parent replaces old AOF
This sequence shows how Redis rewrites the AOF file without blocking client commands.
Execution Table
StepProcessActionStateNotes
1ParentFork child processChild process createdParent continues serving clients
2ChildWrite DB snapshot to new AOFNew AOF file being writtenCaptures current DB state
3ParentBuffer new commandsCommands buffered in memoryCommands received during rewrite
4ChildFinish writing new AOFNew AOF file completeReady to merge buffered commands
5Child->ParentNotify parent rewrite doneParent informedParent prepares to merge
6ParentAppend buffered commands to new AOFNew AOF updatedIncludes commands during rewrite
7ParentReplace old AOF with new AOFAOF file replacedRewrite process complete
8ParentClear bufferBuffer emptiedReady for new commands
💡 Rewrite completes when new AOF replaces old AOF and buffer is cleared
Variable Tracker
VariableStartAfter Step 3After Step 6Final
Child ProcessNot startedRunning and writing AOFFinished writingExited
Parent BufferEmptyContains new commandsMerged into new AOFEmpty
AOF FileOld AOFOld AOF unchangedNew AOF with snapshot + buffered commandsNew AOF active
Key Moments - 3 Insights
Why does Redis fork a child process instead of rewriting AOF in the parent?
Forking a child lets the parent keep serving clients without blocking, as shown in execution_table steps 1 and 2.
How does Redis ensure no commands are lost during the rewrite?
The parent buffers new commands during rewrite (step 3) and appends them to the new AOF after child finishes (step 6).
When does the old AOF get replaced?
After the child finishes and the parent appends buffered commands, the old AOF is replaced at step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the parent start buffering new commands?
AStep 2
BStep 3
CStep 5
DStep 7
💡 Hint
Check the 'Parent' actions and 'Buffer new commands' in execution_table row 3
According to variable_tracker, what is the state of the AOF file after step 6?
ANew AOF with snapshot plus buffered commands
BOld AOF unchanged
CNew AOF with snapshot only
DAOF file deleted
💡 Hint
Look at 'AOF File' row under 'After Step 6' in variable_tracker
If the parent did not buffer commands during rewrite, what would happen?
ARewrite would be faster
BNo commands would be lost
CCommands during rewrite would be lost
DChild process would fail
💡 Hint
Refer to key_moments about buffering commands during rewrite
Concept Snapshot
AOF rewrite process:
- Fork child to write compact AOF snapshot
- Parent buffers new commands during rewrite
- Child finishes and signals parent
- Parent appends buffered commands
- Parent replaces old AOF with new one
- Ensures no downtime or data loss
Full Transcript
The AOF rewrite process in Redis starts by the parent forking a child process. The child writes a new AOF file containing the current database snapshot. Meanwhile, the parent continues to accept client commands and buffers them in memory. When the child finishes writing the snapshot, it notifies the parent. The parent then appends the buffered commands to the new AOF file. Finally, the parent replaces the old AOF file with the new compacted one and clears the buffer. This process allows Redis to rewrite the AOF file without blocking clients or losing commands.