Javatpoint Logo
Javatpoint Logo

Redis Cache


Redis is an open-source data structure store that is primarily used as a database, message broker, or cache. We can assume Redis as a No-SQL database that stores in its memory specific key-value pair kind of data thereby supporting persistent data storage as and when required.

Additionally, Redis can support data structures of various types including sorted sets, hash maps and sets, strings, and so on. It also provides the backbone for various use cases constituting the adoption of specifically defined data structure delivery and performance optimization. One important aspect of using Redis is in a situation where we are required data to be fetched and delivered to the user or client in the minimum amount of time. It means that Redis is quite an efficient mechanism for carrying out time-specific actions.

What is caching?

Caching can be defined as a method of storing the exact copies of data or multiple files to allow applications to process data in a fast-paced environment. The caching is done primarily in the RAM itself since it is the fastest memory known and can access the data which are frequently being used or put onto. The main objective of caching is to achieve that speed and agility to carry out faster processing operations so that there's no delay.

Redis & Cache

The pre-requisites for using Redis cache are to be familiar with Node.js and its applications. In this example, we would be making a node.js application where we will define how caching is done with Redis. In general terms, the process of caching in Redis is quite simple. Whenever data is received from a caching enables route as a user request data, we first check if the received data already exists in the form of cache or not. If it already exists, it would be seamlessly sent to the user request's response since Redis is time-efficient. However, if the data doesn't exist in the cache, we may term it as a cache miss. To resolve this issue, we have to first get the data from the source database or the data from external API taken into consideration and is sent to the client notifying him. We also need to make it confirm that if the retrieved data is found next time in the cache, there should be a quick and seamless response.

Install Redis if it not present in your system through the following commands as shown below.

After this step, the next step is to make sure that the Redis is working properly with your server. To ensure that, we may require to ping it to check using the below command.

If you receive the response as pong, it means that your server is just working fine. We can now proceed with further implementations.

We might now need to set up our server post and add event listeners since we are working with node.js. To do that, we need to initialize the boilerplate for our application. Use the below code snippet as a reference.

Now, we might use any external API for our application. We might also consider that the data received from the external API is in the JSON format since we are working with node.js. A sample API request might look something like this.

Note: The above link is just an example of how a sample request of adopting an external API takes place. It is just a sample representation of API requests from GitHub.

Now, we need to define the external routes from the API that would receive data and send them back to the client. This is how it is done.

Now, the next and the main task that we are here to achieve is to establish cashing. It can be done by establishing the connection of our application to the Redis server. It can be done if we have installed the Redis package earlier. The code is shown below:

We are almost done now. The last task now is to compare the received data from the cache. To check if the data is present or is null is done by using the example code snippet shown below.

In the above code snippet, to get data from the external API, we need to first store it in the Redis and is then sent it to the node server so that it can respond with data and not just making a request from the API. The setex function is used to store data in the cache. Using this function alone instead of a generic regular function may not allow us to set the time. Thus, there's an advantage of using this function to set time in the form of key-value pairs. The set value of specified time will alert Redis and it would automatically remove the key-value pair from the cache when the time interval elapses or expires.


In this tutorial, we have covered almost all the prospects of Redis and the caching mechanism. The primary aim of this tutorial is to discuss the Redis cache from scratch. We came across what is Redis and how caching is achieved through it. We have also taken into account some coding examples and how to determine if the caching data already exists or is NULL. We finally came across the error message displaying a method that responds as an error from the external API if there's no data present or it simply doesn't exist.

Next TopicRedux Thunk

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA