0
0
NestJSframework~8 mins

Message patterns (request-response) in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Message patterns (request-response)
MEDIUM IMPACT
This affects the responsiveness and throughput of backend services handling request-response messaging patterns.
Handling request-response messages in NestJS microservices
NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
  @MessagePattern('sum')
  async handleSum(data: number[]) {
    // offload heavy processing asynchronously
    return await new Promise((resolve) => {
      setImmediate(() => {
        let result = 0;
        for (const num of data) {
          for (let j = 0; j < 1000000; j++) {
            result += num;
          }
        }
        resolve(result);
      });
    });
  }
}
Offloading heavy computation asynchronously prevents blocking the event loop, allowing other requests to be processed.
📈 Performance GainReduces event loop blocking, improving INP and overall throughput.
Handling request-response messages in NestJS microservices
NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
  @MessagePattern('sum')
  async handleSum(data: number[]) {
    // heavy synchronous processing
    let result = 0;
    for (let i = 0; i < data.length; i++) {
      for (let j = 0; j < 1000000; j++) {
        result += data[i];
      }
    }
    return result;
  }
}
Heavy synchronous processing blocks the event loop, delaying responses and reducing throughput.
📉 Performance CostBlocks event loop for hundreds of milliseconds per request, increasing INP and response time.
Performance Comparison
PatternEvent Loop BlockingResponse DelayThroughput ImpactVerdict
Synchronous heavy processingHigh (blocks event loop)High (delays response)Low (reduces throughput)[X] Bad
Asynchronous offloadingLow (non-blocking)Low (fast response)High (better throughput)[OK] Good
Rendering Pipeline
In NestJS request-response message patterns, the server receives a message, processes it, and sends a response. Blocking operations delay the event loop, causing slower response times and poor interaction responsiveness.
Message Reception
Processing
Response Sending
⚠️ BottleneckProcessing stage when synchronous heavy tasks block the event loop
Core Web Vital Affected
INP
This affects the responsiveness and throughput of backend services handling request-response messaging patterns.
Optimization Tips
1Avoid synchronous heavy processing in message handlers to prevent event loop blocking.
2Use asynchronous or offloaded tasks for CPU-intensive work in NestJS microservices.
3Monitor event loop blocking in performance tools to ensure responsive request handling.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue with synchronous heavy processing in NestJS message handlers?
AIt increases bundle size significantly
BIt causes layout shifts in the browser
CIt blocks the event loop, delaying other requests
DIt reduces network bandwidth
DevTools: Performance
How to check: Record a performance profile while sending messages to the NestJS microservice. Look for long tasks blocking the event loop.
What to look for: Long tasks in the main thread timeline indicate blocking synchronous processing causing delayed responses.