So just a quick recap.
Until now we have learned how to install Redis and how to configure it and how to connect to Redis server
using Redis client.
So in this server or in this session, I am again going to connect to Redis client, using Port which
is 6 3 7 0 and if we see the total number of keys available in Redis right now is only 1.
So
I will clear this by doing Flushall and if you see again we should not have anything in the Redis
.Okay.
So right now we do not have anything saved in Redis
And if you want to write something again to Redis, the command which we need to use it Set Command, followed with
the key.
So which is going to be, let's say, in this case I want to keep all the Country short code along with the
Country's Description. So I can put something like this.
This is a key value pair for country short code and the country's Full Name. so once I do that and
if I do key * I'm going to get this back and if I want to see the value of any key.
So I need to use get Command followed with the key name.
So if I do like this. It is going to give me the value, which is against this, which is stored against
this ID
Remember you can also put a key in the ""
It is going to work the same way.
And I can insert more data by following the same approach.
So that's how you do something, you know, Sequential. It is like Sequential Way of doing things.
But let's say, if you want to insert or you want to do a Mass Insertion, where you want to upload
like 1 million records and more than millions of records in Redis.
So what is the best way to do it.
So in this session we are going to learn that
for Demo purpose I'm going to exit from here and then we are, let me clear the screen and right now i am in
Redis Admin User's Home Directory.
Here I have created a folder, which is called Raw_data.
So once we go inside this folder. I have downloaded a sample file which is called countries. csv
So if I show you some data which is available in countries. csv is, something like this.
So it has countries name and also it has countries code, which is separated by comma
OK.
So this is like a csv, which I have.
But using Redis, will not be able to upload this
csv.
So we need to convert it to a format, which is actually recognizable by Redis and which can be used
by Redis mass insertion.
So if you follow Redis Documentation, the mass insertion mechanism which they have, which they advise
is called something like
Redis-cli and --pipe.
Okay.
So the way it works is, basically you create a file which contains list of all the key value pairs OK.
And that actually should be in this format.
So it is going to be like you know, a list of all the set commands, which is actually available in a file
and then you use Redis cli pipe.
to pass
all this file.
I mean the content of this file, to Redis server.
And then, Redis server is going to process all of this data in a sequentially, in a Sequential
Manner.
And then it is going to reply you back ,once all of the work is done.
Okay.
So why this is faster.
The reason is,if you run all of these commands one by one. If you use set command and provided the value
of, you know, Key Value Pair basically one by one.
So Redis Server has to respond to each of the queries back.
So you are making a request sequentially and then actually Redis has to reply back you with a response.
So it increases the number of response and hence it also increases the total amount of Throughput.
Basically the total amount which is taken in the, in order to process the request.
Now when you follow this Redis cli pipe approach.
What it does is, let's say you have one million records or one million Key Value Pairs which you want
to insert to Redis.
So you supply the complete instruction at one go.
And then Redis processes it.
And then Redis response you back.
OK.
So basically Redis has to respond you back only one time
So let's see this in action.
So the file which I have is,
you know countries. csv, which is having Name and Code.
And if you know bit of awk command, if I want to print only the first column. I can do something like
this.
So if you know awk command and
If I run something like this, by default the daily meter which is used is space.
OK.
So first column is going to be treated anything, which comes before a space and then anything which
comes after a space is going to be treated as a Second Column.
But in our case, we do not have this value separated by space. Rather what I have is
These values are separated by a , separator.
So that's why I am using -F to specify the field daily meter.
And now if I print this. So I have got content of First Column.
Okay, so First Column contains Country Names
and if I do a Second Column, then I'm going to get all the Short Codes.
Okay.
Don't worry much about the data.
The data is just the sample data, which I have downloaded.
So sometimes you see like, its a big list of string.
So this is something which can be ignored
okay.
So now the format, in which we want the data is like a Redis
Command.
Okay.
So we should have Set as the keyword which is Redis
Command.
To insert the data and then we want space followed with the Key and then space followed with the Value.
So what I have done is
I use some awk command, to prepare the data and I have
ran something like this
okay.
If you see, this has given me Output or something which we wanted to Export.
And after that what I did is, i removed everything which comes after space because the value which Redis
is going to recognize is, Set keyboard which is Redis command to insert the Data and then space then
followed with a Key Name and then space followed with the Value.
So in this case, if your value contains something, which is including Spaces. You
can actually quote the value within " " or otherwise if it is just for the practice purpose,
you may get rid of the space. So I have already prepared the file and the file which I have prepared is called
countrycode_ names. csv
So if you see, the content of this file is very clean and I basically what I have in this file is, something
like this. So I have set keyword, followed with Keys. Which is Country Short Code and then followed with
a Value, which is the Description of the Country.
So now,
let's once check, how many total number of Records are available and the way to do is, you can use
wc-L command, followed with a File Name.
So what this going to do is, it is going to count the total number of lines which are available in that
file. So we see, we have around 249 Key Value Pairs, which are available in this file.
Which we want to insert to Redis. Okay.
So now the way to insert to Redis is, you use cat
command. Let me clear the screen once.
So the way to do is, you use cat
command, followed with the the File, which you have prepared in order to pass all this insertion to Redis.
Okay.
Just make sure the file, you have already prepared in the format, which is accepted by Redis.
Otherwise, you are going to get errors. So use
cat command
Space followed with the File Name and then you use a Pipe symbol and then you use Redis cli
and since we are running, we are not running it on a default Port of 6 3 7 9. We are running Redis server
on Port 6 3 7 0. So we also have to specify Port explicitly, by providing the Port and also in this
case we are running host.
I mean the Redis server itself is running on the same local machine. So we do not have to provide the
Host name, but in case you want to connect to Redis server, which is running remotely. So you can also provide
the Host name by typing -h and then the name of the Host name.
OK so it can be any IP address on which Redis is running.
So in this case we do not need this.
All right.
So that's basically it.
So what it will do is, it is going to read everything, which is there in Country Code Names and then going
to pass it to Redis cli, to perform the operation and let's see what is the response we get.
So I'm going to hit enter
and why we got response in this way is
because we use this command.
So basically what it did is, it passed all of those lines one by one to Redis cli and we got actually
the same number of response back.
Now what ever number of records which we had in this file.
OK.
This is not we want to do.
So we should have also put --pipe.
So what this will do is, it is going to give all of these instruction at once to Redis sever.
And then we are going to get only one Response back. OK we are not going to get these many number
of Responses back from Redis. So let's run this command. Hit enter and now you see this time we have got
only one response. OK, so it says, replies to 49 which means we have been able to update to 49
Key Value Pair into Redis.
And also there were 0 errors.
So you saw the difference between previous approach and this approach is, that in previous approach
we have got almost 249 Responses from Redis Server
And in this we have received only one Response and using this approach you can insert millions of record
in a very fast manner to the Redis Server. Now to verify the result
let's log into Redis Client
and we can run dbsize command to see the total number of Records which are inserted to Redis right
now.
So we see total 247 Key Value Pairs.
If you remember, in the file we had 249 total number of lines. Which means you might be thinking
that there should be 249 records inserted to Redis but that's not the case.
There may be you know, key value which is Duplicate.
So in the case of Duplicate, what Redis does is, it just updates the previous value with a new value or
let's say, if the Key Name was same, but it had a different Value. So the previous value whatever was inserted
is going to replace with the New Value.
Okay.
Now to illustrate this point, we can do something like, let's say we have a key which is called Color.
In which we have a value of Red.
And if I do Get Color, we are going to get back Red color.
Let's say I try to insert one more color, which is Yellow.
And once I do Get color, what do we think I'm going to get back in return.
If you think I'm going to get 2 values which is Red and Yellow, so that's not how Redis works.
So what it has done is, it has replaced, you know, whatever value which was against this key, which is called Color
to Yellow. Okay so I should be getting Yellow in return.
As you see when I run this Get command I get Yellow.
So basically if you have Duplicate Keys, so whatever the latest value which you have supplied with that
key is going to be your Last Value.
So that's the reason, total number of records which we see is only 247
okay.
I have cleared the screen by passing clear
command and then we are going to do keys *
So it will show us list of all the Keys, which are available in Redis and here we can see we have, these
many number of, we have now 248 total records and if I want to see any Country, you know Full Description
so I can use Get command and I can supply the Country Code
okay.
Something like this.
Now before we move on to next session,
let me show you something.
So
let me clear the screen.
And now to see whether the Redis process is running or not we can use ps -ef command.
If you are on Unix or Linux and then you can grep by Redis keyword.
So you are going to get something like this response.
Okay.
So this is the Redis process, which is running.
So here you can see Redis server, which is running and it is running on Local host which is being denoted
by this IP address and also it is running on, listening on Port 6 3 7 0.
Now let's say, if someone
actually kills this process or let's say the server reboots or there is any incident happens using which
Redis, Redis
application crashes and once this happens, we are going to see whether the data which was
in Redis still exist or not.
OK.
So to simulate that I'm going to type kill -9 and going to kill the process, which is running right
now.
Okay.
So once we do that, Redis process which was running on, running with this process idea is going to get
killed.
Now to make sure whether the process is killed, we can try to connect to Redis again by using
Redis cli
and this time you see when I, actually i supplied the wrong
Port.
Yeah.
So the process was previously running on this ports. So once we do this, now we can see we are no longer
able to connect to Redis.
Now
let me start Redis Server again by using Redis Server command
And if you remember we have also created a Configuration File, which is available inside
Redis -stable folder. So whenever I am starting the server.
I am also supplying the Redis Configuration File, which we have created.
So make sure whatever configuration file which you have created, you need to supply it whenever you are
starting Redis server.
And since I want to run it, in a , as a background process. I have also provided & in the end.
So hit enter and now Redis has started again.
You can verify the same by using ps-ef command again and grepping based on Redis keyword.
Now you can see Redis is running, on the process ID is this 2 6 6 4 9.
And the Port on which it is listening on is 6 3 7 0.
Let me clear the screen and let's connect to Redis , using Redis Client.
okay.
Now let's run dbsize command and see how many records are there after Redis has restarted.
So you see now, since we simulated Redis crash and we actually shut down Redis process and then we
started it again.
Now if I run dbsize, I do not see anything available in Redis. It is showing me 0
And if I do Keys * to see all the keys which are available in Redis. I do not get anything.
So basically whatever was stored in Redis has gone.
OK.
So if you remember from our initial tutorials, we have learnt that Redis by default keeps everything
in memory and by default there is no persistence which is enabled. OK.
So now we need to learn the way how to enable Persistence.
So let's say whenever Redis is failing or this server on which Redis is running is rebooting, we do not
want our data to lose. Okay, we don't want any data loss. So we are going to learn what are the different approaches
which we can follow to
keep our data.
Persistent.