Redis Persistence Management

RedisRedisBeginner
Practice Now

Introduction

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL redis(("Redis")) -.-> redis/RedisGroup(["Redis"]) redis/RedisGroup -.-> redis/access_cli("Connect Using CLI") redis/RedisGroup -.-> redis/store_string("Set String Value") redis/RedisGroup -.-> redis/fetch_string("Get String Value") redis/RedisGroup -.-> redis/remove_key("Delete Single Key") subgraph Lab Skills redis/access_cli -.-> lab-552101{{"Redis Persistence Management"}} redis/store_string -.-> lab-552101{{"Redis Persistence Management"}} redis/fetch_string -.-> lab-552101{{"Redis Persistence Management"}} redis/remove_key -.-> lab-552101{{"Redis Persistence Management"}} end

Configure RDB Persistence

In this step, we will configure Redis Database (RDB) persistence using the CONFIG SET command. RDB creates point-in-time snapshots of your Redis data, saved to disk. This is a crucial aspect of data durability in Redis. The CONFIG SET command allows you to dynamically modify Redis server configuration parameters.

First, let's connect to the Redis server using the Redis command-line interface (redis-cli). Open a terminal and type:

redis-cli

You should now see the redis-cli prompt: 127.0.0.1:6379>.

The CONFIG SET command takes two arguments: the configuration parameter you want to change and the new value for that parameter.

Let's check the current configuration related to RDB. We can use the CONFIG GET command to retrieve the current configuration. For example, to check the save configuration, run:

CONFIG GET save

You'll see output similar to this:

1) "save"
2) 1) "900"
   2) "1"
   3) "300"
   4) "10"
   5) "60"
   6) "10000"

This output shows the current save points. Redis will automatically save the database to disk if 900 seconds have passed and at least 1 key has changed, or if 300 seconds have passed and at least 10 keys have changed, or if 60 seconds have passed and at least 10000 keys have changed.

Now, let's modify the save configuration. We'll set a single save point: save the database if 60 seconds have passed and at least 1 key has changed. To do this, use the following command:

CONFIG SET save "60 1"

You should see the following output:

OK

This indicates that the configuration has been successfully updated.

Let's verify the change:

CONFIG GET save

The output should now be:

1) "save"
2) 1) "60"
   2) "1"

This confirms that we have successfully configured the RDB persistence settings using the CONFIG SET command. Redis will now automatically save the database to disk if 60 seconds pass and at least one key is modified.

Finally, let's configure the directory where Redis stores its RDB files. The default directory is usually the Redis working directory. We can change this using the dir configuration option. Let's set the dir configuration option to /home/labex/project/redis_data:

CONFIG SET dir /home/labex/project/redis_data

You should see the following output:

OK

Verify the change:

CONFIG GET dir

The output should now be:

1) "dir"
2) "/home/labex/project/redis_data"

Now Redis will save RDB files to the /home/labex/project/redis_data directory.

Remember to exit the redis-cli by typing exit and pressing Enter. This ensures that the commands are logged.

exit

Enable AOF Persistence

In this step, we will enable Append-Only File (AOF) persistence using the CONFIG SET command. AOF logs every write operation received by the server, which will be replayed during server startup, reconstructing the original dataset. This makes Redis more durable than RDB, although it can impact performance slightly.

First, connect to the Redis server using redis-cli:

redis-cli

You should now see the redis-cli prompt: 127.0.0.1:6379>.

Let's check if AOF is currently enabled. We can use CONFIG GET command to retrieve the current configuration for appendonly:

CONFIG GET appendonly

You'll likely see output similar to this:

1) "appendonly"
2) "no"

This indicates that AOF is currently disabled.

To enable AOF, we need to set the appendonly configuration parameter to yes. Use the following command:

CONFIG SET appendonly yes

You should see the following output:

OK

This indicates that the configuration has been successfully updated.

Let's verify the change:

CONFIG GET appendonly

The output should now be:

1) "appendonly"
2) "yes"

This confirms that we have successfully enabled AOF persistence. Redis will now start appending every write operation to the AOF file. The default AOF file name is appendonly.aof, and it's located in the directory specified by the dir configuration option (which we set to /home/labex/project/redis_data in the previous step).

Now, let's check the current configuration related to AOF file name. We can use CONFIG GET command to retrieve the current configuration. For example, to check the appendfilename configuration, run:

CONFIG GET appendfilename

You'll see output similar to this:

1) "appendfilename"
2) "appendonly.aof"

This output shows the current AOF file name.

Let's add some data to Redis to see AOF in action.

SET mykey "myvalue"

You should see the following output:

OK

Now, the appendonly.aof file in the /home/labex/project/redis_data directory will contain the command to set mykey to myvalue.

Remember to exit the redis-cli by typing exit and pressing Enter. This ensures that the commands are logged.

exit

Manually Save Data with SAVE

In this step, we will learn how to manually trigger a Redis database save using the SAVE command. While Redis automatically saves data based on the RDB configuration (as set in step 1) and appends write operations to the AOF file (if enabled, as in step 2), you can also force a save at any time. This is useful for creating backups or ensuring data is written to disk before a planned shutdown.

First, connect to the Redis server using redis-cli:

redis-cli

You should now see the redis-cli prompt: 127.0.0.1:6379>.

Before saving, let's add some more data to Redis. This will ensure that the save operation has something to persist.

SET anotherkey "anothervalue"

You should see the following output:

OK

Now, to manually save the database to disk, simply use the SAVE command:

SAVE

The SAVE command is a blocking operation, meaning that the Redis server will be unavailable to handle other requests until the save is complete. For large databases, this can take a significant amount of time.

You should see the following output:

OK

This indicates that the save operation was successful. Redis has now created or updated the RDB file in the directory specified by the dir configuration option (/home/labex/project/redis_data). The file name will be dump.rdb by default.

It's important to note that the SAVE command is generally not recommended for production environments due to its blocking nature. For production use, the BGSAVE command (which performs the save in the background) is preferred. We will not cover BGSAVE in this lab, but it's important to be aware of its existence.

Remember to exit the redis-cli by typing exit and pressing Enter. This ensures that the commands are logged.

exit

Check Last Save Time with LASTSAVE

In this step, we will use the LASTSAVE command to check the timestamp of the last successful database save. This command returns a Unix timestamp, which represents the number of seconds that have elapsed since January 1, 1970 (UTC).

First, connect to the Redis server using redis-cli:

redis-cli

You should now see the redis-cli prompt: 127.0.0.1:6379>.

To check the timestamp of the last successful database save, simply use the LASTSAVE command:

LASTSAVE

You should see output similar to this:

(integer) 1678886400

(The actual number will vary depending on when you executed the SAVE command in the previous step.)

This number represents the Unix timestamp of the last successful save. You can convert this timestamp to a human-readable date and time using online tools or command-line utilities. For example, in a separate terminal, you can use the date command:

date -d @1678886400

(Replace 1678886400 with the actual timestamp you received from the LASTSAVE command.)

The output will be a human-readable date and time:

Mon Mar 15 16:00:00 UTC 2023

The LASTSAVE command provides a quick way to verify when the last RDB save operation occurred. This can be useful for monitoring and troubleshooting persistence issues.

Let's add some more data and save again to see the LASTSAVE value change.

SET anotherkey2 "anothervalue2"
SAVE
LASTSAVE

You should see a new timestamp after running the above commands.

Remember to exit the redis-cli by typing exit and pressing Enter. This ensures that the commands are logged.

exit

Rewrite AOF with BGREWRITEAOF

In this step, we will learn how to rewrite the AOF file using the BGREWRITEAOF command. Over time, the AOF file can become large and contain redundant or outdated information. Rewriting the AOF file creates a new, smaller AOF file that contains the minimal set of commands needed to recreate the current dataset. This process optimizes the AOF file and improves Redis startup time. The BGREWRITEAOF command performs this rewrite in the background, minimizing the impact on Redis performance.

First, connect to the Redis server using redis-cli:

redis-cli

You should now see the redis-cli prompt: 127.0.0.1:6379>.

Before rewriting the AOF, let's add some more data and perform some operations that will create some "garbage" in the AOF file.

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

You should see OK after each SET command and (integer) 1 after the DEL command.

Now, to rewrite the AOF file in the background, use the BGREWRITEAOF command:

BGREWRITEAOF

You should see the following output:

Background append only file rewriting started

This indicates that the AOF rewrite process has started in the background. Redis will create a temporary AOF file, write the current dataset to it, and then atomically replace the old AOF file with the new one.

While the AOF rewrite is in progress, Redis will continue to append new write operations to both the old AOF file and the temporary AOF file. This ensures that no data is lost during the rewrite process.

You can monitor the progress of the AOF rewrite by checking the Redis log file. However, for this lab, we will simply assume that the rewrite completes successfully.

After the BGREWRITEAOF command completes, the appendonly.aof file in the /home/labex/project/redis_data directory will be smaller and more efficient. It will contain only the necessary commands to recreate the current dataset (in this case, setting key2 to value2 and key3 to value3). The DEL key1 command will no longer be present in the rewritten AOF file.

It's important to note that BGREWRITEAOF is a relatively resource-intensive operation, so it's best to schedule it during periods of low traffic.

Remember to exit the redis-cli by typing exit and pressing Enter. This ensures that the commands are logged.

exit

Summary

In this lab, we explored Redis persistence management, focusing on how to configure and manage data durability. We configured RDB using CONFIG SET, enabled AOF, manually saved data, checked the last save time, and rewrote the AOF file. You have learned how to adjust Redis's persistence behavior to suit specific data durability requirements.