Event Streams
Event-Driven Architecture
Model systems that communicate via events
State vs Events: Two Ways to Model Reality
- Stores current value: balance = $1,000
- Updates overwrite: SET balance = balance + 500
- Simple queries: SELECT balance WHERE account_id = 1
- History lost unless separately tracked
- Stores what happened: deposit($500)
- Append-only: never update or delete
- State is derived: SUM(amount) GROUP BY account_id
- Full history preserved by default
Immutable Append-Only Logs
Store events that never get modified
The Power of Never Deleting
Designing an Event Schema
Corrections in an Immutable Log
- Overwrite the wrong value
- History of the error is lost
- Cannot audit what happened
- Simple but destroys information
- Append a correction event
- Original error is visible for audit
- SUM of all events = correct value
- More complex but preserves everything
Event Sourcing
Rebuild state from a sequence of events
Deriving State from Events
When Event Sourcing Makes Sense
Event Sourcing in Data Engineering
Clickstream Modeling
Track user behavior as structured events
Modeling User Behavior as Events
Sessionization
Event Properties: Typed vs Semi-Structured
- All event types in one table
- properties column holds event-specific data
- Schema-flexible: new event types need no DDL
- Harder to query: JSON path access is slower
- Separate table for clicks, purchases, searches
- Typed columns: amount DECIMAL, query_text VARCHAR
- Fast queries: columnar encoding works natively
- Schema-rigid: new event types need new tables
Handling Late-Arriving Data
Process events that arrive out of order
When Events Arrive After the Window Closes
Strategies for Late Data
Event Time vs Processing Time
- Event at 2:00 PM counted in the 2:00 PM window
- Late events land in the correct historical window
- Requires watermarks and late-data handling
- Correct for analytics and reporting
- Event at 2:00 PM arriving at 5:00 PM counted in the 5:00 PM window
- Late events corrupt the current window
- Simple but produces wrong results
- Only correct when events arrive in real-time order
> You are building a pipeline for a mobile app's clickstream data. Events arrive via Kafka, some up to 24 hours late due to offline queuing.
Data that never forgets
- Category
- Data Modeling
- Duration
- 27 minutes
- Challenges
- 12 hands-on challenges
Topics covered: Event-Driven Architecture, Immutable Append-Only Logs, Event Sourcing, Clickstream Modeling, Handling Late-Arriving Data
Lesson Sections
- Event-Driven Architecture (concepts: dmEventSourcing)
State vs Events: Two Ways to Model Reality A state-based system stores the current truth: account_balance = $1,000. An event-based system stores what happened: deposit($500), withdrawal($200), deposit($700). The current balance is derived by replaying the events. Both representations contain the same information, but events are more powerful because you can reconstruct ANY past state, not just the current one. This is the fundamental insight of event-driven data modeling: events are the source o
- Immutable Append-Only Logs (concepts: dmImmutableLogs)
The Power of Never Deleting An immutable log is a sequence of events that can only be appended to. You can add new events but never modify or delete existing ones. Kafka topics, database write-ahead logs, and Git commit histories are all immutable logs. This immutability gives you three superpowers: replay, audit, and debugging. Replay: if your downstream aggregation is wrong, fix the logic and replay the log. The events are still there. Audit: every action is recorded with a timestamp and actor
- Event Sourcing (concepts: dmEventSourcing)
Deriving State from Events Event sourcing is the pattern where events are the source of truth and all state is derived by replaying them. Instead of storing 'account balance = $1,000,' you store every deposit and withdrawal event. The balance is computed by summing all events for that account. This is powerful but expensive. Replaying 10 years of events to compute a current balance is impractical. The solution: snapshots. Periodically compute the current state and save it. To get the balance, st
- Clickstream Modeling (concepts: dmEventSourcing)
Modeling User Behavior as Events Clickstream data is the most common event stream in data engineering. Every page view, button click, scroll, and search is captured as an event. The volume is massive (millions to billions of events per day) and the schema is semi-structured (each event type has different properties). Clickstream events typically share a common schema: event_id, user_id, session_id, event_type, event_timestamp, page_url, and a properties payload with event-specific data. The prop
- Handling Late-Arriving Data (concepts: dmLateArriving)
When Events Arrive After the Window Closes In the real world, events do not arrive in order. A mobile app queues clicks while offline and sends them hours later. A payment gateway batches settlements daily. A sensor loses connectivity and dumps a backlog. If your pipeline processes events by wall-clock time (when the pipeline sees them), all of these produce wrong results. The mobile clicks land in the wrong hour. The settlements land on the wrong day. The fix: process by event time (when the ev