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