Redis

How to use Snapshots with Redis

In Redis, a snapshot refers to the process and method of ensuring the persistence of data from the system memory to a permanent location such as the system’s disk.

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.

Introduction

Redis provides various methods to implement persistence on its data stores. The major two methods include:

  1. RDB
  2. AOF

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.

  1. 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.
  2. It is easy to perform recovery measures using an RDB file.
  3. Compared to other methods such as AOF, RDB is faster when loading large datasets.

Disadvantages of RDB

  1. This method is not very suitable for emergency backups.
  2. 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

  1. 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.
  2. Since the AOF method only appends the newly performed operations, there are minimal chances of data corruption.
  3. An AOF file is easy to understand and export as it logs the operations executed on the server sequentially.

Disadvantages of AOF

  1. Unlike RDB files, AOF backups are larger in size given the same dataset.
  2. 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:

  1. Redis parent process will fork a child process to handle the snapshots to the RDB file.
  2. The child process will take all the datasets and write them to an RBD file.
  3. 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.

192.168.100.78:6379> SAVE
OK

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.

192.168.100.78:6379> BGSAVE
Background saving started

How to use AOF?

To use the AOF command, use the BGREWRITEAOF command as:

192.168.100.78:6379> BGREWRITEAOF
Background append only file rewriting started

Conclusion

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!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list