Redis

Redis BGSAVE

Redis is a fast and reliable in-memory database. However, like most things, it is far from perfect. The Redis server may encounter panic errors and close unexpectedly in some instances. This may lead to data loss which can be dangerous, especially in production.

To circumnavigate this, we can perform backups of our database. This tutorial will discuss how to perform a Redis database backup in the background using the BGSAVE command.

Redis Save Command

Before learning about the BGSAVE command, it is good to understand how to use the SAVE command.

The Redis SAVE command allows you to synchronously backup your database. Running the SAVE command creates a snapshot of your database at the current state in the dump.rdp file.

To use the SAVE command, open the Redis CLI and execute:

127.0.0.1:6379> SAVE

OK

One thing to note about the SAVE command is a blocking query. Hence, running the SAVE command will block the server until the operation is complete.

This can take a while if you have a large data set.

HINT: Avoid using the SAVE command in production.

Redis BGSAVE command

The BGSAVE command is the asynchronous version of the SAVE command.

Once you run the BGSAVE command, Redis will create a new thread by forking the parent.

The parent will continue to process all incoming requests while the child thread processes the backup.

The child will exit successfully once the save operation is complete unless an error occurs.

An example of the BGSAVE command is as shown:

127.0.0.1:6379> BGSAVE

Background saving started

To determine the last save, you can use the LASTSAVE command.

For example:

127.0.0.1:6379> LASTSAVE

(integer) 1646682193

The command returns the last save time in Epoch Time. You can convert to human-readable format using the date command as:

$ date -d @1646682193

Mon 07 Mar 2022 10:43:13 PM EAT

Conclusion

This article covered two fundamental commands to perform database backups in Redis.

We hope you enjoyed the tutorial.

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