A cache store in NestJS is a place where data is temporarily saved to speed up repeated requests. It helps avoid fetching or computing the same data multiple times.
Click to reveal answer
beginner
How does the in-memory cache store work in NestJS?
The in-memory cache store keeps data inside the app's memory. It is fast but data is lost if the app restarts. It is good for small, temporary caching.
Click to reveal answer
intermediate
Why use Redis as a cache store instead of memory in NestJS?
Redis stores cache data outside the app in a separate server. It keeps data even if the app restarts and can share cache between multiple app instances.
Click to reveal answer
intermediate
How do you enable caching with Redis in a NestJS app?
You install Redis, then configure NestJS CacheModule with a Redis store by providing Redis connection details. This lets NestJS use Redis to save and get cached data.
Click to reveal answer
beginner
What is a key benefit of using cache stores in web applications?
Cache stores reduce the time to get data by saving results of expensive operations. This makes apps faster and reduces load on databases or APIs.
Click to reveal answer
Which cache store keeps data only while the app is running?
AIn-memory cache
BRedis cache
CDatabase cache
DFile system cache
✗ Incorrect
In-memory cache stores data inside the app's memory and loses it when the app stops.
What is a main advantage of Redis over in-memory cache in NestJS?
AIt is slower
BData persists after app restarts
CIt uses more CPU
DIt only works locally
✗ Incorrect
Redis stores data outside the app, so cached data stays even if the app restarts.
How do you add caching with Redis in a NestJS app?
AConfigure CacheModule with Redis store and connection details
BUse in-memory cache only
CInstall Redis but do not configure NestJS
DWrite cache logic manually without modules
✗ Incorrect
NestJS CacheModule can be configured to use Redis by providing connection info.
Which of these is NOT a benefit of caching in web apps?
AFaster data retrieval
BReduced database load
CPermanent data storage
DImproved app performance
✗ Incorrect
Cache is temporary storage, not meant for permanent data saving.
What happens to in-memory cache data when the NestJS app restarts?
AData moves to Redis
BData is saved automatically
CData is backed up to disk
DData is lost
✗ Incorrect
In-memory cache is cleared when the app stops or restarts.
Explain how caching with Redis works in a NestJS application and why it might be preferred over in-memory caching.
Think about app restarts and multiple servers.
You got /4 concepts.
Describe the role of cache stores in improving web app performance and give examples of when to use memory cache versus Redis cache.
Consider speed, persistence, and app size.
You got /5 concepts.
Practice
(1/5)
1. What is the main advantage of using Redis as a cache store in a NestJS application compared to in-memory cache?
easy
A. Redis does not support expiration of cached data.
B. Redis cache is stored only in the local app memory.
C. Redis allows sharing cached data across multiple app instances.
D. Redis cache clears automatically every second.
Solution
Step 1: Understand in-memory cache scope
In-memory cache stores data only inside the app instance's memory, so it is not shared.
Step 2: Understand Redis cache scope
Redis is a separate server that multiple app instances can connect to, sharing cached data.
Final Answer:
Redis allows sharing cached data across multiple app instances. -> Option C
Quick Check:
Redis cache is shared [OK]
Hint: Redis shares cache across servers; memory cache is local only [OK]
Common Mistakes:
Thinking in-memory cache shares data between servers
Believing Redis cache is local to one app instance
Assuming Redis cache clears every second automatically
2. Which of the following is the correct way to import and register the Redis cache store in a NestJS module?
easy
A. CacheModule.forRoot({ type: 'redis', url: 'redis://localhost:6379' })
B. CacheModule.register({ store: 'redis', host: 'localhost', port: 6379 })
C. CacheModule.useRedis({ host: 'localhost', port: 6379 })
D. CacheModule.registerRedis({ url: 'redis://localhost:6379' })
Solution
Step 1: Recall NestJS CacheModule syntax
CacheModule.register() is used to configure cache with options including store type and connection details.
Step 2: Identify correct Redis store option
Using store: 'redis' with host and port is the correct way to set Redis cache in NestJS.
But your app throws a connection error. What is the likely cause?
medium
A. The port should be a number, not a string.
B. Redis store does not support host option.
C. CacheModule.register cannot be used for Redis.
D. Missing password option causes connection failure.
Solution
Step 1: Check data types in config
Port should be a number (6379), not a string ('6379'), or connection fails.
Step 2: Validate other options
Host is valid, CacheModule.register supports Redis, password is optional if Redis has no auth.
Final Answer:
The port should be a number, not a string. -> Option A
Quick Check:
Port must be number type [OK]
Hint: Use number type for port, not string, in Redis config [OK]
Common Mistakes:
Passing port as string instead of number
Assuming host option is invalid
Thinking password is always required
5. You want to cache user profile data in Redis with a TTL of 10 minutes in NestJS. Which code snippet correctly sets this cache and retrieves it safely?
hard
A. await cacheManager.set('user_123', profile, 600); const data = cacheManager.get('user_123');
B. await cacheManager.set('user_123', profile, { expire: 600 }); const data = await cacheManager.get('user_123');
C. cacheManager.set('user_123', profile, { ttl: 600 }); const data = await cacheManager.get('user_123');
D. await cacheManager.set('user_123', profile, { ttl: 600 }); const data = await cacheManager.get('user_123');
Solution
Step 1: Use correct TTL option in set()
TTL must be passed as an object property { ttl: 600 } for 10 minutes (600 seconds).
Step 2: Await both set and get calls
Both set and get are async, so await is needed to ensure completion and data retrieval.
Final Answer:
await cacheManager.set('user_123', profile, { ttl: 600 }); const data = await cacheManager.get('user_123'); -> Option D
Quick Check:
TTL as { ttl: 600 } and await both calls [OK]
Hint: Pass TTL as { ttl: seconds } and await async cache calls [OK]
Common Mistakes:
Passing TTL as a number directly instead of in object
Not awaiting async cache get call
Using wrong option name like expire instead of ttl
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.