Although Redis is an in-memory data store, it does provide various methods to store and recover data to and from the system’s disk.
This article will discuss how to use persistence in the Redis database. It is good to note that we will not dive deep into how the methods of backup provided by Redis are implemented. Consider the documentation to learn more and discover which is best for your use case.
Redis provides various methods to implement persistence on its data stores. The major two methods include:
Let us learn how we can use each of the methods above.
What is RDB?
RDB or Redis Database File is a data structure that allows you to take snapshots of your Redis data. Redis will regularly take snapshots of your datasets and save them to a file using RDB as the persistence method.
There are various advantages and disadvantages of using RDB for persistence. These include:
Advantages of RDB.
- Using RDB is compact and hence allows for more data to be stored. It is, therefore, a great choice when you need to archive your datasets.
- It is easy to perform recovery measures using an RDB file.
- Compared to other methods such as AOF, RDB is faster when loading large datasets.
Disadvantages of RDB
- This method is not very suitable for emergency backups.
- It tends to use more system resources, such as the CPU.
What is AOF?
AOF of Append Only File is a method of persistence that writes a log of the actions received by the Redis cluster. This log file can then be used at the startup of the Redis server and recreate the entire dataset.
Similar to RDB, there are various advantages and disadvantages of using such methods.
Advantages of AOF
- It is much more suitable for RDB in case of emergency backups. AOF will continue to perform log backups in the background when it gets too big.
- Since the AOF method only appends the newly performed operations, there are minimal chances of data corruption.
- An AOF file is easy to understand and export as it logs the operations executed on the server sequentially.
Disadvantages of AOF
- Unlike RDB files, AOF backups are larger in size given the same dataset.
- Depending on the fsync policy, AOF backup can be slower compared to RDB.
How do Snapshots work?
Taking a snapshot in Redis will export all the data in the memory into a binary file. Typically, the snapshot file is under the name dump.rdb. The RDB file contains all the datasets in the memory, including configurations and the structure. This makes it very easy to restore data using the RDB file in case of failure.
Under the hood:
- Redis parent process will fork a child process to handle the snapshots to the RDB file.
- The child process will take all the datasets and write them to an RBD file.
- If the RDB process is configured to run at an interval, the old RBD file is overwritten with the contents of the new file.
How to perform a snapshot in Redis
Like everything else in Redis, performing a snapshot of your most current dataset is very straightforward.
Use the SAVE command to create dump.rdb file.
Note: Once you run the SAVE command, all the other clients will be blocked until the task is completed. Hence, it is not recommended to run in production or on an extensive dataset.
An alternative to the SAVE command is the BGSAVE. It will perform the backup in the background using a child process. Using the BGSAVE command does not block other clients.
Background saving started
How to use AOF?
To use the AOF command, use the BGREWRITEAOF command as:
Background append only file rewriting started
This article describes how you can use various persistence methods in Redis to backup your datasets in case of reboot or failure.
Thank you for reading!