Microservices let parts of an app grow or shrink on their own. This helps save resources and handle more users smoothly.
Why microservices scale independently in 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.
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.REDIS, options: { url: 'redis://localhost:6379' }, });
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.NATS, options: { servers: ['nats://localhost:4222'] }, });
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.
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();
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.
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.
Practice
Solution
Step 1: Understand microservice independence
Microservices run as separate processes or servers, each handling a specific task.Step 2: Recognize scaling benefits
Since they run separately, you can increase resources for one microservice without affecting others.Final Answer:
Because each microservice runs separately and can be scaled on its own -> Option CQuick Check:
Microservices run separately = scale independently [OK]
- Thinking all microservices share resources and scale together
- Believing NestJS merges microservices into one app
- Assuming microservices cannot scale independently
Solution
Step 1: Identify correct microservice creation syntax
Using NestFactory.createMicroservice with Transport.TCP and a valid port number is correct.Step 2: Check options validity
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 3001 } }); uses port 3001 (a number), which is valid. const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 'not-a-number' } }); uses a string for port, which is invalid.Final Answer:
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 3001 } }); -> Option AQuick Check:
Correct microservice syntax = const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 3001 } }); [OK]
- Using create() instead of createMicroservice() for microservices
- Setting port as a string instead of a number
- Using HTTP transport incorrectly for microservices
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 4000 } });
await app.listen();
console.log('Microservice running');Solution
Step 1: Understand createMicroservice with TCP transport
Setting transport to TCP with port 4000 means the microservice listens on port 4000.Step 2: Check listen() usage
Calling listen() without arguments works because port is set in options during creation.Final Answer:
The microservice starts on port 4000 and logs 'Microservice running' -> Option BQuick Check:
Port set in options + listen() = runs on port 4000 [OK]
- Thinking listen() always needs a port argument
- Assuming microservice picks random port if none given to listen()
- Confusing syntax errors with runtime behavior
const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.TCP, options: { port: 0 } });
await app.listen();Solution
Step 1: Understand port 0 meaning in networking
Port 0 tells the OS to assign any free port automatically.Step 2: Check NestJS microservice behavior
NestJS accepts port 0 and starts microservice on a random free port, so code works but port is not fixed.Final Answer:
Port 0 lets the OS assign a free port, so it works but port is unknown -> Option AQuick Check:
Port 0 means OS assigns port [OK]
- Assuming port 0 is invalid and causes failure
- Forgetting await before listen() (though it is present here)
- Believing Transport.TCP is unsupported
Solution
Step 1: Understand microservice separation
Running each microservice on its own port means they operate independently.Step 2: Scale only Payments service
You can add more instances of Payments microservice without touching Auth or Notifications.Step 3: Avoid combining or sharing ports
Combining services or sharing ports prevents independent scaling.Final Answer:
Run each microservice on its own port and scale Payments service instances independently -> Option DQuick Check:
Separate ports = independent scaling [OK]
- Merging all services into one app losing independent scaling
- Running all microservices on same port causing conflicts
- Using one service for all features limits scaling options
Ready to prove it?
Pass the test and this pattern moves unknown
