NestJs Caching With Redis

The ultimate guide to implementing caching in NestJs with Cache Interceptor, Cache Manager and Redis

Vladimir Agaev
9 min readJul 30, 2022

--

Photo by Jandira Sonnendeck on Unsplash

Congratulations! You have deployed a NestJs application that is gaining traction! A lot of users are using your app, the traffic goes viral.

At some point, you receive emails complaining that your website is slow. You’ve probably heard that caching can solve the problem, but you are unsure how to implement it.

You came to the right place!

In this article, I will explain caching, why you need it and how to implement it in your NestJs application.

What is Caching?

Before we start, please note that you can find the github repository with the completed project

Caching is a fairly old technique designed to improve your application’s performance and reliability.

Caching involves saving frequently requested data in an intermediary store called the “cache store” to avoid unnecessary calls to the primary database.

An HTTP request asking for data cached by the server will receive it directly from the cache store instead of getting it from a database. Which is much faster!

Why do you need caching?

Any web application that has some success will eventually run into bottlenecks. The most common bottleneck is usually related to how information is fetched from a primary database, like Postgres or MySQL.

Indeed, as the number of users grows, so does the number of HTTP requests made to the server. This results in the same data being fetched all over again and again. Optimizing your application for speed and efficiency is important by caching frequently requested data.

Since most relational databases involve structured data, they are optimised for reliability and not for speed. That means the data they store on a disk is many times slower than the RAM. Using a NoSQL database does not bring any tremendous performance gains either.

--

--