Redis Crash Course
In this post we are going to learn about Redis, which is a must service for performance in apps which gets a lot of traffic.
This post have been inspired by this awesome YouTube video from Piyush Garg.
So, why do we need Redis. Look at the below image. Here a user hits a API server from the browser. Then whether it’s a GET request or a POST request, we get the data from a database.
Let’s assume it’s a GET request and the database is PostgreSQL. Now the data which the user wants might be in four diffeent tables and Foreign keys between them. So, the task will require a lot of time even with scaling. But here Redis comes to the rescue, which basically caches the data. And sends the data quickly back.
We also have a TTL(Time to Live) in Redis, and the data get refreshed periodically so that we don’t get a old data.
To install redis on our system, the best way is through docker. You can learn more about docker and to install it in an earlier post.
So, open a terminal and run the below command which we have got from redis site. It will use the redis port of 6379 and use our local port of 8001.
In docker desktop to can see redis-stack been added.
Now, go to http://localhost:8001/ and we will be asked for permission the first time.
It will open a Redis Browser which will also have a cli. Here, just write ping and we will get a pong response.
Also, in the terminal type docker ps which will return us the unique container id for our docker. After that run the command docker exec it <container_id> and we will be connected to redis.
After that give the command redis-cli and then we can again give the ping command, which will return back pong.
Now, redis have different data structures and we will look into them. The first is called Redis Strings. They are like key value pair and we create them through the SET command. Here, we first give the key, mostly like bike:1 and value like a string value.
Now, in the redis-cli we will set different values for bike. And to get them we have to use the GET command.
Now, in the Redis browser we will also see three strings and clicking on one of them will show it’s value.
In the terminal we will create a new string pair for car. And now in the redis browser, we can see a group.
We also have a property of NX and if we use it in SET, we get a unique key. In the below example, we are not able to use car:4 again.
Now, if we want to get more then one value of akey, we need to use the MGET command as below.
Now, if we add something say age as a number. We can use the incr command as below to increase it.
Since, we have increased the age by 4 previously in the browser we will see it as 42.
Now, we will create a small project with Redis. For this we have create a folder called redis-node and changed to it.
After that using npm init -y created an empty node project. Also installed an open-source package called ioredis in it.
We have opened the project in VS Code and created a .gitignore, client.js and string.js file. Here, in the .gitignore added node_modules.
Now, in the client.js file we are importing Redis from ioredis. And then creating a new instance of it called client. We are also exporting the client.
Now, in the string.js file we are importing client. And then created an init() function. Here, we are using client.get() to execute GET in redis.
When we are running node string.js from terminal, we are getting the correct result.
We can also use the set() of redis to create a new key-value pair of car:5 and tiago ev. And then we are getting the value of car:5.
Now, we will look into the concept of TTL(Time To Live) in redis. By default their is no limit.
Now, we will add expire of car:5 as 10. It means that after 10 seconds this value will be removed.
Now, after 10 seconds we will see the car:5 will be removed. Here, we have seen after 9 seconds, so it’s still there. In real world scenario the TTL is generally set to something like 6–10 hours. After that the data is removed and new data is fetched from database.
Now, we will look into Redis lists. As per the defination they are linked lists of string values.
Just like array in JavaScript, we need to push in it. Here, we use the lpush command and the list name, followed by the value.
Through list we can make a Queue or a Stack. If we insert from the left and remove from the right it’s a queue. If we insert at the left and remove at the right, it’s a stack.
Now, we remove from left we use the lpop command.
Now, opening the List in the redis browser we will see the heicopter list which currently have two items.
Now, we have added to elements to the right using the rpush and then with llen, we can see the list length.
We can also use blpop which removes elements from the head of the list. Here 10 means after 10 seconds
If we have removed all elements using blpop, it will be blocked as in the below case.
So, this will be unblocked after 10 seonds.
Now, create a file list.js in our project. Here, we have created a new list called aircrafts with lpush. We are pushing 5 aircrafts and after that with rpop removing one from the start.
Now, in redis browser we will see a list called aircrafts.
With the lrange command we can get the list of elements. Here, 0 is start and -1 is the end.
Now, we will learn about Redis sets. As per the defination it is an unordered collection of unique strings.
Here, we add a new member to the SET using SADD command. Notice that we are not able to add a duplicate item.
Now, in the Redis browser we can see the SET.
Now, we can remove a member from SET using the SREM command. And we can check if the member exists in a SET with the SISMEMBER command. It returns 0, if the member doesn’t exists. And it returns 1 if member exists.
Now, we will look into Redis hashes. As per the defination they are types structured as collections of field-value pairs.
To create hashes we need to use the HSET command. Here, we have given a motorcycle:1 as key. It contains the model as Splendor, brand as Bajaj, type as ‘Bajaj bikes’ and price as 49720.
We can think of hashes as array of objects in JavaScript.
We can do the same from the code also. Here, through hset we are adding values for motorcycle:2. And getting the model value through hget.
Next, we will look into Redis sorted sets. By defination it is a collection of unique strings, ordered by an associates score.
We have created a sorted set through the ZADD command called cars_brands. Notice that we have also given the score for all of the strings.
Now, we will look into Redis Streams which is actually like Kafka. You can learn more about kafka in our earlier post here.
We will use an example which is already given. It is that we will add a stream entry for a racer when he passes a location_id of 1.
Now, through the XADD command we created race:india. Here, different riders with their position are added and we have given a same location_id.
Now, to get the data we are using the XRANGE command and given the first unique id plus the count of 2.
Now, we will learn about Redis geospatial. By the defination it let’s us store the coordinates and search for them. With GEOADD we can add a location by longitute and latitude.
Now, we can find locations with GEOSEARCH command.
Next, we will look into Redis bitmaps. They are set of bit-oriented operations defined on the String type which is treated like a bit vector.
We use SETBIT and GETBIT in it as per the screenshot below.
Now, we will look into Redis Pub/Sub which is a Publish/Subscribe messaging paradigm.
We have opened a terminal and published two messages by PUBLISH notifications command.
In another terminal, we have connected to redi-cli again. After that SUBSCRIBE notifications will give us our notifications.
Now, back in our project we will see the concept of TTL and the caching of redis. We will be getting data from the famour jsonplaceholder API endpoint.
In the server.js file we have created a simple express app. Here, we are hittin the https://jsonplaceholder.typicode.com/posts and returning the data.
We are listening to PORT 9000 and also started the node app with node server.js command.
Now, we will hit http://localhost:9000/ from Thunder Client(Like Postman) in VS Code. Note the time to receive data is 617ms.
If we hit the GET endpoint again, it’s 550ms due to in-build caching.
We will update the code for server.js now. Here, we are importing client first, which have redis. Next, we have created a constant cacheValue which ises client.get(‘posts’) to get all posts.
if we have cacheValue then will return the cacheValue with res.json(). After the API call with axios, we are setting the posts with the data. But the data will expire in 60 seconds.
Again we have hit the endpoint with Thunder Client. First time it took 625ms.
Not next time it only took 6ms, since the data is cached with Redis.
Again hitting the API endpoint reduces it to 4ms.
But after expiration of 60 seconds, it’s again takes more time which is 540ms.
We can also see the sting for those 60 seconds in Redis browser. After which it is removed till the next hit.
This completes our Redis Crash course. You can get the code for the same here.