We will discuss about Redis Cache and different ways to install and configure it.
Agenda
- Overview
- What is caching?
- Redis Cache
- Redis Cache server installation
- Redis Cache Image using Docker
Overview
Caching is very popular nowadays in the software industry because it will improve the performance and scalability of the application. We use many web applications like Gmail and Facebook and see how responsive they are and we have a great user experience. There are a lot of users using the internet and if an application has huge network traffic and demand, we need to take care of many things which help us to improve the performance and responsiveness of the application. So, because of that, there is the solution of caching and that’s why caching comes into the picture.
What is caching?
The cache is the memory storage that is used to store the frequent accessed data into the temporary storage, it will improve the performance drastically and avoid the unnecessary database hit and store frequently used data into the buffer whenever we need it.
As you see in the above image there are two scenarios, one is without using the cache and another is with the cache. So here when we do not use the cache, in that case, suppose users want data then they will hit each time database and it will increase the time complexity and reduce performance in case there is some static data users want and it is the same for all users. In that case when we do not use cache then each one hits the unnecessary database to fetch data. On the other side as you can see we use the cache, and in that case, if there is the same static and the same data for all users then only the first user will hit the database and fetch data and store it into the cache memory and then other two users use that from the cache without unnecessarily hit to the database to fetch data.
Redis Cache
- Redis is an Open Source (BSD Licensed) in-memory Data Structure store used as a database.
- Basically, it is used to store the frequently used and some static data inside the cache and use and reserve that as per user requirement.
- There are many data structures present in the Redis which we are able to use like List, Set, Hashing, Stream, and many more to store the data.
Redis Cache server installation
Here, we install Redis Cache Server on our local system and use it.
Step 1
Download the Redis Server using the following URL
https://github.com/microsoftarchive/redis/releases/tag/win-3.0.504
Step 2
Extract the zip file and later on open the Redis Server and Redis CLI
Here we can see our Redis Server is running on port number 6379
Step 3
Next, we are going to open Redis CLI
As we can see, here we used a few Redis Cache CLI commands.
- Keys * - This command is used to get a list of keys that are present in our Redis Cache server.
- SET <key name> "key value" - This command is used to set the key value.
- GET <key name> - This command is used to get the value of the key.
- PING - This command is used to check our Redis Server is running.
- FLUSHALL - This command is used to delete all keys which are present inside our Redis Server.
These are only a few commands which we used frequently. If you want more command details then check Redi’s official documentation. ( https://redis.io/docs/manual/cli/)
Redis Cache Image using Docker
Here we are going to use Redis Cache Docker Image using Docker Desktop
Step 1
First, Install the Docker Desktop on our machine.
https://www.docker.com/products/docker-desktop/
Step 2
Next, Install the docker desktop and run the exe
Step 3
Open command prompt and execute following command. (Make sure your docker desktop is running mode)
docker pull redis
Here we can see after executing the above command the docker image is downloaded and we can see it inside the docker desktop.
Step 4
Now, we are going to run Redis image using following command with different parameter like container name, port number, and a docker image.
docker run --name myredis -p 6379:6379 -d redis
docker ps
This command provides you with container details in which your Redis image is running as I shown above
Step 5
Next, we are going to connect Redis which is running inside the container.
docker exec -it myredis sh
So, this is how we used Redis cache using docker.
If you want the practical implementation of Redis using .NET Core Web API then check my following article related to that.
Conclusion
Here we discussed Redis Cache and different ways of installation step-by-step. Also, discussed a few commands which we are used frequently.
Happy Learning!