0
0
NestJSframework~8 mins

Health checks (Terminus) in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Health checks (Terminus)
MEDIUM IMPACT
Health checks impact server responsiveness and uptime monitoring, indirectly affecting user experience by ensuring service availability.
Implementing health checks to monitor service status
NestJS
import { TerminusModule, HealthCheckService, HealthCheck } from '@nestjs/terminus';
import { Controller } from '@nestjs/common';

@Controller('health')
export class HealthController {
  constructor(private health: HealthCheckService) {}

  @HealthCheck()
  check() {
    return this.health.check([]); // lightweight async checks
  }
}
Uses Terminus async health checks that do not block event loop and integrate with NestJS lifecycle.
📈 Performance GainNon-blocking, responds within milliseconds, minimal CPU usage
Implementing health checks to monitor service status
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('health')
export class HealthController {
  @Get()
  check() {
    // Synchronous heavy operation
    const result = heavySyncCheck();
    return { status: result ? 'ok' : 'fail' };
  }
}

function heavySyncCheck() {
  // Simulate CPU intensive task
  for (let i = 0; i < 1e8; i++) {}
  return true;
}
Blocking synchronous operations delay response and block event loop, reducing server responsiveness.
📉 Performance CostBlocks event loop for 100+ ms per request, increasing response time and reducing throughput
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous heavy health check000[X] Bad
Asynchronous Terminus health check000[OK] Good
Rendering Pipeline
Health check requests are handled by the server without rendering UI, but inefficient checks can block the Node.js event loop, delaying all other requests.
Request Handling
Event Loop
Response Sending
⚠️ BottleneckEvent Loop blocking due to synchronous or heavy operations
Optimization Tips
1Avoid synchronous or CPU-intensive tasks in health check endpoints.
2Use asynchronous health check libraries like Terminus to keep event loop free.
3Keep health checks lightweight to ensure fast server responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of implementing health checks with synchronous heavy operations in NestJS?
AIncreasing DOM reflows and layout shifts
BBlocking the Node.js event loop and delaying all requests
CAdding large assets to the client bundle
DCausing CSS selector complexity
DevTools: Network
How to check: Open DevTools Network panel, trigger health check endpoint, observe response time and status code.
What to look for: Fast response times (under 50ms) and HTTP 200 status indicate good performance; slow or hanging responses indicate blocking.