In typical web software, updating an account balance is done with a standard SQL UPDATE accounts SET balance = balance + 100 WHERE id = :id. In consumer apps, this might suffice. In enterprise FinTech, healthcare settlement, or regulatory billing, this pattern is an architectural anti-pattern.
When you update a row in place:
- You destroy the chronological sequence of operations.
- You lose the business intent behind why the modification occurred.
- You must rely on peripheral audit tables that are easily bypassed by direct database migrations or administrator errors.
Event sourcing shifts the paradigm: state is not stored—events that create state are stored.
Architectural Schema: The Append-Only Ledger
Every state transition is represented as a strongly-typed, immutable event inserted into an append-only table.
Command (e.g. DepositFunds)
│
▼
[Aggregate Root Validation] ──► Fails: Throw BusinessException
│
▼ Emits Event: FundsDeposited
[Event Store (Append-Only)] ──► Emits to Kafka/NATS
│
┌───┴────────────────────────┐
▼ ▼
[Read Projection (Postgres)] [Analytics Warehouse]
Fast Query Engine Compliance & Reporting
Data Model in PostgreSQL
CREATE TABLE account_event_stream (
event_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
aggregate_id UUID NOT NULL,
sequence_number BIGINT NOT NULL,
event_type VARCHAR(64) NOT NULL,
payload JSONB NOT NULL,
metadata JSONB NOT NULL,
recorded_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT uq_aggregate_sequence UNIQUE (aggregate_id, sequence_number)
);
CREATE INDEX idx_event_stream_aggregate
ON account_event_stream (aggregate_id, sequence_number ASC);
The unique constraint uq_aggregate_sequence guarantees optimistic concurrency control. If two transactions attempt to append sequence #42 simultaneously, the database engine enforces serialization and rejects the slower transaction.
Balancing Append Speeds with Read Performance: Projections
A frequent criticism of event sourcing is query speed. Replaying 50,000 events to compute a current account balance during an API call would cripple response times.
We solve this using Command Query Responsibility Segregation (CQRS):
- Write Path: Commands only append to the event log (sub-1ms execution).
- Read Path: Asynchronous or synchronous read-model projection tables maintain pre-computed balances and transaction summaries.
export class AccountAggregate {
private balance: bigint = 0n;
private version: number = 0;
public apply(event: { type: string; payload: { amount: bigint } }): void {
switch (event.type) {
case "AccountOpened":
this.balance = event.payload.amount;
break;
case "FundsDeposited":
this.balance += event.payload.amount;
break;
case "FundsWithdrawn":
if (this.balance < event.payload.amount) {
throw new Error("Insufficient funds in aggregate");
}
this.balance -= event.payload.amount;
break;
}
}
public getBalance(): bigint {
return this.balance;
}
}
Automated Snapshotting Strategies
When an aggregate exceeds 1,000 historical events, replaying from event zero adds unnecessary CPU cycles during aggregate rehydration. We deploy deterministic snapshotting:
Event 001 ──► Event 500 ──► Event 1000 [SNAPSHOT CREATED] ──► Event 1001 ──► Event 1042
▲
│
Replay starts here instead of Event 001
Every 500 events, a background worker writes the aggregate's serialized state to a snapshot table. When a command arrives, the aggregate loads the latest snapshot and replays only subsequent delta events.
This architecture has allowed Opstin financial client integrations to process over 20,000 transactions per second with sub-5ms p99 latency, while giving forensic compliance teams instantaneous point-in-time state reconstruction.



