Course contentsShow
← System Design
Lesson 586 of 1,91916. Distributed TransactionsFree lesson

Alternative: Event Sourcing for Consistency

Using immutable event logs and projections to achieve consistency without distributed locks.

Alternative: Event Sourcing for Consistency

What You'll Learn

You'll learn how to achieve consistency across distributed systems by storing immutable event logs instead of using distributed locks or two-phase commit.

The Core Idea

Instead of updating state directly and coordinating that update across multiple systems with locks, event sourcing records every state change as an immutable event in an append-only log. The current state is derived by replaying these events.

Think of it like a bank ledger: instead of updating your balance directly (overwriting the old value), the bank records every deposit and withdrawal. Your balance is calculated by summing all transactions. If there's ever a dispute, you can replay the entire history.

How It Works

  1. Write events, not state: When something happens (order placed, payment received), you write an immutable event describing what happened
  2. Events are facts: "OrderPlaced at 10:05am" cannot be undone, only compensated with a new event like "OrderCancelled"
  3. Projections compute state: Different services read the event log and build their own view of current state
  4. No distributed locks needed: Since events are immutable and append-only, there's no cross-system coordination during writes

Consistency Without Coordination

Event sourcing achieves consistency through eventual convergence: all systems processing the same ordered event log will eventually reach the same state. You avoid the blocking problems of two-phase commit because there's no distributed transaction—just events flowing through the system.

Services can process events at their own pace, rebuild state from scratch if needed, and create multiple projections from the same event stream.

Key Takeaway: Event sourcing achieves distributed consistency by storing immutable events as the source of truth, allowing systems to derive consistent state independently without distributed locks or blocking protocols.