Complete Guide on Nest JS Caching | Redis
Summary
TLDRThis tutorial explores caching in NestJS, comparing in-memory caching to Redis for scalability and persistence. It demonstrates how to implement caching using the CacheManager package, set up Redis as a cache store, and utilize cache interceptors for automatic caching of data fetched from the database.
Takeaways
- 📒 Caching in NestJS can be implemented in multiple ways based on the project needs, primarily through in-memory caching and Redis.
- 📝 In-memory caching stores data directly in the application's memory, acting like a quick reference guide for fast access.
- 🚀 In-memory caching is fast and easy to set up, ideal for small-scale applications but has limitations with larger data volumes or multiple server instances.
- 🔄 Redis can be used for larger applications, ensuring consistency and efficiency by storing data in a dedicated data store.
- ⚙️ Implementing caching in a NestJS application involves installing the necessary packages and configuring the CacheModule.
- 🗂️ The CacheModule can be registered globally, making it available across the entire application, or locally within specific modules.
- 📥 Caching data with Redis involves setting up the Redis store in the CacheModule and configuring the host and port.
- 🔧 Caching interceptors can be used to automatically cache responses from specific routes or controllers in NestJS.
- 🔄 Redis provides benefits such as scalability and persistence, ensuring cache data is retained even if the application restarts or crashes.
- 📈 Using Redis for caching involves setting up a Redis container, interacting with Redis CLI, and verifying cached keys and data.
Q & A
What is caching in the context of a Node.js application?
-Caching in Node.js refers to the process of storing frequently accessed data in a temporary storage area to improve the performance of an application by reducing the need to fetch the data from its original source repeatedly.
What is the primary advantage of in-memory caching in Node.js?
-The primary advantage of in-memory caching is its speed. It allows for quick access to data stored directly in the application's memory, which is ideal for small-scale applications and does not require any external tools.
What limitations does in-memory caching have as an application grows?
-In-memory caching may struggle to keep up as the application grows due to its inability to handle large volumes of data or maintain consistency across multiple server instances.
What is Redis and how does it help overcome the limitations of in-memory caching?
-Redis is a data store that can be used for caching purposes. It helps overcome the limitations of in-memory caching by providing a shared, persistent, and scalable cache that can be accessed across different instances of an application.
What is the role of the 'CacheModule' in a NestJS application?
-The 'CacheModule' in NestJS is used to set up and configure caching within the application. It allows for the registration of various caching options and settings, such as the maximum number of keys and time-to-live (TTL) for cached data.
What does TTL stand for in the context of caching?
-TTL stands for 'Time To Live', which is the duration for which cached data is considered valid before it needs to be refreshed or expired.
How can you implement caching in a NestJS application?
-To implement caching in a NestJS application, you need to install the required packages, import the 'CacheModule' in the AppModule, register the caching options, and then inject the cache manager into your service classes to interact with the cache.
What is an 'Interceptor' in NestJS and how does it relate to caching?
-An 'Interceptor' in NestJS is a class annotated with the @Interceptor() decorator that can intercept incoming requests before they reach their respective handlers. In the context of caching, a cache interceptor can check for a cached response and return it if available, otherwise, it allows the request to proceed and caches the response for future requests.
How can you use Redis as a cache store in a NestJS application?
-To use Redis as a cache store in a NestJS application, you need to install the 'cache-manager-redis-store' package and its TypeScript type definitions. Then, configure the 'CacheModule' to use Redis by setting the 'store' property to 'RedisStore' and providing the host and port of the Redis instance.
What is the purpose of using a Docker container for running Redis?
-Using a Docker container for running Redis allows for easy deployment, isolation, and management of the Redis service. It ensures that the Redis cache store is separate from the application environment, promoting better resource management and consistency.
How can you verify if data is being cached in Redis?
-You can verify if data is being cached in Redis by using the Redis CLI (Command Line Interface) to connect to the Redis instance and executing the 'KEYS' command to list all cached keys or the 'GET' command to retrieve the cached data for a specific key.
Outlines
📚 Introduction to Caching in Njs
This paragraph introduces the concept of caching in the context of Njs applications. It explains two primary methods of caching: in-memory caching, which is likened to a sticky note for quick access to data stored directly in the application's memory, and using a data store like Redis for larger applications or those requiring data consistency across multiple server instances. The paragraph emphasizes the speed and simplicity of in-memory caching for small-scale applications and the scalability and persistence advantages of Redis for larger applications. It also outlines the steps to implement caching in a Nest application, including installing necessary packages, setting up the cache module, and creating methods to interact with the cache manager.
🛠 Implementing Caching and Auto-Caching in Njs
In this paragraph, the tutorial continues with the practical implementation of caching within a Nest application. It describes how to set up and interact with the cache manager, including methods for setting and getting cache keys, as well as deleting and resetting the cache. The paragraph also introduces the use of cache interceptors for auto-caching data fetched from the database, demonstrating how to bind the interceptor to a controller to cache all GET routes. It further explains the process of using Redis as a cache store, including installing the necessary packages, configuring the cache module to use Redis, and verifying that data is being cached in Redis by interacting with the Redis CLI.
🔄 Utilizing Redis for Persistent Caching in Njs
The final paragraph focuses on the integration of Redis as a persistent cache store in a Nest application. It discusses the importance of using Redis to maintain cache data even when the application restarts or crashes, ensuring data consistency and availability. The tutorial guides through the process of setting up Redis as a cache store, including configuring the host and port, and troubleshooting potential type mismatch errors by ensuring the correct version of the cache manager store package is installed. The paragraph concludes with a demonstration of how data is now being cached in Redis and the improved performance observed when fetching data from the cache.
Mindmap
Keywords
💡Caching
💡NestJS
💡In-memory caching
💡Redis
💡Cache Manager
💡TTL (Time To Live)
💡Interceptor
💡Docker
💡Postman
💡Scalability
💡Persistence
Highlights
Introduction to caching in NestJS with a focus on its importance and implementation.
Explanation of in-memory caching as a quick reference guide for small-scale applications.
Limitations of in-memory caching when scaling applications and handling large volumes of data.
Introduction of Redis as a data store for caching to ensure consistency and efficiency across multiple server instances.
Practical guide on choosing between in-memory and Redis caching based on project needs.
Step-by-step tutorial on implementing caching within a NestJS application.
Installation of required packages for caching and setting up the CacheModule in the AppModule.
Configuration of cache properties such as max keys and TTL (Time To Live).
Injection of the CacheManager instance into a class using the CacheManager token.
Creation of methods to set and get cache keys, demonstrating basic cache interactions.
Demonstration of a simple NestJS application running in a Docker container to test caching endpoints.
Use of Postman to interact with the application's caching endpoints and observe cache behavior.
Introduction of additional methods for cache management such as delete, reset, and get all keys.
Explanation of auto-caching data when fetched from the database using cache interceptors.
Setting up a new user module with an endpoint for auto-caching and demonstrating its effectiveness.
Advantages of using Redis over in-memory caching, including persistence and scalability.
Installation and configuration of Redis as a cache store in a NestJS application.
Verification of Redis cache data using the Redis CLI and the impact on application performance.
Conclusion summarizing the implementation of caching in a NestJS application and its benefits.
Transcripts
today we'll dive into njs and Tackle an
important topic called caching I'll
break down caching in njs into simple
terms and we'll show you how to
implement it but before we start let's
understand how caching Works in SGS we
have multiple ways on how we can cach
data in our application based on our
project needs first up we have inmemory
caching imagine it like having a sticky
note on your desk where you quickly
write down important info in njs the
application stores data directly in its
memory so when a request comes in it
checks this fast ACC memory first before
doing any heavy lifting it's like having
a quick reference guide that saves a lot
of time it is super quick for small
scale applications and is easy to set up
and doesn't require any external tool
however it has limitations as your
application grows the in-memory cache
May struggle to keep up it's great for
Simplicity but not always the best for
handling large volumes of data or
multiple server instances so in order to
resolve this issue we can use a data
store like RIS when your nest app needs
to cat something it sends the data to
radies and whenever it needs the data
again it checks radies first it's like
having a team wi memory ensuring
consistency and efficiency across the
board so which one to use in a nutshell
if you're working on a smaller project
where Simplicity is the key inmemory
caching might be a go-to it's fast and
straightforward on the other hand if
you're dealing with a larger application
handling a substantial amount of data or
need to share their data across
different instances redus could be the
hero it brings scalability and to the
table next let's Implement caching
within our
application all right let's see this in
action I've created a simple Nest
application for this tutorial to start
with I'll first install the required
packages for
caching once the packages are installed
next let's import cache module in app
module
file in Imports we can type cache
module and import it from njs Cache
manager package next let's import the
register
method this register method takes few
optional
properties first we have
Max which is a maximum number of keys we
can store in cash memory let's set it to
100 next we have
TTL which is the cash expiry
time now if I open Package notice the
version for cash manager it is five so
if we are using cash manager of version
four we need to Define TTL in seconds if
it is greater than four then we need to
Define TTL in
milliseconds now if we want that we
don't want our cash to expire we can set
it to
zero now since we have registered cache
module let's see in memory caching so to
interact with cash manager instance we
need to inject it to our class using
cash manager
token I'll head over to app
service I'll create a Constructor
first then I'll inject our cash manager
tokon which is imported from Nest cash
manager
package next I'll create an instance for
a cash manager
class which is imported from cash
manager next I have created two methods
to set and get cach keys set cach key
will accept two parameters key and
value and we'll set the key in the cache
memory get cach key method will accept
key as the parameter and we fetch the
value for that key from the cach memory
if the key does not exist in memory it
returns null next I have created route
for these two methods let's run our app
first and hit these endpoints
to run our app let's create a Docker
Image
First Once the image is built let's run
it in a
container we can see our app is running
next let's open Postman and hit these
end
points
we have set two keys first name and last
name now let's try to get these keys
I'll try to get first name first so I'm
getting the data similarly let's try to
get the last name key as
well now let's try to get a key that we
haven't set so far let's say
age so in this case since we haven't set
this key we are getting nulls data next
I added few more methods
to delete a cach key to reset the whole
cach memory this will delete all the
keys and to get all the keys in our cash
tour also have added routes for these
three methods as well next let's open
Postman let's first try to get all the
keys from our store so we have two keys
that we created
previously let's try to delete one of
these Keys let's try to delete first
name this will delete our key now if I
try to get first name we'll get
null similarly let's try to reset our
cache this will clear our cache so if I
try to get all the keys we don't have
any key
now this is not an ideal way how we cat
data in our
application we don't have to hit this
end points every time we want to save
data in Cache memory these endpoints
were just for this tutorial to show how
caching Works in SGS and how we can use
inbu cache manager to fetch the cach
data n provides a mechanism to autoc
cach the data when it's fetched from DB
let's see that in
action I've created a new user module
for autoc
caching I'll create a new endpoint to
get data and we'll try to autoc catch
that
data first I'll head over to app module
in my cache modu register method I'll
register a new property is
global and we'll set it to
true if you want to use cache module in
other modules in our application we have
to import it in those modules but since
we set is global property to true it
will be loaded in root module and will
be available to use in other
modules in my user service I've created
a new method get users which is
returning test users data from Json
pleas soer
API next let's create an endpoint for
this
method in my user controller I've
created a get endpoint to get all
users now if we want to cache data for
this route we can bind cache Interceptor
here so after our get decorator let's
import use interceptors decorator which
excepts an Interceptor let's add cach
Interceptor which is important from Nest
cash manager package what it does is it
intercepts the incoming requests before
they reach their respective
handlers it checks if there is a cach
response available for the request if
there is it Returns the cach response
instead of forwarding the request to the
Handler if there isn't a cach response
the Interceptor allows the request to
proceed to the Handler and once the
request is generated it caches it for
future requests now if you want to cach
all routes in this controller we can B
cache Interceptor at controller level so
if we remove it from here and add it
after for controller decorator in this
way it will cach all get routes within
this controller next let's head over to
postman and hit this
endpoint we can see that we don't have
any keys in Cache
memory now if I hit this endpoint first
time it will take some time to fetch
data we can see that it took 97
milliseconds to fetch users data but if
we hit endpoint again we can see it f
data in a very less time because this
time it Fest data from cach memory now
if we see the cache key again we can see
that it takes the route as key since we
didn't set any key explicitly for this
route I'll head over to vs code
again and my user controller after I've
defined the get
aurator i'll import the cach key
aurator and here I'll Define the key for
this route
let's name it users now the next time I
hit this endpoint it will store the
value for this route against the users's
key so far so good if we can cache a
data in inbuilt Cache memory then why do
we need third party stores like radius
the reason is inmemory cache will reset
every time our Nest application is
restarted or even crashed so we need a
store like redes to store cash data so
that even if our app is crashed or even
restarted we don't lose our cash data
although this is not the only reason to
use redis there are many other
advantages as well of using redis like
its scalability persistence Etc next
let's add ready store in our Nest
application now in order to use redis we
need to install two more packages cash
manager red store package allows us to
use redis as a cash store with cash
manager
next we need to install this package
which provides typescript type
definitions for cash manager at store
package we need to install it as a
developer
dependency once both packages are
installed we can head over to app module
and import our package then in a
register method we can add another
property called store and point it to
ready store by default it points to in
memory store then we need to add
host and Port of our red store so Port
is by default
6379 and for host since we are running
redes in Docker we need to define the
container
IP so first let's head over to Docker
and check if our radius image is running
then let's open our terminal and type
Docker PS this will list down our
running
containers then copy the container ID
for Rus and type talker inspect
and then paste this
ID we'll get the IP address for our
container let's copy
this and paste here now we are getting
an error for store property it says that
there is a type mismatch for the
property that means it is expecting a
value that we are not providing so if we
head over to package
J we can see that we have installed the
latest version for manager at store
which is version three we need to decate
this version and provide version two
here so let's open
Terminal let's clear it first and
uninstall our
package then let's install version two
for this
package so this error should be gone now
now once we have set up our ready store
let's hit the endpoint again and see if
data is being cached in radi or not
let's open open Docker first then in
containers click on this red is
container go to
execute Tab and type redis hyphen CLI to
interact with the radius CLI then type
Keys Asis to list down all the keys we
can see that there's no key currently in
redish then open Postman and hit the
endpoint
again we got the data and it took about
700 milliseconds to fetch the
data let's see if we got the key in
radius or not let's type key asri again
and we can see that we now have users
key in redis if you want to get the data
we can type get and then users and we
can see that we have our cach
data now if I hit the endpoint
again it took just 14 milliseconds to F
the data now because it f the data from
ready cash and that's it with just a few
lines of code we have implemented
caching in our Nest application thanks
for tuning in I'll see you in the next
video
Ver Más Videos Relacionados
Redis Tutorial for Beginners #1 - What is Redis?
Cache Systems Every Developer Should Know
Redis Deep Dive w/ a Ex-Meta Senior Manager
Spring Boot Cache Annotations || Cache Providers || Where to set Caching Policy || Green Learner
Caching demystified: Inspect, clear, and disable caches #DevToolsTips
What are Distributed CACHES and how do they manage DATA CONSISTENCY?
5.0 / 5 (0 votes)