Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pirumu/kafka-retry
Kafka non-blocking retry topic for nestjs microservice.
https://github.com/pirumu/kafka-retry
kafkajs microservice nestjs retries
Last synced: about 1 month ago
JSON representation
Kafka non-blocking retry topic for nestjs microservice.
- Host: GitHub
- URL: https://github.com/pirumu/kafka-retry
- Owner: pirumu
- License: mit
- Created: 2022-02-20T14:44:59.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-02-23T10:55:49.000Z (almost 3 years ago)
- Last Synced: 2024-08-09T21:13:55.433Z (5 months ago)
- Topics: kafkajs, microservice, nestjs, retries
- Language: TypeScript
- Homepage:
- Size: 75.2 KB
- Stars: 12
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
- License: LICENSE.MD
Awesome Lists containing this project
README
# 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
![solution](https://evgeniy-khist.github.io/spring-kafka-non-blocking-retries-and-dlt/img/kafka-blocking-retries.png)
![solution](https://evgeniy-khist.github.io/spring-kafka-non-blocking-retries-and-dlt/img/kafka-non-blocking-retries-1.png)[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 `@KafkaListener` and `@KafkaDlt` decorators:```typescript
// 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.