Video in progress
What is the difference between a domain event and an integration event, and why is it dangerous to express them with one class?
Domain event / Integration event — Who listens: handlers inside the same module / other services, other teams; Transport: in-memory (ApplicationEventPublisher) / Kafka, RabbitMQ; Content: domain objects, IDs / only what's needed, flat data; Change: free, along with refactoring / follows schema evolution rules
📖 Answer
| Domain event | Integration event | |
|---|---|---|
| Who listens | handlers inside the same module | other services, other teams |
| Transport | in-memory (ApplicationEventPublisher) |
Kafka, RabbitMQ |
| Content | domain objects, IDs | only what's needed, flat data |
| Change | free, along with refactoring | follows schema evolution rules |
// internal, in domain language
record OrderPaid(OrderId id, Money amount, Instant at) {}
// external, a contract, v1
record OrderPaidV1(String orderId, String amount, String currency,
String paidAt, int schemaVersion) {}
What happens when you use one class: you change amount to minorUnits in the internal Money class. The compiler checks the internal code, but the JSON in Kafka changes, and another team's service crashes at night.
This is how I usually build it:
- The aggregate emits a domain event.
- Internal handlers process it synchronously.
- A separate translator turns it into an integration event and writes it to an outbox table, in the same transaction.
- A relay (Debezium or a poller) sends it from the outbox to the broker.
What an integration event should contain: an event ID (for idempotency), a timestamp, a schema version, and the minimum data the consumers need. Dumping the whole aggregate state (a fat event) ties consumers to your internal model; sending only the ID (a thin event) forces them to query you back. Pick something in between based on what the consumers need.
Sign up to mark studied questions and save your progress.