Domain-Driven Design (50 Qs)•Part 3: Advanced Architecture & Production Patterns (Senior / Lead)
Domain Events Lifecycle: Internal emission vs Outbox publishing.
senior LevelLifecycleSystem Design
In Simple Words: How events travel from aggregate mutations to external message brokers:
- Emitted Inside Aggregate: Calling
order.cancel("Customer request")records an internal event in an uncommitted in-memory list:this.addDomainEvent(new OrderCancelled(...)). - Repository Commit: When
orderRepository.save(order)runs, it commits both the order state change and writes the domain event into the Transactional Outbox Table within the exact same local database transaction. - Message Relay: A background CDC engine (Debezium) or worker process reads the Outbox table and publishes the event to Apache Kafka / RabbitMQ with guaranteed zero-loss delivery.