📖 Scenario: You are managing a Redis database for a small online store. You want to ensure that your data is saved safely so it won't be lost if the server restarts or crashes.Redis offers two main ways to save data: RDB snapshots and AOF logs. You will set up both to understand how they work together.
🎯 Goal: Set up Redis persistence by configuring RDB snapshotting and enabling AOF logging. This will help you keep your data safe and recoverable.
📋 What You'll Learn
Create a Redis configuration snippet to enable RDB snapshotting every 60 seconds if at least 5 keys changed
Add a configuration line to enable AOF persistence
Set the AOF rewrite policy to trigger when the AOF file grows to twice its original size
Complete the Redis configuration with both RDB and AOF settings combined
💡 Why This Matters
🌍 Real World
Redis persistence ensures that data is not lost during server restarts or crashes, which is critical for applications like online stores, chat apps, and caching layers.
💼 Career
Understanding Redis persistence is important for roles like backend developers, DevOps engineers, and database administrators who manage data reliability and recovery.
Progress0 / 4 steps
1
Enable RDB Snapshotting
Write a Redis configuration line to save a snapshot every 60 seconds if at least 5 keys changed. Use the exact syntax: save 60 5.
Redis
Hint
Use the save directive followed by the time in seconds and the number of changes.
2
Enable AOF Persistence
Add a Redis configuration line to enable Append Only File (AOF) persistence. Use the exact syntax: appendonly yes.
Redis
Hint
Set appendonly to yes to turn on AOF persistence.
3
Set AOF Rewrite Policy
Add a Redis configuration line to set the AOF rewrite policy to trigger when the AOF file size grows to twice its original size. Use the exact syntax: auto-aof-rewrite-percentage 100.
Redis
Hint
The auto-aof-rewrite-percentage sets the growth percentage to trigger rewrite. 100 means 2x the original size.
4
Complete Redis Persistence Configuration
Combine all previous lines into a single Redis configuration snippet with RDB snapshotting and AOF persistence enabled. The final code must include save 60 5, appendonly yes, and auto-aof-rewrite-percentage 100.
Redis
Hint
Make sure all three configuration lines are present together.
Practice
(1/5)
1. What is the main purpose of Redis persistence?
easy
A. To speed up data retrieval during queries
B. To save data so it is not lost after a restart
C. To compress data for less storage use
D. To encrypt data for security
Solution
Step 1: Understand Redis persistence goal
Redis persistence is designed to keep data safe even if Redis restarts or crashes.
Step 2: Match purpose with options
Only To save data so it is not lost after a restart talks about saving data across restarts, which matches the goal.
Final Answer:
To save data so it is not lost after a restart -> Option B
Quick Check:
Persistence means saving data = A [OK]
Hint: Persistence means data stays after restart [OK]
Common Mistakes:
Confusing persistence with speed optimization
Thinking persistence compresses data
Assuming persistence encrypts data
2. Which Redis persistence method saves snapshots of the dataset at intervals?
easy
A. None of the above
B. AOF (Append Only File)
C. Both AOF and RDB
D. RDB (Redis Database)
Solution
Step 1: Recall persistence methods
RDB saves snapshots of the dataset at set intervals, while AOF logs every write operation.
Step 2: Identify correct method for snapshots
Only RDB matches the description of saving snapshots periodically.
Final Answer:
RDB (Redis Database) -> Option D
Quick Check:
Snapshots = RDB [OK]
Hint: Snapshots = RDB saves whole data periodically [OK]
Common Mistakes:
Mixing AOF with snapshot saving
Choosing both methods as snapshot savers
Thinking AOF saves snapshots
3. Given Redis configured with both RDB and AOF enabled, which statement is true about data recovery after a crash?
medium
A. Redis will use the AOF log if available, otherwise the RDB snapshot
B. Redis will use only the RDB snapshot for recovery
C. Redis will use only the AOF log for recovery
D. Redis cannot recover data if both are enabled
Solution
Step 1: Understand recovery priority
When both RDB and AOF are enabled, Redis prefers AOF for recovery because it has more recent data.
Step 2: Analyze options
Redis will use the AOF log if available, otherwise the RDB snapshot correctly states Redis uses AOF if available, else falls back to RDB.
Final Answer:
Redis will use the AOF log if available, otherwise the RDB snapshot -> Option A
Quick Check:
AOF if available else RDB = B [OK]
Hint: AOF has latest data, used first for recovery [OK]
Common Mistakes:
Assuming Redis uses only RDB always
Thinking Redis cannot recover with both enabled
Believing AOF is ignored if RDB exists
4. You configured Redis with AOF persistence but notice the AOF file is not growing after writes. What is the most likely cause?
medium
A. AOF rewrite is stuck and blocking writes
B. Redis is only using RDB snapshots
C. AOF persistence is disabled in the config
D. Redis does not support AOF persistence
Solution
Step 1: Check AOF file behavior
If AOF file size does not increase after writes, it suggests AOF is not active or enabled.
Step 2: Identify cause from options
AOF persistence is disabled in the config states AOF persistence is disabled, which explains no file growth.
Final Answer:
AOF persistence is disabled in the config -> Option C
Quick Check:
No AOF growth means AOF disabled = C [OK]
Hint: No AOF file growth? Check if AOF is enabled [OK]
Common Mistakes:
Assuming Redis does not support AOF
Thinking AOF rewrite blocks all writes
Confusing RDB usage with AOF file size
5. You want to minimize data loss in Redis but keep fast restart times. Which persistence setup is best?
hard
A. Enable both RDB snapshots and AOF with fsync every second
B. Enable only AOF with every write logged
C. Disable persistence and rely on backups
D. Enable only RDB snapshots every hour
Solution
Step 1: Understand trade-offs
RDB snapshots are fast but risk losing data between snapshots. AOF logs every write but can slow restarts.
Step 2: Analyze combined setup
Enabling both with AOF fsync every second balances minimal data loss and reasonable restart speed.
Step 3: Evaluate other options
Only RDB risks data loss; only AOF can slow restarts; disabling persistence risks total data loss.
Final Answer:
Enable both RDB snapshots and AOF with fsync every second -> Option A
Quick Check:
Best mix for safety and speed = D [OK]
Hint: Combine RDB + AOF for safety and speed [OK]
Common Mistakes:
Choosing only RDB and ignoring data loss risk
Choosing only AOF and ignoring restart speed
Disabling persistence entirely
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.