Chapter 19 · Reading
From streams to tables and back
An append, an upsert and a retraction carry different meaning.
Prerequisites Expand and re-key; Build a materialized view. Find chapters in the course contents.
Translate between event/change streams and materialized views without double-counting updates or losing delete/version semantics.
The situation
The dashboard receives a total of 10, then a corrected total of 15. Adding both produces 25, even though only 15 is current. Another consumer needs every correction for audit. Both consumers can be correct only if the stream explains whether each message is a contribution, a replacement or a deletion.
Tell new contributions and replacements apart
An append-only business event contributes a new fact. An upsert replaces the value for a key. A retraction removes a previous contribution; some systems represent an update as a retract/insert pair. A delete may use a tombstone with a specific encoding. The wire shape alone is insufficient: a number 15 can mean add 15 or replace the old total with 15.
For an aggregate over a changing table, replacing an entity amount from 10 to 12 contributes a net change of +2, not +12. Applying a replacement twice should leave the same visible row under an idempotent upsert contract. That property does not prove only one write request happened, nor does it apply automatically to append-only notifications or charges.
A current view compresses history
A keyed projection stores the current accepted fact for each entity. It can answer current-price queries efficiently but cannot necessarily reconstruct every historical value or explain why a correction occurred. A separate retained history can serve those questions. Decide explicitly whether audit, current queries and temporal as-of queries need different stores or indexes.
A delete removes a visible value, but a retained version/tombstone can still be needed to reject a delayed old update. The materialized-view lab demonstrates this with A deleted at version 3 and a later version-2 delivery. Forgetting deletion metadata changes which replays can be safely accepted. Tie tombstone cleanup to a justified replay boundary and version authority.
Worked case
This worked case uses an example written for the course.
| Input change | Correct current value | Incorrect additive value | Why |
|---|---|---|---|
| A upsert 10 | A=10 | 10 | First fact |
| A upsert 12 | A=12 | 22 | Replacement, not contribution |
| A delete v3 | A absent | 22 | Delete needs defined semantics |
| A old upsert v2 | A remains absent with version guard | 33 | Retained tombstone rejects stale history |
Match the starting snapshot to the log
A projection often starts with a snapshot and then consumes changes. A snapshot without a coordinated source position can miss or duplicate changes around its creation. Record the snapshot consistency boundary and the position from which continuing changes apply; those positions may be database log coordinates, Kafka partition positions or another source-specific token.
For a log with several partitions, record the next offset for every partition. Event-time progress is another quantity entirely. Keep transform/schema versions and destination namespace in a replay manifest. Build a new projection alongside the old one, validate it, catch up to a declared cut and promote under a fencing or atomic-switch contract appropriate to the query system.
Expose revisions to readers
A query consumer needs to know whether a value is provisional, corrected, deleted or final under a stated policy. Stable window/entity keys and revisions let it replace earlier values rather than sum changing totals. A slow reader may skip intermediate versions yet still reconstruct current state; an audit reader may not be allowed to skip them.
Test the meaning of changes with examples before measuring throughput. Duplicate a replacement, reorder versions, deliver a delete followed by an old value, and rebuild from retained history. Check both final state and the observed change stream: a correct final table can hide duplicate notifications or missing intermediate audit records.
Think it through
The query team asks to retain only the latest product row. Finance asks to explain every correction for seven years. Can one compacted current view satisfy both requirements?
Show hints
Hint 1
Current state and complete history answer different queries.
Hint 2
Compaction may intentionally remove intermediate values.
Hint 3
A version guard prevents stale updates but does not preserve all business context.
Show explanation
Not by assuming the current view is a full audit history. Specify separate retention/query contracts, often with an immutable identified history and a rebuildable current projection. Include deletion/privacy obligations and version authority. Validate snapshot-plus-change replay and make revision semantics explicit to query clients.
How frameworks implement this
Kafka Streams formalizes stream/table relationships and stateful changes. Flink dynamic tables use changelog semantics appropriate to their operators and sinks. Spark output modes are another contract. The Bytewax lab implements an explicit versioned state policy; it does not silently inherit these frameworks’ table or sink guarantees.
ReferencesPrimary sources for this chapter’s design case
Use these guides to check the framework or protocol contract discussed in this chapter. Moving “stable” or “current” guides can describe a different release; check the version used by your deployment. These readings do not execute the distributed frameworks they compare.
Kafka Streams concepts
Flink dynamic tables
Debezium snapshots