CodePark/ questions / Microservices, queues & events
START PRACTICINGPRACTICE
Backend question · NestJS › Microservices, queues & events

In a Nest microservice, what is the difference between @MessagePattern() and @EventPattern()?

@MessagePattern is request-response and its return value goes back to the caller; @EventPattern is fire-and-forget with no response

@MessagePattern is request-response. Its handler pairs with client.send(), and the caller waits for the returned value. @EventPattern is fire-and-forget. It pairs with client.emit(), and the publisher doesn't expect a reply.

"
@MessagePattern is for TCP only; @EventPattern is for Kafka only" is wrong: both work across transports. "@MessagePattern broadcasts to all instances" is also wrong. Whether a message goes to one instance or all of them depends on the broker, not the decorator.