We will be understanding the basics of Redis, set up in a local machine, and compare the results of endpoint(API) created with Redis and without Redis.
What is Redis?
Redis is an open-source, in-memory data structure. In-memory means all the data we store in Redis is not stored in any physical hard drive, it is stored in RAM. As we all know RAM is a volatile memory (i.e the data is not stored permanently it gets erased when the machine is shut down or it is restarted). So whatever data we stored in Redis will also get erased.
Why do we need Redis?
As we saw that Redis stores the data in RAM, So we can use this for caching data that is not frequently changing. So that when any resource is requested it does not iterate in DB table rows, if it is present in Redis cache it serves from there.
When we see the first image we can see that the user makes a request to the server asking for post1 details. Server query database for the post. So it takes 400ms for the round trip. Now let's introduce the Redis in between server and database calls. So when the user makes a request to the backend server, before querying the database it checks for the key present in Redis if it is already present it responds to the user with the post info, else it goes for a DB call and responds to the user, also saves a copy into it so next time when the same request is done it can respond without DB call and for this time taken is 40ms (considering that post p1 is in Redis) which is 10 times faster than the previous approach.
Setting up Redis in Local
In mac, we can use homebrew to install Redis. For windows, the process is a bit complicated. we need to install the windows subsystem for Linux. There are many videos/articles available to set up subsystems. Once that is done enter the below command.
sudo apt-get install redis
This will download and install the Redis on your machine and listen to the connections on default port 6379.
So now we have redis up and working in local machine. There is one more easy way to install Redis if you are aware of Docker, we can the docker tool on windows and enter the below command in the terminal.
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
This will pull the docker image and spin it up in a container. We can interact it with CLI.
Now to run the redis-cli enter the command “redis-cli” in the command prompt opened from the above screenshot.
Basic Commands
Strings
- SET <keyname> <value> → Sets a key value pair in redis store.
- GET <keyname> → Reads a value with a given key name.
- DEL <keyname> → Deletes a value with a given key name.
- EXISTS<keyname> → Check if the given key is present in redis.
- KEYS * → Returns all the keys in redis.
- FLUSHALL → Removes all the keys.
- TTL <keyname> → Returns the time to live of key (-1 indicates that there is no expiry)
- EXPIRE <keyname> <Seconds> → Sets the expiry for key
- SETEX <keyname> <Seconds> <value> → Set the key value with the given expiry in seconds.
Array
- LPUSH <key name> <value> → Push an element to left of array
- LRANGE <Keyname> <start_range><end_range> → View the list of all elements in array
- RPUSH <key name> <value> → Push an element to right of array
- LPOP <keyname> → Remove the element from left.
- RPOP <keyname> → Remove the element from right.
SET
It stores only the unique values.
- SADD <keyname> <value> → Add item to set.
- SMEMBERS <key name> → View the list of all items in set.
- SREM <keyname> <value> → Removes the value from set.
HASHES
Hashes are like JSON, they can not have a circular link between nodes.
- HSET <key> <field> <value> →Set the value to given keys field
- HGET <key> <field> → Returns the value of key
- HGETALL <key> → Returns all key value pairs.
- HDEL <key> <field> → Del the key value.
- HEXISTS <key> <field> → Check if the given key is present in Hashtable.
So this is just the basic introduction of Redis and some basic operations explained. Will be writing another story for Nodejs implementations with Redis. Do clap if you find this article interesting, it motivates the authors.