0
0
NestJSframework~8 mins

Cache stores (memory, Redis) in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Cache stores (memory, Redis)
HIGH IMPACT
This affects how fast data can be retrieved and reduces server processing time, improving response speed and user experience.
Storing frequently accessed data to speed up responses
NestJS
async getUser(id: string) {
  const cached = await this.cacheManager.get(id);
  if (cached) return cached;
  const user = await this.userService.findById(id);
  await this.cacheManager.set(id, user, { ttl: 300 });
  return user;
}
Caches data in memory or Redis to serve repeated requests instantly.
📈 Performance GainReduces DB calls by 90%, cuts response time to under 20 ms
Storing frequently accessed data to speed up responses
NestJS
async getUser(id: string) {
  return await this.userService.findById(id); // Always fetches from DB
}
Fetching from the database every time causes slow responses and high server load.
📉 Performance CostBlocks rendering for 100+ ms per request under load
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No cache (DB fetch every time)N/AN/AHigh server delay causes slow paint[X] Bad
In-memory cache storeN/AN/AFast response reduces paint delay[OK] Good
Redis cache storeN/AN/AFast network cache reduces paint delay[OK] Good
Rendering Pipeline
Cache stores reduce server processing time before the response reaches the browser, speeding up the critical rendering path.
Server Processing
Network Transfer
First Contentful Paint
⚠️ BottleneckServer Processing time waiting for data fetch
Core Web Vital Affected
LCP
This affects how fast data can be retrieved and reduces server processing time, improving response speed and user experience.
Optimization Tips
1Cache frequently requested data to reduce server processing time.
2Use Redis for distributed caching to keep data consistent across servers.
3Avoid cache misses by setting appropriate TTL and warming cache when possible.
Performance Quiz - 3 Questions
Test your performance knowledge
How does using a Redis cache store affect page load performance?
AIt reduces server processing time by serving cached data quickly
BIt increases DOM nodes causing slower rendering
CIt blocks the main thread during cache retrieval
DIt causes more layout shifts on the page
DevTools: Network
How to check: Open DevTools > Network tab, reload page, check response times for API calls
What to look for: Look for reduced response times and fewer database query calls indicating cache hits