kafka-retry
Version:
Handle kafka non-blocking retries and dead letter topics for nestjs microservice
64 lines (53 loc) • 2.01 kB
Markdown
# KAFKA RETRY
[Docs](https://github.com/nattogo/kafka-retry/blob/master/README.MD) |
[MIT Licensed](https://github.com/nattogo/kafka-retry/blob/master/LICENSE.MD)
Kafka non-blocking retry topic for nestjs microservice.
### Non-Blocking Retries and Dead Letter Topics


[See more: Spring Kafka Non-Blocking Retries and Dead Letter Topics](https://evgeniy-khist.github.io/spring-kafka-non-blocking-retries-and-dlt/)
## Usage example
```ts
// 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 `` and `` decorators:
```typescript
// controller.ts
export class UserController {
public async message(
data,
ctx: KafkaContext
) {
try {
//Todo something
} catch (err) {
// must throw RpcException
throw new RpcException('abcd');
}
}
public async handleDeadLetterTopic(
data,
ctx: KafkaContext
) {
//Todo something to report
}
}
```
With this ``, 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 .