Questions
How do you do persistence in Redis?
The Scenario
You are a database administrator at a social media company. You are responsible for a Redis database that is critical to the business.
You need to make sure that the data in the database is not lost in the event of a server restart or a power failure.
The Challenge
Explain how you would do persistence in Redis. What are the different persistence options, and what are the trade-offs between them?
A junior engineer might not be aware of persistence in Redis. They might just use Redis as an in-memory cache, which would not be a very robust solution for a production database.
A senior engineer would know that persistence is a critical part of database administration. They would be able to explain the different persistence options and would have a clear plan for how to set up persistence for a production database.
Step 1: Understand the Different Persistence Options
| Option | Description | Pros | Cons |
|---|---|---|---|
| RDB | A point-in-time snapshot of the database. | Fast and easy to use. | Can result in data loss if the server crashes between snapshots. |
| AOF | An append-only log of all the write operations that are performed on the database. | More durable than RDB, can be configured to fsync on every write. | Can be slower than RDB, and the AOF file can grow to be very large. |
| RDB + AOF | A combination of RDB and AOF. | The best of both worlds: fast and durable. | More complex to set up than RDB or AOF alone. |
Step 2: Choose the Right Tool for the Job
For our use case, we should use RDB + AOF. This will give us the best of both worlds: the speed of RDB and the durability of AOF.
Step 3: Configure Persistence
Here’s how we can configure persistence in the redis.conf file:
# RDB
save 900 1
save 300 10
save 60 10000
# AOF
appendonly yes
appendfsync everysecIn this example, we have configured Redis to create an RDB snapshot every 15 minutes if at least 1 key has changed, every 5 minutes if at least 10 keys have changed, and every 1 minute if at least 10000 keys have changed. We have also configured Redis to use AOF persistence and to fsync the AOF file every second.
Practice Question
You need to be able to restore your database to any point in time. Which of the following would you use?