Questions
2 of 25
1What is the difference between request-response and event-based (fire-and-forget) message patterns in NestJS microservices?
2What is the @Payload() decorator and how do you use @Ctx() to access transport-specific context in NestJS microservices?
3How do you connect a NestJS microservice to RabbitMQ?
4How do you implement manual message acknowledgement with RabbitMQ in NestJS?
5How do you implement a RabbitMQ topic exchange for routing messages to multiple queues based on routing keys in NestJS?
6How does gRPC differ from REST and when would you choose it for inter-service communication in NestJS?
7How do you implement an API Gateway pattern in NestJS that aggregates multiple microservices?
8How do you implement distributed tracing across NestJS microservices?
9What transport layers does NestJS support for microservices and how do you bootstrap a microservice?
10How do you connect a NestJS microservice to Apache Kafka?
11How do you handle Kafka message patterns and access message headers and partition info via KafkaContext in NestJS?
12How do you set up gRPC in a NestJS microservice?
13How do you implement a circuit breaker pattern when calling a downstream microservice in NestJS?
14How do you inject and use a ClientProxy to communicate with another microservice in NestJS?
15How do you handle errors in microservice request-response patterns and propagate them to the caller in NestJS?
16How do you implement Kafka consumer groups and what guarantees does partition assignment give you in NestJS?
17How do you implement the outbox pattern with Kafka in NestJS to guarantee exactly-once message delivery?
18How do you implement gRPC streaming including server-side, client-side, and bidirectional streaming in NestJS?
19What is the difference between @EventPattern() and @MessagePattern() in NestJS microservices?
20How do you implement the Saga pattern for distributed transactions across NestJS microservices?
21How do you implement idempotent event handlers to safely handle duplicate message delivery in NestJS?
22How do you implement event sourcing with NestJS microservices?
23How do you implement retry logic with exponential backoff for failed microservice calls in NestJS?
24How do you test a NestJS microservice controller in isolation without a real message broker?
25How do you implement service discovery and load balancing across NestJS microservice instances?
02 / 25

What is the @Payload() decorator and how do you use @Ctx() to access transport-specific context in NestJS microservices?

@Payload() extracts the message data and can accept a ValidationPipe for automatic validation. @Ctx() injects the transport-specific context object — RmqContext for RabbitMQ, KafkaContext for Kafka, TcpContext for TCP. Context objects expose transport metadata like channel references, partition numbers, and message offsets.

@Payload() with validation and @Ctx() for transport context
Context types by transport:
  1. 1

    TcpContext — getPattern() returns the message pattern object.

  2. 2

    RmqContext — getChannelRef() returns the AMQP channel; getMessage() returns the raw message for ack/nack.

  3. 3

    KafkaContext — getTopic(), getPartition(), getMessage() with offset and headers.

  4. 4

    NatsContext — getHeaders() returns NATS message headers.

  5. 5

    @Payload(new ValidationPipe()) validates message data the same way @Body(new ValidationPipe()) validates HTTP bodies.

Difficulty: 5/10
Topics: NestJS microservices, decorators, transport context

Scenario Questions

0-2 years experience
  1. 1

    You need to create a NestJS microservice handler that receives a JSON payload. How would you use the @Payload() decorator to extract the data, and what would happen if you omitted it?

  2. 2

    In a simple TCP microservice, you want to log the client’s IP address from the transport context. Show how you would use @Ctx() in the handler to get that information.

2-5 years experience
  1. 1

    We have a message pattern that sometimes includes metadata in the transport context and sometimes doesn't. How would you safely access optional fields using @Ctx() without causing runtime errors?

  2. 2

    During integration testing, a handler that uses @Payload() started receiving undefined values after a refactor. Walk me through how you would debug the issue and verify the decorator is applied correctly.

5-8 years experience
  1. 1

    Our system uses RabbitMQ and gRPC transports side by side, and we need a unified way to extract correlation IDs for tracing. How would you design a utility that leverages @Ctx() to handle both transports efficiently?

  2. 2

    When scaling the microservice, we observed that heavy payloads cause increased memory usage. Discuss trade‑offs of using @Payload() versus manually parsing the raw message inside the handler.

8+ years experience
  1. 1

    We are migrating a legacy monolith to a NestJS microservice architecture and need to preserve existing request context (auth tokens, request IDs) across multiple transports. How would you architect the use of @Ctx() and possibly custom decorators to ensure consistent context propagation across teams?

  2. 2

    Consider a cross‑team initiative to implement end‑to‑end tracing across all NestJS microservices. What are the implications of relying on @Ctx() for transport‑specific data, and how would you standardize context handling to avoid duplication and maintainability issues?

Follow-up Questions

  • What fields does @Ctx() expose for TCP versus RabbitMQ transports?
  • How would you test that @Payload() correctly maps incoming data, especially when the payload is malformed?
  • What performance implications arise when handling very large payloads with @Payload()?