Skip to main content
InterviewHubPRO
Section Overview
Q25

Question 25 of 50

Part 3: Advanced Architecture & Production Patterns (Senior / Lead)

50%
Domain-Driven Design (50 Qs)•Part 3: Advanced Architecture & Production Patterns (Senior / Lead)

What is Event Sourcing and how does it integrate with DDD Aggregates?

senior LevelSystem Design

In Simple Words: In an Event-Sourced aggregate, the state of the entity is reconstructed dynamically by replaying its domain events:

  • The database stores an events stream table: (aggregate_id, version, event_type, payload).
  • When loading an aggregate:
    1. Fetch all events where aggregate_id = 'ORD-101' ordered by version.
    2. Instantiate an empty Order object.
    3. Apply each event sequentially (apply(OrderCreated), apply(ItemAdded)).
  • Snapshotting: To prevent replaying 100,000 events, take a state snapshot every 100 events, replaying only events occurring after the snapshot.