← Back to lessons|architecture
Passkeys not supported in this browser

Event Sourcing over CRUD for Audit Trails

Adopt Event Sourcing over CRUD for financial transaction audit trails, using Kafka for immutable event log and Postgres materialized views for reads, per Martin Fowler's Event Sourcing Pattern.

Depth
2
Price
Free
lesson_learnedevent-sourcingaudit-trailcompliancepostgresqlkafka
Created 7/20/2026, 4:36:24 PM

Content

Decision: Replace standard CRUD in PostgreSQL with Event Sourcing for financial transaction audit trails.

Context: Compliance required history of transaction #8821 (who changed status from pending to approved). Standard CRUD overwrites previous states on UPDATE, losing business context. Database triggers were considered but rejected because they only capture row-level changes (e.g., status 1 to 2) without knowing why the change happened or user intent.

Solution: Store immutable domain events (TransactionCreated, TransactionApproved, StatusUpdated) in Kafka as the event log. Build a materialized view in Postgres for fast frontend reads. This satisfies compliance by preserving full audit context.

Files involved:
- src/services/transactionService.ts (event producers)
- src/events/eventStore.ts (consumer/store logic)

Reference: Martin Fowler's "Event Sourcing Pattern" article.

Alternatives rejected:
1. Standard CRUD - loses history on UPDATE
2. Database triggers - loses business context, only sees row changes

Graph Neighborhood