Implementing Redis Cache in a Simple Nodejs Project

Enes Gür
Level Up Coding
Published in
4 min readSep 24, 2020

--

In this article, I am going to explain what Redis is, its advantages, and how to integrate it into a Nodejs project with a simple application.

What is Redis Cache

Redis is an in-memory data structure store, usable as a database (NoSQL), cache, and message broker. Redis stores data as “key-value” relationship. It stores data temporarily or permanently in RAM.

What is the Advantages of Redis Cache

  • Redis supports many data structures to keep like lists, strings, hashes etc.
  • Speed
  • Support for many programming languages
  • Open-source

So we can start by creating our app :)

It will be a simple movie app and we are going to use “http://www.omdbapi.com” API’s data.

In a terminal window, we are writing to these commands:

> mkdir Redis_Node-MovieApp

> npm init(I set entry point as “src/index.js”)

> npm install express node-fetch nodemon

And then we are creating an “index.js” file in the “src” folder (that's the entry point of my app) and we are adding the following code:

On the third line, we defined router for now and we will generate it soon

I prefer to use “async-redis” instead of “redis” for this app because Node Redis currently doesn’t natively support promises (this is coming in v4). We will divide our methods into sections using router folder, cache folder, data folder, and we will use async-await in order for the program flow not to continue in the get and set operations done to Redis Server and to wait for the response and return the response to the called method (prevent returning undefined). Redis does not support it natively, so I included the “async-redis” library in the project.

According to the document “This library does very little modification to the api of node_redis. It simply appends a promise resolving/rejecting callback for every command.” For more info click here.

> npm install async-redis

After we install the Redis library to our project, we are creating a “cache” folder and inside to “cache” folder, we are creating a “redisCache.js” file to do cache operations. We are writing the below code in in “redisCache.js” file:

After we implement our code which is get, set, and clear from Redis Cache, we are going to start to implement the data layer to get data from API. Creating “data_access_layer” folder and “data.js” file. In here we are implementing methods to get data from API. We are adding the following codes:

Now are we will be adding our routing for movie to route and manage requests. We will add the “routes” folder and inside it, we will add “movieRouter.js” and adding the following code:

The last step, we include the nodemon library in the project in package.json

Now we are ready for take-off, hold on tight …

We are passing to the terminal window and running the “npm run” command. If everything is okay, we can see the “Server started on port 3000” message in the terminal. For testing, we will be used “Postman” tool.

We can find movie id in “https://www.imdb.com/”. I am using Spider-Man movie’s id (tt0145487). We are making request to url:“http://localhost:3000/movie/tt0145487

Our first request, we took data from API and it took 537ms, this is a huge time and nobody does not want to spend and wait this time for every request. Our second request we took data from the cache because after we took data from API in the first request, we set our data to cache before sending a response to the client.

As we see, it took just 5ms and for 300 seconds our application to responded with data from the cache. We also implemented a clear cache method for our program. You can test it also too.

In this post, we saw the power of Redis and why developers generally choose it. You can access the source of this project here.

Hope to see you in the next post, I wish you healthy days.

Resources

--

--