š Event Sourcing: When Kafka Becomes Your Database
When Kafka Becomes Your Database
Event Sourcing is not a storage optimization.
It is a truth model.
If you adopt it casually, it will break your system.
If you adopt it deliberately, it will change how you think about data forever.
1ļøā£ The Fundamental Shift (This Is the Whole Game)
Traditional CRUD Model
āStore the current state.ā
Order {
status = SHIPPED
total = 100
}
Event Sourcing Model
āStore everything that happened.ā
OrderCreated
ItemAdded
PaymentCaptured
OrderShipped
State is derived. Events are the source of truth.
2ļøā£ Why Kafka Fits Event Sourcing Naturally
Kafka provides:
Append-only log
Ordered per partition
Durable history
Replayability
That maps perfectly to event sourcing requirements.
Kafka doesnāt store rows.
It stores facts.
3ļøā£ Core Principle (Non-Negotiable)
Events are immutable.
You do NOT:
Update events
Delete events
Fix events
You only:
- Append new events
If this scares you ā good. It should.
4ļøā£ The Event Store (Kafka as DB)
Topic as Table
orders-events
Partitioning Rule
partition key = orderId
This guarantees:
Per-order ordering
Parallelism across orders
5ļøā£ Commands vs Events (Critical Distinction)
Command
āPlease do this.ā
CreateOrder { orderId: 42 }
Event
āThis happened.ā
OrderCreated { orderId: 42 }
Commands can fail.
Events never lie.
6ļøā£ Building State by Replaying Events
Example: Order Aggregate
function replay(events) {
let state = { status: "NEW", items: [] };
for (const e of events) {
switch (e.type) {
case "OrderCreated":
state.id = e.orderId;
break;
case "ItemAdded":
state.items.push(e.item);
break;
case "OrderShipped":
state.status = "SHIPPED";
break;
}
}
return state;
}
The database is now a function of history.
7ļøā£ Why This Is Powerful (Correctness Wins)
You gain:
Full audit log
Perfect traceability
Temporal queries (āwhat did we know then?ā)
Replay after bugs
Deterministic recovery
Debugging becomes re-running history, not guessing.
8ļøā£ Event Sourcing vs Traditional DB (Truth Table)
| Dimension | CRUD DB | Event Sourcing |
|---|---|---|
| History | ā Lost | ā Preserved |
| Audit | Hard | Free |
| Replay | ā | ā |
| Fix bugs | Manual | Reprocess |
| Simplicity | ā | ā |
9ļøā£ The First Big Trap ā Read Performance
Replaying 10,000 events on every read is not acceptable.
ā Naive
Read ā replay all events
ā Solution ā Projections (Read Models)
1ļøā£0ļøā£ Projections (Materialized Views)
Projections turn events into queryable state.
events ā projection ā read API
Example Projection
on("OrderCreated", e => db.insert({ id: e.orderId }));
on("ItemAdded", e => db.addItem(e.orderId, e.item));
on("OrderShipped", e => db.updateStatus(e.orderId));
Kafka is the write database.
Projections are read databases.
1ļøā£1ļøā£ Eventual Consistency (You Must Accept This)
Writes:
- Immediate
Reads:
- Eventually consistent
If you need:
Strong read-after-write
Immediate consistency
Event sourcing may be a bad fit.
1ļøā£2ļøā£ Schema Evolution (The Silent Killer)
Events live forever.
You cannot:
Change event meaning
Rename fields casually
Rule
Events are public contracts.
1ļøā£3ļøā£ Versioned Events (Required)
OrderCreatedV1 { orderId }
OrderCreatedV2 { orderId, currency }
Consumers must handle all historical versions.
1ļøā£4ļøā£ The Second Big Trap ā Exactly-Once Illusion
Kafka gives:
- At-least-once delivery
Your projection must be:
Idempotent
Offset-aware
if (alreadyApplied(eventId)) return;
apply(event);
1ļøā£5ļøā£ Deleting Data (GDPR Nightmare)
You cannot delete events.
Common Solutions:
Encrypt PII and delete keys
Redaction events
Data minimization
Event sourcing + GDPR requires design-time planning.
1ļøā£6ļøā£ When Kafka Becomes Your Database (Danger Zone)
You are now responsible for:
Backups
Retention policies
Compaction strategy
Disaster recovery
Cross-region replication
Kafka is a database, whether you admit it or not.
1ļøā£7ļøā£ When Event Sourcing Is the Right Choice
Use it when:
Auditability is critical
Business logic is complex
Reprocessing is valuable
History matters
You can afford complexity
Examples:
Payments
Financial ledgers
Order lifecycles
Trading systems
1ļøā£8ļøā£ When Event Sourcing Is a Bad Idea
Avoid it when:
CRUD is simple
Strong consistency required
Team is small
Schema changes frequent
Operational maturity low
šÆ Mental Model to Remember
Traditional DBs store answers.
Event sourcing stores evidence.
