Microservice transports help different small services talk to each other easily and reliably.
Microservice transports in NestJS
import { Transport } from '@nestjs/microservices'; const microserviceOptions = { transport: Transport.TCP, // e.g. Transport.REDIS, Transport.NATS, Transport.RMQ, Transport.GRPC options: { // transport-specific options like host, port, queue name }, };
Choose the transport type based on your communication needs.
Each transport has its own options to configure connection details.
const tcpMicroservice = {
transport: Transport.TCP,
options: { host: '127.0.0.1', port: 3001 },
};const redisMicroservice = {
transport: Transport.REDIS,
options: { url: 'redis://localhost:6379' },
};const rmqMicroservice = {
transport: Transport.RMQ,
options: { urls: ['amqp://localhost:5672'], queue: 'main_queue' },
};This example shows how to create a simple NestJS microservice using TCP transport. The service listens on port 4000 for incoming messages from other services.
import { NestFactory } from '@nestjs/core'; import { MicroserviceOptions, Transport } from '@nestjs/microservices'; import { AppModule } from './app.module'; async function bootstrap() { // Create a microservice using TCP transport const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, { transport: Transport.TCP, options: { host: '127.0.0.1', port: 4000 }, }); await app.listen(); console.log('Microservice is listening on TCP port 4000'); } bootstrap();
Pick the transport that fits your app's needs for speed, reliability, and complexity.
TCP is simple but less flexible; message brokers like RabbitMQ add reliability and features.
Always configure transports with correct host, port, and credentials to avoid connection issues.
Microservice transports let small services communicate in different ways.
Choose transports like TCP, Redis, or RabbitMQ based on your app's needs.
Proper setup helps your app grow and handle many users smoothly.
Practice
Solution
Step 1: Understand transport types in NestJS
TCP transport is a simple protocol for direct communication between services on the same network without needing a message broker.Step 2: Compare with other transports
Redis, RabbitMQ, and Kafka are message brokers used for more complex messaging patterns and scalability.Final Answer:
TCP transport -> Option AQuick Check:
Direct network communication = TCP transport [OK]
- Confusing Redis or RabbitMQ as direct transports
- Thinking Kafka is a simple direct transport
- Assuming TCP requires a message broker
Solution
Step 1: Identify RabbitMQ transport setup
RabbitMQ transport in NestJS uses Transport.RMQ and requires options like urls (connection strings) and queue name.Step 2: Differentiate from other transports
TCP uses host and port, Redis uses channel and password, Kafka uses topic and brokers, so these are incorrect for RabbitMQ.Final Answer:
Transport.RMQ with options including urls and queue -> Option DQuick Check:
RabbitMQ setup = Transport.RMQ + urls + queue [OK]
- Using TCP options for RabbitMQ
- Confusing Redis channel with RabbitMQ queue
- Omitting urls in RabbitMQ config
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.REDIS,
options: { url: 'redis://localhost:6379' }
});
app.listen(() => console.log('Microservice is listening'));Solution
Step 1: Analyze the transport configuration
The code uses Transport.REDIS with a valid Redis URL, which is sufficient for Redis transport in NestJS.Step 2: Understand the listen callback
The listen method's callback logs 'Microservice is listening' when the microservice starts successfully.Final Answer:
Microservice is listening -> Option CQuick Check:
Valid Redis config logs listening message [OK]
- Expecting queue option for Redis transport
- Confusing Redis with RabbitMQ setup
- Assuming error without testing listen callback
const app = await NestFactory.createMicroservice(AppModule, {
transport: Transport.RMQ,
options: {
urls: ['amqp://localhost:5672'],
queue: ''
}
});Solution
Step 1: Check RMQ options correctness
RabbitMQ transport requires a non-empty queue name to route messages properly.Step 2: Validate other options
The urls option is correctly an array of connection strings; transport RMQ is correct; Redis password is irrelevant here.Final Answer:
The queue name is empty, which is invalid -> Option BQuick Check:
Empty queue name causes config error [OK]
- Thinking urls must be a string
- Confusing transport type with Redis
- Ignoring empty queue name error
Solution
Step 1: Identify requirements for high throughput and guaranteed delivery
Guaranteed delivery and multiple consumers require a message broker with queueing and acknowledgment features.Step 2: Evaluate transport options
TCP is simple but lacks message queueing; Redis supports pub/sub but has limited message durability; RabbitMQ supports queues, acknowledgments, and multiple consumers; in-memory is local only and not scalable.Final Answer:
RabbitMQ transport, because it supports message queues with acknowledgments and multiple consumers -> Option AQuick Check:
Guaranteed delivery + multiple consumers = RabbitMQ [OK]
- Choosing TCP for guaranteed delivery
- Assuming Redis persistence equals RabbitMQ reliability
- Using in-memory transport for distributed systems
Ready to prove it?
Pass the test and this pattern moves unknown
