advanced
Domain events
Record meaningful business facts after state changes so side effects, integration, and audit behavior can be coordinated explicitly.
Domain events record meaningful business facts that already happened—OrderPlaced, PaymentCaptured—inside the domain model. They coordinate side effects, integration, and audit without entangling aggregates. Raise events from aggregate roots and dispatch after successful persistence to avoid phantom notifications.
On interviews: contrast domain events with integration events and technical logs. Discuss outbox and idempotent handlers.
Common pitfalls: events named as commands; publishing before commit; handlers with business logic that belongs in domain.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Past-tense names reflecting business facts.
- Collected on aggregate, dispatched on commit.
- Handlers idempotent and side-effect aware.
- Schema versioning for downstream consumers.