Dev.Lib
arrow_backBack to Blogs
LEVEL_044.7June 27, 20261 min read

Event sourcing in frontend

State & Data Flow

Append-only event log as source of truth; UI derives state by folding events — enables undo, replay, audit, time-travel debug.

Summary

Append-only event log as source of truth; UI derives state by folding events — enables undo, replay, audit, time-travel debug. Heavier than CRUD state; fits collaboration, editors, complex wizards. Snapshot periodically for performance. Not every app needs it — cost vs benefit.

Code example

function eventsReducer(events, action) {
  switch (action.type) {
    case 'APPEND':
      return [...events, action.payload];
    case 'UNDO':
      return events.slice(0, -1);
    default:
      return events;
  }
}

const state = events.reduce(eventsReducer, []);

Key points

  • Append-only event log as source of truth; UI derives state by folding events — enables undo, replay, audit, time-travel debug.
  • Heavier than CRUD state; fits collaboration, editors, complex wizards.
  • Snapshot periodically for performance.
  • Not every app needs it — cost vs benefit.

Further reading

Related concepts