If you build services with NestJS, the Node.js application framework, this guide helps you choose a transport and configure it for production. The transport is how your services pass messages, and the same @MessagePattern handler works over Redis, NATS, RabbitMQ, Kafka, or gRPC, Google’s typed call protocol, with no code change. That makes them look interchangeable, but they differ on whether a message survives a crash or a broker restart, so code that passes staging can quietly drop orders later. The short version is that RabbitMQ and NATS JetStream store and re-deliver messages, so use them when a lost message would hurt. Redis publish/subscribe is fine for broadcasts you can afford to miss, and by the end you will have your transport chosen on purpose, acknowledgements turned on, and a dead-letter queue catching what fails.
How to verify
Confirm which transport a service is using and that messages are flowing:
ss -tnlp | grep node # transport's listening port
curl -s http://localhost:3000/_meta/transport # if you expose it
docker exec -it rabbitmq rabbitmqctl list_queues
redis-cli psubscribe '*' | head -20
nats sub '>' # listen to everything on NATS
journalctl -u myapp --since "5 min ago" | grep -i 'transport\|connect'
If you see no messages flowing but the service started, the client may be connected to a different broker than the server expected. Compare the connection string on both sides exactly.
What’s happening
NestJS microservices have a server (createMicroservice) and a client (ClientProxy). The server registers handlers for “message patterns” (request/reply) or “event patterns” (fire-and-forget). The client emits messages or invokes patterns. The chosen Transport enum determines the wire protocol but not the semantics — you must read the docs of the underlying broker to know what guarantees you have.
The big distinctions: durability (does a message survive a broker restart?), delivery (at-most-once, at-least-once, exactly-once?), ordering (per-key, global, none?), back-pressure (does the broker push or does the consumer pull?), and topology (point-to-point queue vs publish/subscribe topic). Picking the wrong transport for your guarantees gives you a system that works in staging and loses messages in production.
The decision
| Transport | Durability | Delivery | Ordering | Best for |
|---|---|---|---|---|
Transport.TCP | None | At-most-once | None (single conn) | Internal sync RPC, no broker |
Transport.REDIS (pub/sub) | None | At-most-once | None | Fire-and-forget notifications |
Transport.NATS (core) | None | At-most-once | Per-subject | Low-latency RPC, ephemeral events |
Transport.NATS (JetStream) | Yes | At-least-once | Per-subject | Durable streams, replay |
Transport.RMQ (RabbitMQ) | Yes | At-least-once | Per-queue | Work queues, RPC with reliability |
Transport.KAFKA | Yes | At-least-once | Per-partition | Event sourcing, high throughput |
Transport.MQTT | Configurable QoS | 0/1/2 | None | IoT devices, low bandwidth |
Transport.GRPC | None | At-most-once | None | Cross-language sync RPC, typed |
Pick Redis pub/sub only when message loss is acceptable (cache invalidation broadcasts, presence pings). Pick NATS core for low-latency RPC inside a cluster where loss is acceptable. Pick NATS JetStream or RabbitMQ when you need acks and replay. Pick Kafka when you need long-term retention and high partition counts. Pick gRPC for cross-language typed RPC where you don’t want a broker in the path.
The procedure
-
Pick the transport. Wire it on the server side at bootstrap:
// main.ts — RabbitMQ example import { NestFactory } from '@nestjs/core'; import { Transport } from '@nestjs/microservices'; const app = await NestFactory.createMicroservice(AppModule, { transport: Transport.RMQ, options: { urls: ['amqp://rabbitmq:5672'], queue: 'orders_queue', queueOptions: { durable: true }, noAck: false, // require manual ack prefetchCount: 32, // back-pressure }, }); await app.listen(); -
Declare the handler. Use
@MessagePatternfor request/reply,@EventPatternfor fire-and-forget:@Controller() export class OrdersController { @MessagePattern('order.create') async createOrder(@Payload() dto: CreateOrderDto, @Ctx() ctx: RmqContext) { try { const order = await this.svc.create(dto); ctx.getChannelRef().ack(ctx.getMessage()); // explicit ack return order; } catch (e) { ctx.getChannelRef().nack(ctx.getMessage(), false, false); // dead-letter throw e; } } } -
Configure the client side. The injection token must match the module registration:
@Module({ imports: [ClientsModule.register([{ name: 'ORDERS_CLIENT', transport: Transport.RMQ, options: { urls: ['amqp://rabbitmq:5672'], queue: 'orders_queue' }, }])], }) export class AppModule {} -
For request/reply,
await client.send(pattern, payload)returns an Observable; convert to a Promise with.toPromise()orfirstValueFrom():const order = await firstValueFrom(this.client.send('order.create', dto)); -
Add a dead-letter queue (DLQ) wherever your transport supports it. For RabbitMQ, declare the exchange with
x-dead-letter-exchange. Messages nacked twice land in the DLQ and you alert on its depth rather than losing them silently.
Common pitfalls
- The same handler code works on every transport. The guarantees do not. A handler that’s safe under RabbitMQ (acks, redelivery on crash) will silently lose messages under Redis pub/sub if you copy-paste without thinking.
prefetchCountdefaults to unlimited on RabbitMQ in some Nest versions — a slow handler will swallow the whole queue into memory. Set it explicitly.noAck: truemeans RabbitMQ removes messages on delivery, not on ack. If your handler crashes between receive and process, the message is lost. UsenoAck: falseand explicit acks for anything important.- gRPC in Nest requires proto file compilation; the
loader: { keepCase: true }option is often necessary to match snake_case Go services. Missing this corrupts field names silently. - Kafka consumer groups are tied to the
groupId. If two replicas of the same service use different group IDs, both receive every message — you have unintentional fan-out, not load balancing.
For the HTTP side of the same NestJS app, see NestJS production deploy. In the engagements Stack Harbor runs, the transport decision is made at design time and documented — the implementation, broker tuning, DLQ alerting, and reconnection logic are part of managed operations, so a misconfigured transport doesn’t end up dropping production messages.