Skip to content

Kafka non-blocking retry topic for nestjs microservice.

License

Notifications You must be signed in to change notification settings

pirumu/kafka-retry

Repository files navigation

KAFKA RETRY

Docs | MIT Licensed

Kafka non-blocking retry topic for nestjs microservice.

Non-Blocking Retries and Dead Letter Topics

solution solution

See more: Spring Kafka Non-Blocking Retries and Dead Letter Topics

Usage example

// main.ts
import {KafkaOptions} from "@nestjs/microservices";

const kafkaOptions: KafkaOptions = {};
const app = await NestFactory.create(AppModule);
app.connectMicroservice({
    strategy: new KafkaStrategy(kafkaOptions)
});
await app.startAllMicroservices();

Decorate your controllers with the @KafkaListener and @KafkaDlt decorators:

// controller.ts
@Controller()
export class UserController {
  @KafkaListener('user-created', {
      attempts: 2,
      backoff:  {
          delay: 200,
          multiplier: 2.0, 
      },
      autoCreateRetryTopic: true,
  })
  public async message(
    @Payload() data, 
    @Ctx() ctx: KafkaContext
    ) {
      try {
        //Todo something
      } catch (err) {
          // must throw RpcException
        throw new RpcException('abcd');
      }
  }

  @KafkaDlt('user-created')
  public async handleDeadLetterTopic(
    @Payload() data, 
    @Ctx() ctx: KafkaContext
    ) {
      //Todo something to report 
  }
}

With this @KafkaListener, the first delivery attempt fails and the record is sent to a topic user-created-retry-1 configured for a 200ms delay. When that delivery fails, the record is sent to a topic user-created-retry-2 with a 400ms delay and, finally, to a dead letter topic user-created-dlt handled by @KafkaDlt.

About

Kafka non-blocking retry topic for nestjs microservice.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published