Challenge - 5 Problems
Cache Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when using NestJS CacheModule with memory store?
Consider a NestJS service that caches a value in memory using CacheModule. What will be the output of the following code snippet when retrieving the cached value?
NestJS
import { Injectable, Inject, Module } from '@nestjs/common'; import { CacheModule, CACHE_MANAGER } from '@nestjs/cache-manager'; import type { Cache } from 'cache-manager'; @Injectable() export class SampleService { constructor(@Inject(CACHE_MANAGER) private cacheManager: Cache) {} async cacheAndGet() { await this.cacheManager.set('key', 'hello'); return await this.cacheManager.get('key'); } } @Module({ imports: [CacheModule.register()], providers: [SampleService], }) export class AppModule {}
Attempts:
2 left
💡 Hint
Think about what CacheModule.register() uses by default for storage.
✗ Incorrect
By default, NestJS CacheModule uses an in-memory store. Setting a key and then getting it immediately returns the stored value.
📝 Syntax
intermediate2:00remaining
Which option correctly configures Redis cache store in NestJS?
You want to configure NestJS CacheModule to use Redis as the cache store. Which of the following code snippets correctly sets up Redis as the cache store?
Attempts:
2 left
💡 Hint
The Redis store requires a specific package to be passed as the store option.
✗ Incorrect
To use Redis with NestJS CacheModule, you must pass the Redis store package (like 'cache-manager-redis-store') as the store option. Other options are incorrect or incomplete.
❓ state_output
advanced2:00remaining
What happens to cached data when using memory store in a clustered NestJS app?
In a NestJS app running multiple instances (clustered), you use CacheModule with the default memory store. What will happen when you cache data in one instance and try to access it from another?
Attempts:
2 left
💡 Hint
Think about how memory works in separate processes.
✗ Incorrect
Memory cache is local to each process. In a clustered environment, each instance has its own memory space, so caches are isolated.
🔧 Debug
advanced2:00remaining
Why does the Redis cache throw a connection error in this NestJS setup?
You configured CacheModule with Redis store but get a connection error at runtime. Which of the following is the most likely cause?
NestJS
CacheModule.register({ store: require('cache-manager-redis-store'), host: '127.0.0.1', port: 6380 })Attempts:
2 left
💡 Hint
Check if Redis server is listening on the specified port.
✗ Incorrect
If Redis is not running or listening on the configured port, connection errors occur. The host and store usage are correct.
🧠 Conceptual
expert3:00remaining
Why choose Redis over memory store for caching in NestJS in production?
Which of the following reasons best explains why Redis is preferred over the default memory store for caching in a production NestJS app?
Attempts:
2 left
💡 Hint
Think about app restarts and multiple server instances.
✗ Incorrect
Redis is an external cache that supports sharing data across multiple app instances and persists data beyond restarts, unlike the in-memory store.