0
0
NestJSframework~20 mins

Cache stores (memory, Redis) in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Cache Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 {}
A"hello"
Bundefined
Cnull
DThrows a runtime error
Attempts:
2 left
💡 Hint
Think about what CacheModule.register() uses by default for storage.
📝 Syntax
intermediate
2: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?
ACacheModule.register({ store: require('cache-manager-redis-store'), host: 'localhost', port: 6379 })
BCacheModule.register({ store: 'redis', host: 'localhost', port: 6379 })
CCacheModule.register({ redis: { host: 'localhost', port: 6379 } })
DCacheModule.register({ store: require('redis'), host: 'localhost', port: 6379 })
Attempts:
2 left
💡 Hint
The Redis store requires a specific package to be passed as the store option.
state_output
advanced
2: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?
ACached data is shared across all instances and accessible everywhere
BCacheModule automatically syncs memory cache between instances
CEach instance has its own cache; data is not shared between instances
DAccessing cached data from another instance throws an error
Attempts:
2 left
💡 Hint
Think about how memory works in separate processes.
🔧 Debug
advanced
2: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 })
AHost IP '127.0.0.1' is invalid for Redis connections
BThe store option should be a string 'redis' not a require call
CCacheModule does not support Redis caching
DRedis server is not running on port 6380
Attempts:
2 left
💡 Hint
Check if Redis server is listening on the specified port.
🧠 Conceptual
expert
3: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?
AMemory store is faster and more scalable than Redis
BRedis supports distributed caching and persists data beyond app restarts
CRedis cache is built into NestJS by default, no extra setup needed
DMemory store automatically syncs cache between multiple app instances
Attempts:
2 left
💡 Hint
Think about app restarts and multiple server instances.