In this session we are going to learn about RDB persistent. So I have already logged into the server
and right now I am in the Home Directory of
Redis Admin User
and if you remember all our Redis File or Redis Configuration is available under Redis- stable
Directory. So we are moving inside Redis- stable Directory and here if i do ls-lrt
I can see Redis.conf File.
Okay so, this is the file in which we are going to make Configuration related changes to enable RDB
Snapshotting. Okay.
So I'm going to open this file using VI Editor.
okay.
So you need to scroll down a bit and you need to reach to the point where it talks about RDB
okay.
So right now, this is the line which is talking about db file name.
Okay so this, db file name is going to be the Snapshot File Name, which is going to get created.
So by default it is called dumb.rdb.
So whenever it is going to perform the Snapshotting,
It is going to create a file which is called dumb.rdb.
And if you want to change the name you can do that.
So it is just an Optional Name or if you want to just keep the same name, you can do that as well.
So let's say, I want to change it to
dump_redis.rdb
So I have, I just need to make those changes in the Configuration File.
And now you'll be wondering on, at which location this file is going to get saved.
Okay.
So the way to specify that is, here using these dir parameter.
So you make a change here.
Okay.
So by default it is pointing to ./
So by that what it means is, that whichever place basically you are going to start Redis from, the
same directory is going to be used for keeping this dump_redis.rdb
Now if I do not make any changes and I just want to demonstrate you, how it is going to work.
So let me just save this and exit from here
and let me go back to the home directory of this user. Okay.
If I do pwd, You can see right now I am in the home directory of Redis Admin and if i do ls,
the only directories or the files you see are, these are the 3 items which you see.
Okay.
So I am going to start Redis Server by providing Redis Server and also we need to supply the Configuration
File Name. So which is available at this location.
So I'm specifying that and also we are using &
So start this process as a background.
Now notice in this case, the you know, the Redis dump file which is going to be created is, going to be created
at this location.
which is /home/redisadmin. Because this is the location from where we are starting Redis
server and by default for the directory in the Redis.conf file, it was specified as./
So basically it is similar like present working directory. Okay.
So once I hit enter and if I do ls, right now there is nothing available because Redis has not
performed any Snapshotting.
Okay.
So we also need to specify, what is this Snapshotting. Mechanism or basically how often Snapshotting should take
place.
So if you go and open this file again using VI editor, you will also notice,
I guess we need to go bit up. Okay.
All right, here.
So by default you see, these are the default configuration which is available here but you can specify
the configuration based on the approach which you want to follow.
So in this case using save as a keyword.
What it is telling to Redis is, that save or snapshot all the data every 900 seconds.
If there is at least one key has been changed.
Okay.
The second line what it is telling is that perform a Snapshotting every 300 seconds.
If there are 10 keys, which has been added or updated.
Okay.
And the third one is telling about
to perform a snapshot every 60 seconds.
If there are 10000 Keys has been changed. Okay so all of these three conditions are going to get checked.
And based on that, the Snapshotting is going to happen.
Now what I will do is I'll create one more piece of line and I will say to
snapshot every 30 seconds
and we can save there is 50 keys changed.
Okay so I have added, added it as a First Line.
This can be, you know, any line but usually the practice is you give the higher number on the top and
then you give the lower number of Snapshotting at the bottom but that's all about how you want
to basically document your these things.
But yeah.
So that's all for now.
And if I'm going to save it and again whenever you make any changes in redis.conf
It is not going to reflect automatically.
So whatever changes I have done there, has not been reflected as of now.
So I need to restart Redis.
So what I will do is, I'll just kill this process.
Now if I see Redis is not running
I'll copy paste this Redis startup command to start Redis.
Okay let's do ls-lrt. So right now, we do not see any dump file which has been created.
Now let's start inserting data to Redis
So I already have a file which is called country code names.
It is having around 240 plus record. So I'm going to insert this to Redis.
So I'm going to use Mass Insertion Approach or Redis cli pipe Approach.
Okay so it has performed the insertion
and now if I go back to Home Directory and do ls-lrt. Now you can see we have a dump_redis. rdb File
Okay so which means, it has performed the snapshot or taken a snapshot based
on the
whatever configuration which we have defined in the Configuration File.
Okay.
Now what is the mechanism to restore it.
So basically our dump file is available here.
So we need to make sure in this case, basically when we are starting Redis
and since in the Redis.conf File, dir property is set to ./ which means present working
directory.
So whenever you start Redis, it is going to look for this dump file in the current working directory.
And then if it is available, it is going to restore everything.
But if you are starting Redis from the directory, where this file or dump.rdb file doesn't
exist, then it will not be able to restore anything. It would be assuming there is no snapshot available
and it will not be able to restore any data.
So let me show you that approach.
So we'll move to raw_data directory and here if you see, we do not have any
dump_redis.rdb file.
so let me kill this Redis process.
Okay.
We do not have Redis running right now.
So let's say, if I start Redis from here
okay.
So in this case Redis will not be able to restore the data because in the configuration file we have
defined that dumpfile is going to be stored at ./ location which is present working directory
but when I am starting Redis, I am starting from the location where Redis doesn't have or basically there
is no dump file. okay or
There is no snapshot file available, which can be restored.
So Redis is just going to assume that there is nothing available to restore and it is just
going to start without loading any data.
Okay so let's start this and let's connect to Redis, using Redis cli. Specify the port
and if I do dbsize, you can see I do not have any data and if I do Keys * you can see we do not have
any data. The reason being is, we restarted it from a different directory and there was no dumped file available
to restore.
So now there are two things which you can do. Either you can move back to directory where you have this
dump_redis.rdb file available or let's say you want to restart Redis from any
other location. In that case you can move this file from this location to the location from where you
want to start Redis from.
OK.
So in this case I'm just going to kill previous Redis process and then we will be starting Redis from
this location
okay.
So we do not have Redis running right now and currently I am in the Redis Admin Users Home
Directory and where we have dump_redis. rdb file available
So I'm going to start Redis by using Redis-server Command.
and also going to provide the Configuration File Location and let's start this as a background process.
So once we do that and when we log in to Redis using Redis cli
and if I run dbsize, this time you can see we have 247 Key Value Pair available in Redis.
And if I do Keys * you can see these are the number of keys which are available.
So basically what it has done is, it has restored the snapshot which was taken
OK.
If you want to see any value available for this key. You can use Get command and you can see the data
is available in
OK.
So that's one way.
OK.
So this is probably not the best approach in the Production. In the Production, you always want your
dump rdb file to be created at a fixed location and also always to be restored from that fixed location
because in a production you're going to run Redis from a, through probably some start up Script and you
don't want all this location to be, you know, Volatile.
You want it to be Static. So let's go back to Redis- stable directory and then move on or basically
open the file which we want
to Edit.
and then move back to this location, where it say directory and here you specify the full name or full path
of the directory, where you want to keep all this snapshotting file or from where you want to restore
the snapshot file.
Okay.
So here I'm going to specify
home redis admin.
So now this is the location or probably I will also specify redis backup.
Okay, so this is the directory where the Redis is going to basically, you know, put all the snapshot file or
this is the location from where it is going to restore the snapshot file.
I'm not sure if, you know, Redis will automatically create this directory. So let's check that. I've saved
it and I'm coming out of this
Editor and let's kill the existing Redis process.
Okay so let's remove this dump file, which was available.
Okay.
So we see, we do not have any of Redis process running right now.
And let's start Redis and now I can start Redis from any location.
It doesn't matter that I need to be on the same location where, you know, Redis snapshot files
are going to be kept.
So I can be in any location, just to demonstrate you I'm moving to /tmp location.
You see my current working directory /tmp and from here we'll be starting that
Redis.
So the command is used Redis-server and also specify the location where configuration file
is available
and give & in the end
So this process runs as a background process.
Okay so that's it.
And if I go back to the, My Home Directory
and if I do ls-lrt, here right now we do not see any backup directory created here.
So let's try to load some data to Redis.
okay.
So there seems to be some issue in starting Redis. So let's check the Logs
okay.
So in the Log Directory, we see there is a error which is related to, it cannot find this directory.
So whatever directory which you provide, there must exist.
Redis is not going to create that directory.
So let's create this directory
okay.
Now if I just clear the screen and show you.
So we have created a directory, which is called Redis_backup
and now let's move back to /tmp. From this location we are going to start Redis Server.
okay.
Now let's connect to Redis and see if
this time we are able to connect
okay.
So this time we can successfully connect to Redis
And if I do dbsize, right now we do not have any data.
Because if you remember, previously we had already removed the dumb file and also we made some Configuration
Changes.
OK so
our goal this time is
that once we insert the data, that snap or the dumb file should be getting created inside
Redis_backup directory.
So right now if I see
insider Redis_backup directory,
We do not have any file.
OK because we have not performed any insertion or there is basically no keys have changed and in the
Configuration File
We have defined, how many minimum number of keys should change, before Redis performs a Snapshot.
OK.
So we have to insert some data
so I'll be inserting data using, Redis cli pipe method
OK.
So it has inserted around 200.
Basically it has made key changes, 249 key changes, and now if I go back to
Redis_backup directory, we can see there is a dumb file is created
and now regardless of the position which we are in, let's say we move on to, let's say we go back to,
/tmp directory and let's kill the existing Redis process and we will restart Redis.
so right now, we do not have Redis process running
and I'm going to use this command.
OK.
So my current working directory is /tmp and I'm going to start Redis from this location but
you will notice unlike the, you know, the way we started previously, when we started previously Redis
from a different location, then the Redis.dump file present in, we were not able to restore any
data But this time we have specified in Redis.conf directory that all the backup is going to be
stored in to /home/redisadmin/redis_backup directory. So it knows
the location and it is going to pick or scan for the dump file from that location and then it is going to
restore.
So let's run this, to start Redis server and let's connect to Redis server.
Okay.
Now if I run dbsize, you can see I still have 247 keys available
so basically whatever data, whatever snapshot data was available, it has been successfully restored. Okay
so that's about Redis RDB Approach of Snapshotting
And Restoring. In next session we are going to learn about AOF