Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the AOF rewrite process in Redis?
easy
A. To delete all keys from the database
B. To backup the Redis database to a remote server
C. To increase the memory usage of Redis
D. To clean and compact the Append Only File to save disk space

Solution

  1. Step 1: Understand the role of AOF in Redis

    The Append Only File (AOF) logs every write operation to Redis for durability.
  2. Step 2: Identify what rewrite does

    The rewrite process compacts the AOF by removing redundant commands, saving disk space and improving startup speed.
  3. Final Answer:

    To clean and compact the Append Only File to save disk space -> Option D
  4. Quick Check:

    AOF rewrite = compacts file to save space [OK]
Hint: AOF rewrite compacts file to save space [OK]
Common Mistakes:
  • Confusing AOF rewrite with backup process
  • Thinking it increases memory usage
  • Believing it deletes data
2. Which Redis command starts the AOF rewrite process without stopping the server?
easy
A. SAVE
B. BGSAVE
C. BGREWRITEAOF
D. FLUSHALL

Solution

  1. Step 1: Recall commands related to persistence

    SAVE and BGSAVE are for RDB snapshots, FLUSHALL deletes keys.
  2. Step 2: Identify the command for AOF rewrite

    BGREWRITEAOF triggers background AOF rewrite without stopping Redis.
  3. Final Answer:

    BGREWRITEAOF -> Option C
  4. Quick Check:

    BGREWRITEAOF = background AOF rewrite [OK]
Hint: BGREWRITEAOF rewrites AOF in background [OK]
Common Mistakes:
  • Using SAVE which blocks Redis
  • Confusing BGSAVE (RDB snapshot) with AOF rewrite
  • Using FLUSHALL which clears data
3. Given the following Redis commands sequence:
SET key1 value1
APPEND key1 _extra
BGREWRITEAOF
What is the expected effect on the AOF file after BGREWRITEAOF completes?
medium
A. The AOF file will contain only the latest state of key1 with value 'value1_extra'
B. The AOF file will be empty
C. The AOF file will contain all commands including SET and APPEND separately
D. The AOF file will be deleted

Solution

  1. Step 1: Understand the commands effect

    SET key1 value1 sets key1, APPEND adds '_extra' to its value.
  2. Step 2: Understand BGREWRITEAOF effect

    BGREWRITEAOF rewrites AOF to contain minimal commands to recreate current state, so it will store SET key1 value1_extra.
  3. Final Answer:

    The AOF file will contain only the latest state of key1 with value 'value1_extra' -> Option A
  4. Quick Check:

    BGREWRITEAOF compacts commands to current state [OK]
Hint: Rewrite saves only final key states in AOF [OK]
Common Mistakes:
  • Thinking all commands remain in AOF
  • Assuming AOF is emptied
  • Believing AOF is deleted
4. You run BGREWRITEAOF but notice the AOF file size does not reduce. What could be a likely cause?
medium
A. The rewrite process failed due to insufficient disk space
B. The AOF file was already compact and minimal
C. Redis server was stopped during rewrite
D. The command BGSAVE was used instead

Solution

  1. Step 1: Consider reasons for no size reduction

    If AOF is already compact, rewriting won't reduce size.
  2. Step 2: Evaluate other options

    Failure due to disk space would cause errors; server stop would abort rewrite; BGSAVE does RDB snapshot, not AOF rewrite.
  3. Final Answer:

    The AOF file was already compact and minimal -> Option B
  4. Quick Check:

    No size change means AOF already minimal [OK]
Hint: No size change means AOF already compact [OK]
Common Mistakes:
  • Assuming rewrite always reduces size
  • Confusing BGSAVE with BGREWRITEAOF
  • Ignoring disk space errors
5. Which of the following best explains why the AOF rewrite process improves Redis startup speed?
hard
A. Because the rewritten AOF file contains fewer commands, Redis loads data faster
B. Because the rewrite process compresses the AOF file using gzip
C. Because the rewrite process deletes all expired keys before startup
D. Because the rewrite process moves the AOF file to a faster disk

Solution

  1. Step 1: Understand what affects Redis startup speed

    Redis loads data by replaying commands in the AOF file; fewer commands mean faster loading.
  2. Step 2: Analyze options

    Rewrite does not compress with gzip, delete expired keys at startup, or move files to faster disks.
  3. Final Answer:

    Because the rewritten AOF file contains fewer commands, Redis loads data faster -> Option A
  4. Quick Check:

    Fewer commands in AOF = faster startup [OK]
Hint: Fewer commands in AOF speed up Redis startup [OK]
Common Mistakes:
  • Thinking rewrite compresses with gzip
  • Believing expired keys are deleted by rewrite
  • Assuming file is moved to faster disk
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard