Caching in NestJS: Using Redis To Improve Performance

Caching in NestJS: Using Redis To Improve Performance

Caching is a technique that improves the performance of an application and reduces the amount of time an application requires resulting in a better user experience.

It is a process of storing data in a way data is retrieved quickly the next time it is needed. When an application receives a request, it first checks if the requested data is already in the cache. If the data is present, it returns to the user without recomputing or fetching from a database. This results in significant performance improvements when data is accessed frequently.

Implementing Caching in NestJS:

NestJS provides several built-in features to implement caching in an application. Two popular caching tools are Redis and Memcached.

In this blog post, we will be implementing caching using Redis.

What is Redis?

Redis is short for Remote Dictionary Server. Redis is a caching system which temporarily stores information in a key-value data structure.

Redis cache is famous because it is available in almost all major programming languages.

Let's start by installing nestJS:

npm i -g @nestjs/cli
npm new nest-app

And further installing cache manager and redis cache manager:

npm install [email protected]
npm install [email protected]

Now let's create a folder inside the src folder named cache, with a cache module and a cache service file inside it to make things organised.


Here, in CacheService we have used:

  1. OnModuleInit: Called once the host module's dependencies have been resolved.
  2. OnModuleDestroy: Called after a termination signal (e.g., SIGTERM) has been received.

You can read more about NestJS lifecycle events.

The get method is used to 'get' cached responses by key, and the set method to 'set' key and its value for caching, we have a 3rd parameter ttl (time to live) that is the expiration time of cached data.

Now we set up our Cache Module.


Import RedisCacheModule in AppModule and use it inside AppService.


The getUser method is to get a user by id, we try to get the key value in our cache and if we get data, it returns quickly without calling the database (in this case I have used dummy data), else we get data from database and then set it in the cache and return the data. We have used the console to log cached responses when using cached data.

Now we set up our AppController.


Let's not forget to create redis server using Docker (download):

Create a docker-compose.yml file inside the project.

And then create a redis container inside it.


Now we are done, Let's test this out by starting our server:

npm run start:dev

In the first response, we get the data without anything on the console because no data with this key was inside the cache.

Not a Cached response

But, in the second call, we get data with a log in the console Cached Data which means this is a cached response.

Cached response

You can use different IDs, as we have not validated the requested id for testing purposes.

Congratulations, you have learned how to implement caching in NestJS.

Depending on your project's needs, there are various add-ons and techniques to implement caching. Read more about caching on the NestJS website.