0
0
NestJSframework~8 mins

Why caching reduces response latency in NestJS - Performance Evidence

Choose your learning style9 modes available
Performance: Why caching reduces response latency
HIGH IMPACT
Caching reduces the time it takes for the server to respond by avoiding repeated heavy computations or database calls.
Serving frequently requested data in a NestJS API
NestJS
private cache = new Map<string, User>();

async getUserData(userId: string) {
  if (this.cache.has(userId)) {
    return this.cache.get(userId);
  }
  const user = await this.database.findUserById(userId);
  this.cache.set(userId, user);
  return user;
}
Returns cached data instantly for repeated requests, reducing server work.
📈 Performance GainResponse time drops to under 5ms for cached requests, reducing server load
Serving frequently requested data in a NestJS API
NestJS
async getUserData(userId: string) {
  return await this.database.findUserById(userId);
}
Every request hits the database causing slow responses and high server load.
📉 Performance CostBlocks response for 50-200ms per request depending on DB speed
Performance Comparison
PatternServer ProcessingDatabase QueriesResponse TimeVerdict
No cachingHigh CPU and I/OOne query per request50-200ms[X] Bad
With cachingMinimal CPUZero queries on cache hitUnder 5ms[OK] Good
Rendering Pipeline
When caching is used, the server skips database queries and complex logic, sending data immediately. This reduces backend processing time and speeds up the network response.
Server Processing
Network Transfer
⚠️ BottleneckServer Processing (database queries and computations)
Core Web Vital Affected
INP
Caching reduces the time it takes for the server to respond by avoiding repeated heavy computations or database calls.
Optimization Tips
1Cache data that is expensive to compute or fetch to reduce server response time.
2Use in-memory or fast-access storage for caching to maximize speed gains.
3Ensure cache invalidation strategies to keep data fresh without sacrificing performance.
Performance Quiz - 3 Questions
Test your performance knowledge
How does caching reduce response latency in a NestJS API?
ABy avoiding repeated database queries and returning stored data quickly
BBy increasing the number of database queries to speed up data retrieval
CBy delaying the response to batch multiple requests together
DBy compressing the response data to reduce size
DevTools: Network
How to check: Open DevTools, go to Network tab, make repeated API calls and observe response times.
What to look for: Cached responses show much lower latency and faster load times compared to uncached ones.