Search code examples
rabbitmqcross-domaindomain-driven-designnestjscqrs

Is there a way to use nestjs cqrs with rabbitmq event bus/queue. And is it anti-pattern to dispatch events handled by microservice from commands?


I am reading through nestjs docs and there seems to be no way to use anything else then built in eventbus. Now let's say my cqrs event wants to communicate with microservices with rabbitmq. So 2 questions:

  1. Is it possible to dispatch event that will send queue to external eventbus?
  2. Is it okey from ddd point of view? Where should I dispatch it then? Dispatch from domain layer? And then parties (microservices) that are interested can listen and write to their own db parts of it or whatever. And if you can please explain it on human level, I am new to ddd with cqrs. And sorry if it's frequently asked question but it's also related to nestjs so I need some clarifications.

Thanks 😊


Solution

  • Integration events should be used to notify external services. A good practice is to keep the external event bus (integration bus) independent from any micro service. And regarding the 2 questions:

    1. It should be possible to send events to external bus with nestjs. Ex: write a domain event handler that create an integration event and dispatch it. You can create a new IntegrationBus interface and implement it in any technology that fits.
    2. It is definitely OK from DDD point of view. Context mapping (how different contexts share data) is part of the strategic patterns of DDD. Usually the integration events are published in the application layer and in some case it can be more convinient to do it in the infrastructure layer.

    Here are few articles with more details about domain and integration events:

    https://learn.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/domain-events-design-implementation

    https://devblogs.microsoft.com/cesardelatorre/domain-events-vs-integration-events-in-domain-driven-design-and-microservices-architectures/