Questions
11 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?
11 / 25

How do you handle Kafka message patterns and access message headers and partition info via KafkaContext in NestJS?

@MessagePattern maps to a Kafka topic name. Use @Ctx() to inject a KafkaContext which exposes getTopic(), getPartition(), and getMessage() containing the offset and headers. Publish messages with a key to determine partition assignment and include headers for cross-cutting concerns like correlation IDs.

KafkaContext metadata access and keyed message publishing
Kafka message key and ordering guarantees:
  1. 1

    Message key determines which partition receives the message — same key always goes to the same partition.

  2. 2

    All messages with the same key are ordered within their partition — critical for event sourcing.

  3. 3

    Without a key, Kafka round-robins across partitions — no ordering guarantee.

  4. 4

    Offset identifies the message position within a partition — use it for manual offset commits.

  5. 5

    Headers are available via ctx.getMessage().headers — use for correlation IDs and trace context propagation.

Difficulty: 5/10
Topics: KafkaContext, message headers, partition handling

Scenario Questions

0-2 years experience
  1. 1

    Suppose you need to consume a Kafka topic in a NestJS microservice and you must read a custom header called 'event-type' to route the payload. How would you access that header using KafkaContext?

  2. 2

    If a message arrives on partition 3 and you want to log the partition number inside your NestJS consumer, what code would you write to retrieve the partition from KafkaContext?

  3. 3

    What happens if you try to read a header that wasn't set on the incoming Kafka message? How would your NestJS handler behave?

2-5 years experience
  1. 1

    You added a new header 'correlation-id' to all outbound events, but downstream services are sometimes not receiving it. Walk me through how you'd debug the issue using KafkaContext in your NestJS consumer.

  2. 2

    When implementing a pattern that processes messages differently based on the partition they came from, what trade‑offs do you consider, and how would you structure your NestJS handler to access partition info efficiently?

  3. 3

    Explain why using KafkaContext to read headers inside a @MessagePattern handler might cause issues when you have multiple consumer groups, and how you'd mitigate them.

5-8 years experience
  1. 1

    At scale, you need to enforce ordering guarantees per key across partitions while also extracting headers for audit logging. How would you design your NestJS Kafka consumer architecture to reliably get partition and header info without impacting throughput?

  2. 2

    If you need to route messages to different processing pipelines based on a combination of header values and partition number, what patterns would you use in NestJS, and how would you handle potential back‑pressure or hot partitions?

  3. 3

    Discuss the performance implications of repeatedly calling KafkaContext.getMessage().headers in a high‑throughput NestJS service and how you would optimize it.

8+ years experience
  1. 1

    Your organization is migrating from a legacy Java Kafka consumer to a NestJS‑based platform. How would you ensure consistent handling of message headers and partition metadata across teams, and what abstraction or governance would you introduce?

  2. 2

    When designing a cross‑service event schema that relies heavily on custom headers for routing, what architectural considerations would you raise about coupling NestJS consumers to header conventions, and how would you future‑proof the design?

  3. 3

    If you need to support multiple versions of a message format where headers indicate version, how would you structure the NestJS layer to handle version negotiation while keeping partition handling robust across deployments?

Follow-up Questions

  • Can you sketch the exact code you would write inside the handler?
  • What would you do if a required header is absent?
  • How would you unit‑test the header‑extraction logic?