0
0
NestJSframework~5 mins

Why microservices scale independently in NestJS

Choose your learning style9 modes available
Introduction

Microservices let parts of an app grow or shrink on their own. This helps save resources and handle more users smoothly.

When you want to improve app speed by scaling only busy parts.
When different teams work on different app features separately.
When you want to update or fix one part without stopping the whole app.
When you expect some features to get more users than others.
When you want to use different tools or languages for different parts.
Syntax
NestJS
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
    transport: Transport.TCP,
    options: { port: 3001 },
  });
  await app.listen();
}
bootstrap();

This code creates a microservice using NestJS with TCP transport.

You can run many microservices on different ports to scale parts independently.

Examples
This example uses Redis transport for microservice communication.
NestJS
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.REDIS,
  options: { url: 'redis://localhost:6379' },
});
This example uses NATS transport, good for event-driven microservices.
NestJS
const app = await NestFactory.createMicroservice(AppModule, {
  transport: Transport.NATS,
  options: { servers: ['nats://localhost:4222'] },
});
Sample Program

This microservice listens on port 3001 and sums numbers sent to it. You can run many such microservices on different ports to scale only the parts you need.

NestJS
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
  @MessagePattern({ cmd: 'sum' })
  sum(data: number[]): number {
    return data.reduce((a, b) => a + b, 0);
  }
}

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';

@Module({
  controllers: [AppController],
})
export class AppModule {}

import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
    transport: Transport.TCP,
    options: { port: 3001 },
  });
  await app.listen();
  console.log('Microservice is listening on port 3001');
}
bootstrap();
OutputSuccess
Important Notes

Each microservice runs alone, so you can add more instances of just the busy ones.

Microservices communicate over networks, so choose fast and reliable transport methods.

Scaling independently means less waste and better app performance.

Summary

Microservices let you grow parts of your app separately.

This helps save resources and handle more users smoothly.

NestJS makes it easy to create and run microservices on different ports or transports.