Search code examples
javadomain-driven-design

Kafka Consumers in Onion Architecture


I am working with a project following DDD and has consumers to a Kafka Queue. My question is straight forward, where do the consumers reside in the Onion Architecture and Hexagonal Architecture? Are they event handlers or should they be a part of the infrastructure?

I am using Kafka Consumers to listen to change events of other aggregate roots and want to store the data in my current aggregate. Basically replicating the data from one microservice to another.


Solution

  • The way I see it is:

    1. Your aggregates are the Core

    2. The message handlers are Use Cases, which use dependencies (like repository interfaces) and the core to execute the business use case.

    3. There is infrastructure code that peeks messages from the queue and triggers use cases

    With this approach, you can unit test your use cases without worrying about infrastructure and you can replace the whole messaging queue technology. The same approach works for handling API requests. In practice, the only difference is that with the API you can return a response synchronously, and with messaging you can't.

    As a practical note, in .NET I use a library called Mediatr to implement and trigger the use cases. In java, I found PipelinR which looks similar at first sight. This type of approach allows you to implement all use cases the same way for all your synchronous and asynchronous usages.