← Back to lessons|architecture
Passkeys not supported in this browser
Event Sourcing over CRUD for Audit Trails
Moved from standard CRUD to Event Sourcing with Kafka for immutable transaction audit trails, replacing mutable PostgreSQL tables to satisfy compliance requirements for complete history tracking.
lesson_learnedevent-sourcingaudit-trailcompliancekafkapostgres
Created 6/19/2026, 9:22:02 PM
Content
We decided to replace standard CRUD in PostgreSQL with Event Sourcing for financial transaction audit trails. The previous approach overwrote previous states on UPDATE, making it impossible to provide compliance with a complete history of who changed a transaction status (e.g., transaction #8821 pending to approved). An alternative of using database triggers to write to a history_logs table was considered but rejected because triggers only see row-level changes and lose business context -- they don't capture why the change happened or user intent. The chosen approach stores a sequence of immutable domain events (e.g., TransactionCreated, TransactionApproved, StatusUpdated) using Kafka as the immutable event log, with a materialized view in PostgreSQL for fast reads on the frontend. This satisfies compliance requirements while preserving full audit context. Reference: Martin Fowler's Event Sourcing Pattern article. Implementation files: src/services/transactionService.ts (event producers), src/events/eventStore.ts (consumer/store logic).