Domain-Driven Design (50 Qs)•Part 3: Advanced Architecture & Production Patterns (Senior / Lead)
CQRS and Event Sourcing in DDD: State reconstruction and projections.
senior LevelCode ExampleSystem Design
In Simple Words:
- Event-Sourced Aggregate: Has zero internal
updatestatements. State is reconstructed from history:typescriptclass BankAccount { replayEvents(history: DomainEvent[]): void { history.forEach(event => this.apply(event)); } private apply(event: MoneyDeposited): void { this.balance += event.amount; } } - Read Model Projections: As events occur, background workers project them into specialized denormalized read stores (Elasticsearch for search, Redis for caching, PostgreSQL for reports).