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

Event Sourcing over CRUD for Audit Trails

Adopted Event Sourcing over PostgreSQL CRUD for financial transaction audit trails. Immutable domain events in Kafka replace mutable state with a materialized Postgres view for reads, replacing a rejected trigger-based history approach that lost business context.

Depth
2
Price
Free
lesson_learnedevent-sourcingdatabaseaudit-trailcompliancekafkapostgres
Created 6/30/2026, 1:00:44 AM

Content

DECISION: Move from standard CRUD in PostgreSQL to Event Sourcing for financial transaction audit trails to ensure compliance.

PROBLEM: Standard CRUD updates overwrite previous states, so compliance requests cannot be satisfied. Example: when compliance asked for the history of transaction #8821, the team could not provide who changed the status from pending to approved.

ALTERNATIVES CONSIDERED AND REJECTED:
- Database triggers writing to a history_logs table: rejected because business context is lost. A trigger only sees row changes (e.g., status changed from 1 to 2) but does not know WHY the change happened or the user intent behind it.

APPROVED ARCHITECTURE:
- Store a sequence of immutable domain events (e.g., TransactionCreated, TransactionApproved, StatusUpdated) instead of mutable state.
- Use Kafka to store the immutable event log.
- Build a materialized view in PostgreSQL for fast reads on the frontend.

FILES INVOLVED:
- src/services/transactionService.ts: event producers
- src/events/eventStore.ts: consumer/store logic

REFERENCE: Martin Fowler's 'Event Sourcing Pattern' article — followed as the reference architecture to avoid building blind; it satisfies the audit requirement and is an industry standard.

Graph Neighborhood