Redis Persistence Management

RedisBeginner
Practice Now

Introduction

In this lab, we will explore Redis persistence management, focusing on how to configure and manage data durability. We will start by configuring Redis Database (RDB) persistence using the CONFIG SET command, which allows us to create point-in-time snapshots of our Redis data. We will also cover enabling Append-Only File (AOF) persistence, manually saving data, checking the last save time, and rewriting the AOF file to optimize its size.

Configure RDB Persistence

In this step, we will configure Redis Database (RDB) persistence. RDB creates point-in-time snapshots of your dataset at specified intervals. We will use the CONFIG SET command to modify the Redis server configuration dynamically without restarting the server.

First, connect to the Redis server using the Redis command-line interface (redis-cli). Open your terminal and run the following command:

redis-cli

You will see the redis-cli prompt, which looks like 127.0.0.1:6379>.

Let's check the current RDB save configuration using the CONFIG GET command.

CONFIG GET save

The default output shows several save points:

1) "save"
2) "900 1 300 10 60 10000"

This means Redis will save the database if at least 1 key has changed in 900 seconds, OR at least 10 keys have changed in 300 seconds, OR at least 10000 keys have changed in 60 seconds.

Now, let's modify this to a single save point: save the database if at least 1 key has changed in 60 seconds.

CONFIG SET save "60 1"

You should see OK as the output, confirming the change.

OK

Verify that the configuration was updated:

CONFIG GET save

The output should now reflect our new setting:

1) "save"
2) "60 1"

Next, we will configure the directory where Redis stores its RDB file (dump.rdb). First, exit the redis-cli to run some shell commands.

exit

Now, create a new directory and set the correct permissions. The Redis server runs as the redis user, so it needs ownership of the directory to write files.

sudo mkdir -p /var/lib/redis/labex_data
sudo chown redis:redis /var/lib/redis/labex_data

Connect back to Redis and set the dir configuration to the new path.

redis-cli
CONFIG SET dir /var/lib/redis/labex_data

You should see OK. Verify the change:

CONFIG GET dir

The output will confirm the new directory:

1) "dir"
2) "/var/lib/redis/labex_data"

Now Redis will save its persistence files to the /var/lib/redis/labex_data directory.

To ensure your progress is saved and verified correctly, please exit the redis-cli session.

exit

Enable AOF Persistence

In this step, we will enable Append-Only File (AOF) persistence. AOF logs every write operation received by the server. This provides better durability than RDB because data is written to the log more frequently.

First, connect to the Redis server:

redis-cli

Check if AOF is currently enabled using the CONFIG GET command.

CONFIG GET appendonly

By default, it is disabled:

1) "appendonly"
2) "no"

To enable AOF, set the appendonly parameter to yes.

CONFIG SET appendonly yes

You will see OK as the output. Now, verify the change:

CONFIG GET appendonly

The output should confirm that AOF is now enabled:

1) "appendonly"
2) "yes"

With AOF enabled, Redis will log all write commands to the appendonly.aof file located in the directory we configured in the previous step (/var/lib/redis/labex_data).

Let's add some data to see AOF in action. This SET command will be written to the AOF file.

SET mykey "myvalue"

You should see the following output:

OK

To ensure your progress is saved and verified correctly, please exit the redis-cli session.

exit

Manually Save Data with SAVE

In this step, you will learn how to manually trigger an RDB snapshot using the SAVE command. While Redis saves data automatically based on your configuration, a manual save is useful for creating backups or ensuring data is persisted before a planned maintenance event.

First, connect to the Redis server:

redis-cli

Let's add some new data so we have something to save.

SET anotherkey "anothervalue"

You should see the following output:

OK

Now, manually trigger a save to disk using the SAVE command.

SAVE

The SAVE command performs a synchronous save, meaning it will block all other client requests until the save operation is complete. For large databases, this can cause a noticeable pause.

You will see OK once the save is finished:

OK

This confirms that Redis has successfully written the current dataset to the dump.rdb file in the /var/lib/redis/labex_data directory. For production environments, the non-blocking BGSAVE command is generally preferred.

To ensure your progress is saved and verified correctly, please exit the redis-cli session.

exit

Check Last Save Time with LASTSAVE

In this step, we will use the LASTSAVE command to check the timestamp of the last successful RDB save. This command is useful for monitoring and verifying that your persistence strategy is working as expected.

First, connect to the Redis server:

redis-cli

To get the timestamp of the last successful save, run the LASTSAVE command.

LASTSAVE

The command returns a Unix timestamp, which is the number of seconds that have passed since January 1, 1970 (UTC). The output will look something like this, though the number will be different for you:

(integer) 1678886400

This timestamp corresponds to the time you executed the SAVE command in the previous step.

Let's see the timestamp update. We will add more data, perform another manual save, and then check the last save time again.

SET anotherkey2 "anothervalue2"
SAVE
LASTSAVE

After running these commands, you will see a new, more recent timestamp returned by the second LASTSAVE command, confirming that a new snapshot was created.

To ensure your progress is saved and verified correctly, please exit the redis-cli session.

exit

Rewrite AOF with BGREWRITEAOF

In this step, we will learn how to rewrite the AOF file using the BGREWRITEAOF command. As your application runs, the AOF file grows larger. It may contain many redundant commands (e.g., incrementing a counter multiple times). Rewriting the AOF file creates a new, compact file with the minimal set of commands needed to reconstruct the current dataset, which can significantly improve Redis startup times.

First, connect to the Redis server:

redis-cli

To demonstrate the effect of rewriting, let's perform a few operations that will add unnecessary commands to the AOF log.

SET key1 "value1"
SET key2 "value2"
DEL key1
SET key3 "value3"

The AOF file now contains four commands. However, to rebuild the current state, we only need to set key2 and key3. The commands related to key1 are redundant.

Now, trigger a background rewrite of the AOF file.

BGREWRITEAOF

You will see a confirmation message that the process has started:

Background append only file rewriting started

Redis performs this operation in the background, so it does not block client requests. It creates a temporary AOF file, writes the current dataset to it in the most efficient way, and then atomically replaces the old AOF file with the new one.

After the rewrite is complete, the appendonly.aof file will be smaller and contain only the commands to set key2 and key3.

To ensure your progress is saved and verified correctly, please exit the redis-cli session.

exit

Summary

In this lab, you have learned the fundamentals of Redis persistence management. You practiced configuring both RDB and AOF persistence mechanisms using CONFIG SET. You also learned how to perform manual data saves with the SAVE command, check the last save time with LASTSAVE, and optimize the AOF file with BGREWRITEAOF. These skills are essential for ensuring data durability and maintaining a healthy Redis instance.