Code Monkey home page Code Monkey logo

nest-kafka's Introduction

nest-kafka

This repository contains some feasible solutions to resolve the problem of using NestJS microservice with Kafka.

Grouping events

According to Confluent, it is recommended to group events about same entity in the same topic. Managing fine-grained topics is not beneficial in terms of performance and clarity.

However, it is not easy to do that in NestJS microservice(using @nestjs/microservices). The reason is that NestJS microservice uses @EventPattern to subscribe events, and @EventPattern maps a event pattern to a single topic.

There is relative issue on nestjs/nest Github. Kamil Mysliwiec, the creator of NestJS, suggests to have single method to subscribe single topic, which manage groups of event. Then broadcast another internal events using @nestjs/event-emitter package.

It is a possible solution, but doesn't seems elegant personally. Emitting internal event after consuming message from Kafka could make the system more complex and give difficulty to debug.

Here I came up with another solution. It is to use custom deserializer and server to convert topic to handler pattern and vice versa so that we can group multiple events in a single topic.

start microservice

flowchart LR
  consumer-->|"1. subscribes specific events\n @EventPattern(HandlerPatternTopicTransformer.createHandlerPatternFrom())"|custom-server
  custom-server-->|"2. group each events to single topic\n HandlerPatternTopicTransformer.toTopic()"|kafka

publish

flowchart LR
  producer-->|"serialize event in a format\n MessageProducer.enqueue()"|kafka

consume

flowchart LR
    kafka-->|1. consume message|custom-deserializer
    custom-deserializer-->|"2. convert topic to handlerPattern and route to handler\n HandlerPatternTopicTransformer.createHandlerPatternFrom()"|consumer

Idempotent consumer

(TBD)

nest-kafka's People

Contributors

pigrabbit avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.