Event sourcing with Kafka: ten years of payroll
13,167 events for 100 employees over 120 months, kept in a Kafka topic and nothing else, then replayed into every view the business asks for, including the payroll as it was on any past date.
The question#
What does it take for a log to be the system of record, and what do you get for it? Payroll is a good test: it is history-heavy, it is audited, and the questions asked of it change over time.
The setup#
salaries produce ──> Kafka topic "salary-events" ──> salaries report
2015-01 ... 2024-12 13,167 events, replays them all
100 employees, kept forever into tables
month by month
Three event types: Hired, Raised (annual review or promotion) and SalaryPaid, keyed by employee. The topic has retention switched off and broker auto-creation disabled, so it can only exist with those settings.
What it shows#
- State is a fold over events. One function applies each event in turn; every table in the report is built from what it returns.
- Time travel comes free. The payroll as of March 2020 is a replay that stops at March 2020, a question a current-state table cannot answer.
- New views need no migration. The month-by-month grid was added after the events were written and needed nothing that was not already in them.
- Replay is cheap at this scale: all 13,167 events replay in about 100 ms.
Reproduce it#
git clone https://github.com/caelus-engineering-labs/kafka-event-sourcing.git
cd kafka-event-sourcing
./demo.sh # or ./demo.sh 2019 for that year month by month
The simulation is seeded, so every run produces the same numbers.
Where it stops#
These simplifications are deliberate, and each is a design decision in a real system:
- One partition, so a single reader sees global order. Production would partition by employee and order per key.
- Full replay every time. At millions of events, reports start from a snapshot.
- No optimistic concurrency. With several writers, Kafka cannot refuse a conflicting event the way a dedicated event store can.
- No schema evolution plan. Long-lived events need one before the first shape change.
Repository
- caelus-engineering-labs/kafka-event-sourcing ↗Go, about 400 lines; Kafka in Docker Compose; ./demo.sh needs only Docker. Tag v1.0.0 is an earlier event-sourced bank on Kubernetes