So in this session let's talk about AOF, which is another approach of Data Persistence in Redis.
So just a quick recap. In our previous session, we used RDB Persistence Mode and we specified we made
some changes in the Configuration Directory, where we specified that the Dump should be kept at
Redis_backup location
and if we go on this,
I mean if we go at this folder, we can see there is a file which is called dump_redis.rdb and
this File Name was also specified in, Redis.conf, Configuration File. OK.
So let's stop the Redis process
and then let's go back to the Configuration File
Let me clear the screen.
OK.
So we need to go inside stable folder, Redis-stable folder and here we have Redis.conf
File.
OK.
So I will open this file and first thing which I'll do is, I'm going to comment these statements
because we do not want to use RDB Snapshotting.
Basically we are going to use AOF.
So in that case you can comment these lines, because if you keep these specified, then it is still going
to snapshot based on whatever the configuration which has been written here.
OK.
So once that is done, now it is not going to, you know, perform RDB Snapshotting.
Remaining all other things you can leave as it is, because it doesn't really matter, because you haven't
really provided the Snapshotting Mechanism or the Check Point. It will Snapshotting should perform.
So even though if you leave this file, it is not going to make any difference.
And now this is the directory where we have specified and this was specifying the location of the
dump_redis.rdb.
So by default, AOF file is also going to be stored at the same directory. Okay whatever directory is specified
here.
It is the same directory, inside which AOF files are going to be stored.
However, you do have option to change the Name of the AOF file
So if you scroll down, let's try to find the information related to AOF
Or rather I will just search for the AOF Keyword.
And now we move on to the location, where we have AOF file name specified.
Okay so this is the name of the file, or AOF file which is going to be created.
So I'm also going to change this to different file name. i will call it Redis_backup.aof
Okay.
So this is the AOF file, which is going to get created.
And right now if you see, by default appendonly is set to no.
Okay.
That's why Redis doesn't perform any AOF backup.
So I'm going to change it to Yes. Okay.
So what these two configuration will do is, it is going to tell to Redis that append only or AOF
Persistence is enabled and also all the instructions, which is going to be stored in
append only file, that should be stored at this location. The location which we specified in directory and
in this file.
okay.
If you scroll down you are going to
get the location where append fsync has been specified. So by default appendfsync is set to every
second, which means AOF is basically going to, every second is going to log everything.
So whatever has been or whatever has been written to Redis or whatever keys have been changed, those
are going to be logged in to AOF file.
every one second. If you change it to Always. In that case, it is going to be very slow I mean the performance
is going to be impacted because whenever you perform or whenever you make any key changes that is going
to be logged into that File, AOF File.
So usually the best approach is to, just keep Every Second.
So in this case even if your system crashes, the only data which you are going to lose is maximum of
one second of data.
OK.
So if that is OK you can go ahead with this approach.
If this is not okay, then you can or you do not want any data loss, then you can go ahead with append fsync
set to always.
So in this case I'm just going to leave it to Every Second
and we are going to save it and go back.
Okay.
So right now, we're in this location but it doesn't really matter, which location we start Redis from.
So I have moved to Home Directly of this user. I'm going, I'm going to start Redis.
So when I'm starting Redis, I'm also providing the full path of the Redis Configuration File. If you
do not provide this, Redis is just going to assume, there is no Default configuration File.
Okay so whatever changes you have provided, is not going to get reflected.
So I have ran this and let's connect to Redis by using redis cli
OK.
So right now this DB is empty
and if I go back to this location, you see there is already a redis_backup.aof
file , which has been created and right now the size is 0
So if I cat this file and if I see what is the content in this file.
So right now we do not see anything.
Okay.
This file is completely empty.
Now let's perform some Redis write operations. So yeah lets connect to Redis again using
Redis cli
and I'm going to write some Country Short Codes along with the Country Names.
So basically whatever instruction, which has been written after delay of 1 sec, is going to get written
to redis_backup.aof
So if I exit from here and if I do ls-lrt here again. Now you see redis_backup.aof
size has been changed to 9. Okay let's see the size
Yeah it has been changed to 91.
And let me also get rid of this
dump redis.rdb
We are not no longer using this.
OK.
Yeah.
So basically now we have only 1 file, which is called redis_backup.aof
And if I perform More Operation
and if I exit and if I see the size again. You see the size has been changed again.
The reason is whatever
commands, which you have been writing here.
That is actually being inserted into this log file.
OK.
So this is a Text kind of File. So you can open this file using cat Editor any other editor.
And here you can see all the commands, which we have basically requested to Redis, has been saved here.
OK.
So this is how the data gets written to this
redis_backup.aof File.
Now let's insert some large data.
So whatever the data is there in this country_code_names.csv file, I'm
going to write it to Redis.
OK.
So there are 249 Key Value Pairs, which has been changed. Now if I go back to Redis_backup directory.
and I see the size of redis_backup.aof file has changed again.
And if we see the content in that file. We can see contends has also been updated and we can see
the statements, which are key value pairs, which is available in that AOF file.
So now what Redis is going to do is, it will keep on appending all the instructions which we are passing
to Redis and going to make an entry here.
And if I want to, let's say, whenever Redis is crashing and we are restoring. All the data which is available
here in redis_backup.aof is going to be played back and then this is how Redis is going
to restore all the data.
So let's perform a simulation of Redis crash, by killing this process
so i have killed Redis.
a process now. Actually we try to connect to Redis, we see we'll not be able to connect to Redis
OK.
So connection is refused, which means Redis not running
and now let's start Redis again by, giving Redis-server command.
and also we need to provide full path of the Redis.conf configuration file
OK.
So this has started Redis process.
Now if I connect using Redis cli, I'll be, I should be able to connect
okay.
And if I do dbsize, you can see whatever changes which were there, it has already been restored and Redis
has played back that AOF File
And we do not see any Data Loss.
okay.
So whatever data which we had inserted, it is there.
and also as we learn that this Redis or whatever AOF File, which we have created it is possible which
means you can change the instructions in this. So let's try to change some instructions and then restore
this file. So I will log into Redis Client again
okay.
Just make a note of this dbsize. So right now we have 248 number of keys which is there in Redis
And now I'll exit from here and I will make changes in this file.
Okay.
So
let's remove some of the entries.
All right, so let me remove these Entries. So whatever we see here,
this set instructions. I'm going to remove.
So couple of set instructions, I have removed from this AOF file
and now if we stop Redis and start Redis. It is going to be restored using this file and also the
total number of keys, which is now available in this file, has been reduced.
It is not going to be 248
So let's check that
so i have stopped
Redis
And then I have started Redis again.
And let's say, if we are able to connect to Redis, by using Redis cli
okay.
So we are able to connect to Redis.
And if I dbsize,
So now you can see, since we deleted some entries from that
redis_backup.aof file. This time when I see dbsize, after restarting
Redis. I see less number of entries here or less number of output when I do a dbsize
command.