Introduction to Redis cache using NodeJS

Kamal Walia
Nerd For Tech
Published in
4 min readJun 19, 2022

Hello everyone today I would like to introduce you to the basics of caching and how it can help you in boosting your application performance.

As you can guess from the title we will be using Redis cache as our caching service with NodeJS.

Let’s first start with the basics of caching but before that let's first try to understand, what is cache memory?

In computing, a cache is a high-speed data storage layer (Hardware or Software) that stores a subset of data, typically transient, so that future requests for that data are served up faster than is possible by accessing the data’s primary storage location. The data stored in a cache might result from an earlier computation or a copy of data stored elsewhere.

A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. The more requests that can be served from the cache, the faster the system performs.

Now that you understand the concept of cache memory, it will be easier for you to understand what caching is — the answer is simple caching is the process of storing data into a cache.

Now let’s talk about Redis:-

From the official documentation (redis.io), Redis is an open-source, in-memory data store.

It can be used as a database, cache, streaming engine, and message broker.

Now that you have an understanding of what Caching and Redis are, let’s build a very basic project that implements caching using Redis.

If you are on MAC install user Homebrew to install and get started on Redis

First, make sure you have Homebrew installed. From the terminal, run:

% brew --version

If this command fails, you’ll need to follow the Homebrew installation instructions.

Installation

From the terminal, run:

% brew install redis

This will install Redis on your system.

Starting and stopping Redis in the foreground

To test your Redis installation, you can run the redis-server executable from the command line:

% redis-server

If successful, you’ll see the startup logs for Redis, and Redis will be running in the foreground.

To stop Redis, enter Ctrl-C.

If you face any issues or want to install it on a different OS then Checkout this link Redis Getting Started

Now, let’s start working with Redis

  • Create a new directory
% mkdir redis-cache-demo
  • Navigate to the new directory
% cd redis-cache-demo
  • Generate a package.json file
% npm init — force

— force tells npm to use the default options while generating the package.json so you are not asked any questions.

After running the command above, you should have a package.json file in the redis-cache-demo directory.

Create a file server.js in the directory.

Install Express, Redis, and node-fetch npm modules with the below command

npm install — save node-fetch express redis

We will import modules as per newer ES specifications, so add the following line in package.json if it’s not already there.

“type”: “module”,

Your package.json now should look like this:-

Now that you have Redis installed, add the following code to the server.js file.

Now you are ready to start your node server with node server.js command from your terminal.

Now to test our basic implementation open postman to make a request to the todos endpoint.

First request:-

Non-cached result

Second Request:-

Take note of the time in the images above.

The first request took 323 milliseconds while the second one only took 5 milliseconds.

Conclusion:-

Redis is a very powerful in-memory data-store that we can use in our applications to boost its performance. It’s very simple to save and get data without much overhead.

P.S. This post is in collaboration with Redis.

--

--

Kamal Walia
Nerd For Tech

👨‍💻 Software Engineer | Tech Enthusiast | Wordsmith 📝