Bird
Raised Fist0
Redisquery~10 mins

AOF (Append Only File) in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - AOF (Append Only File)
Client sends command
↓
Redis executes command
↓
Command appended to AOF file
↓
AOF file saved on disk
↓
If Redis restarts
↓
Replay commands from AOF to restore data
Commands from clients are executed and then saved one by one to a file. On restart, Redis replays this file to restore data.
Execution Sample
Redis
SET key1 value1
INCR counter
DEL key1
Three commands are executed and appended to the AOF file in order.
Execution Table
StepClient CommandRedis ActionAOF File ContentResulting Data State
1SET key1 value1Set key1 to value1SET key1 value1 {key1: "value1"}
2INCR counterIncrement counter (0->1)SET key1 value1 INCR counter {key1: "value1", counter: 1}
3DEL key1Delete key1SET key1 value1 INCR counter DEL key1 {counter: 1}
4Redis restartsReplay AOF file commandsSET key1 value1 INCR counter DEL key1 {counter: 1}
💡 All commands appended and replayed to restore data after restart
Variable Tracker
VariableStartAfter 1After 2After 3After Restart
key1null"value1""value1"nullnull
counter01111
AOF file"""SET key1 value1\n""SET key1 value1\nINCR counter\n""SET key1 value1\nINCR counter\nDEL key1\n""SET key1 value1\nINCR counter\nDEL key1\n"
Key Moments - 3 Insights
Why does Redis append commands to the AOF file after executing them, not before?
Appending after execution ensures only successful commands are saved, so replaying the AOF file restores the exact data state (see execution_table steps 1-3).
What happens if Redis crashes before a command is appended to the AOF file?
That command is lost on restart because it was never saved in the AOF file, so replaying the file won't restore it (see execution_table step 4).
How does Redis restore data from the AOF file on restart?
Redis reads each command in the AOF file and executes them in order to rebuild the data state exactly as before (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of the AOF file after step 2?
A"SET key1 value1\nINCR counter\n"
B"INCR counter\nDEL key1\n"
C"SET key1 value1\nDEL key1\n"
D"DEL key1\n"
💡 Hint
Check the 'AOF File Content' column at step 2 in the execution_table.
At which step does the key 'key1' get deleted from the data state?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Resulting Data State' column in the execution_table for when 'key1' disappears.
If Redis crashes before appending the 'DEL key1' command, what will be the data state after restart?
A{key1: null, counter: 0}
B{counter: 1}
C{key1: "value1", counter: 1}
D{}
💡 Hint
Refer to the variable_tracker for 'key1' and 'counter' values and the explanation about command loss before append.
Concept Snapshot
AOF (Append Only File) saves every executed command to a file.
Commands are appended after execution to ensure data consistency.
On restart, Redis replays the AOF file to restore data.
This provides durability by logging changes step-by-step.
AOF file grows over time and can be compacted by rewriting.
Full Transcript
The Append Only File (AOF) in Redis works by saving every command executed by Redis to a file after the command runs successfully. This means when a client sends a command, Redis executes it, then appends that command to the AOF file. If Redis restarts, it reads the AOF file and replays all commands in order to restore the data exactly as it was. For example, if Redis runs SET key1 value1, then INCR counter, then DEL key1, each command is saved in the AOF file. After restart, replaying these commands rebuilds the data state. This method ensures data durability by logging changes step-by-step. However, if Redis crashes before a command is appended, that command is lost and not restored on restart. The AOF file can grow large, so Redis can rewrite it to keep it compact.

Practice

(1/5)
1.

What is the main purpose of Redis AOF (Append Only File)?

easy
A. To save every write operation for data recovery
B. To compress data for faster network transfer
C. To create backups only once a day
D. To store data in a binary format

Solution

  1. Step 1: Understand AOF functionality

    AOF logs every write command to a file to ensure data can be recovered after a crash.
  2. Step 2: Compare options with AOF purpose

    Only To save every write operation for data recovery correctly describes saving every write operation for recovery.
  3. Final Answer:

    To save every write operation for data recovery -> Option A
  4. Quick Check:

    AOF saves writes = B [OK]
Hint: AOF logs all writes to recover data after crashes [OK]
Common Mistakes:
  • Thinking AOF compresses data
  • Confusing AOF with snapshot backups
  • Believing AOF stores data in binary
2.

Which of the following is the correct Redis configuration directive to enable AOF persistence?

?
easy
A. enable_aof on
B. appendonly yes
C. persistence append
D. aof_enable true

Solution

  1. Step 1: Recall Redis config syntax for AOF

    The correct directive to enable AOF is appendonly yes.
  2. Step 2: Check other options for correctness

    Options A, B, and C are not valid Redis config directives.
  3. Final Answer:

    appendonly yes -> Option B
  4. Quick Check:

    Enable AOF with appendonly yes [OK]
Hint: Remember: 'appendonly yes' enables AOF in redis.conf [OK]
Common Mistakes:
  • Using incorrect directive names
  • Confusing AOF with RDB snapshot settings
  • Using boolean words like true/on instead of yes
3.

Given the Redis AOF configuration appendfsync everysec, what is the expected behavior?

medium
A. Redis writes AOF data to disk every second
B. Redis never writes AOF data to disk
C. Redis writes every command to disk immediately
D. Redis writes AOF data only on shutdown

Solution

  1. Step 1: Understand appendfsync everysec meaning

    This setting tells Redis to sync AOF data to disk once every second.
  2. Step 2: Compare with other options

    Redis writes every command to disk immediately means immediate write (appendfsync always), B and D are incorrect behaviors.
  3. Final Answer:

    Redis writes AOF data to disk every second -> Option A
  4. Quick Check:

    everysec means sync once per second [OK]
Hint: everysec means disk sync once per second, not instantly [OK]
Common Mistakes:
  • Confusing everysec with always
  • Thinking it never writes to disk
  • Assuming writes happen only on shutdown
4.

Identify the error in this Redis AOF configuration snippet:

appendonly yes
appendfsync everyminute
medium
A. appendonly should be no
B. appendfsync must be set to 'never'
C. appendfsync value 'everyminute' is invalid
D. No error, configuration is valid

Solution

  1. Step 1: Check valid appendfsync values

    Valid values are 'always', 'everysec', or 'no'. 'everyminute' is not valid.
  2. Step 2: Verify appendonly setting

    appendonly yes is correct to enable AOF.
  3. Final Answer:

    appendfsync value 'everyminute' is invalid -> Option C
  4. Quick Check:

    appendfsync accepts only always, everysec, or no [OK]
Hint: appendfsync only accepts always, everysec, or no [OK]
Common Mistakes:
  • Assuming 'everyminute' is valid
  • Thinking appendonly yes is wrong
  • Believing appendfsync must be 'never'
5.

You want to ensure Redis AOF persistence is safe but also fast. Which configuration balances durability and performance best?

appendonly yes
appendfsync everysec
hard
A. appendonly no and appendfsync no
B. appendonly no and appendfsync always
C. appendonly yes and appendfsync no
D. appendonly yes and appendfsync everysec

Solution

  1. Step 1: Understand durability vs performance

    appendonly yes enables AOF persistence; appendfsync everysec syncs data every second, balancing safety and speed.
  2. Step 2: Evaluate other options

    Options B and D disable AOF, losing durability; appendonly yes and appendfsync no disables syncing, risking data loss.
  3. Final Answer:

    appendonly yes and appendfsync everysec -> Option D
  4. Quick Check:

    Enable AOF with everysec sync for balance [OK]
Hint: Use appendonly yes with everysec for good speed and safety [OK]
Common Mistakes:
  • Disabling AOF for speed
  • Using appendfsync no risking data loss
  • Choosing appendfsync always causing slow writes
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