Skip to Content
Mengty Learns
Architecture

Event Sourcing: Overview, Projections, and Traps

Store every state change as an append-only sequence of events, rebuild state with projections and snapshots, and recognize when Event Sourcing is a trap.

explanationadvancedTested with typescript 7.0.2Updated Nov 27, 2026

TL;DR

Event Sourcing stores every state change as an append-only event (OrderCreated, ItemAdded, OrderPaid) instead of overwriting rows in place. Projections build queryable read tables from the stream, snapshots speed up long replays—and for 90% of apps, it's overkill compared to a normal DB + audit log.

Why it exists

Traditional UPDATE orders SET status = 'cancelled' erases how and why state changed. In domains where the full history is the business (banking ledgers, accounting, git, collaborative editing, strict compliance), an immutable event log gives you a built-in audit trail, time-travel debugging, and the ability to build new read projections retroactively.

Mental model

Instead of storing balance = 150, you store [Deposited(200), Withdrawn(50)] and fold them (reduce) to compute 150:

  • Event Store: append-only log keyed by (stream_id, version) with optimistic concurrency (expected_version).
  • Projection: background worker that turns events into normal SQL tables for fast SELECT queries (pairs with CQRS).
  • Snapshot: checkpoint of folded state at event #1000 so replaying #1001..#1020 stays fast.
Writes append events; Aggregates replay from snapshots while Projections build query tables.

Do it

Folding an Order's current state from its event stream in TypeScript:

// TypeScript 7.0.2
type OrderEvent =
  | { type: 'OrderCreated'; orderId: string }
  | { type: 'ItemAdded'; priceCents: number; qty: number }
  | { type: 'OrderPlaced' };

interface OrderState {
  id: string;
  totalCents: number;
  status: 'draft' | 'placed';
}

export function foldOrder(events: OrderEvent[]): OrderState {
  return events.reduce<OrderState>(
    (state, evt) => {
      switch (evt.type) {
        case 'OrderCreated':
          return { ...state, id: evt.orderId };
        case 'ItemAdded':
          return { ...state, totalCents: state.totalCents + evt.priceCents * evt.qty };
        case 'OrderPlaced':
          return { ...state, status: 'placed' };
      }
    },
    { id: '', totalCents: 0, status: 'draft' },
  );
}

Gotchas (when it's a trap)

  • Eventual consistency everywhere: you cannot run SELECT * FROM orders WHERE email = ? against an event stream without maintaining a separate projection.
  • Event schema evolution: old events live forever—renaming or splitting fields requires upcasters that translate v1 events to v2 on every read.
  • GDPR / PII deletion: deleting a user's personal data conflicts with an "immutable" append-only log (requires crypto-shredding—encrypting PII per user and deleting the key).
  • Confusing Event Sourcing with Event-Driven: you can publish domain events (via Transactional Outbox) without Event Sourcing your database!

Production checklist

  • Confirm you actually need full historical state reconstruction (not just a standard audit_logs table)
  • Enforce optimistic concurrency (expected_version) on every stream append
  • Version every event schema and write unit-tested upcasters before changing event shapes
  • Keep PII out of events—or encrypt it per subject key (crypto-shredding) for deletion compliance

Check yourself

Check Yourself

1. In Event Sourcing, what is the purpose of a Snapshot?

2. Your team wants to publish OrderPlaced to Kafka when orders are placed. Do you need Event Sourcing?

Sources