Chapter 17 · Reading
Where does a key live?
Partitions, exchanges and parallel state.
Prerequisites Read a partitioned log; Keep state by key. Find chapters in the course contents.
Trace a record from source partition to keyed operator, and distinguish ordering from parallelism.
The situation
North’s orders are correct on one worker. Add a second worker, split the input, and the totals disagree. Both tasks now hold only part of the same shop’s history.
Two different kinds of partition
A source partition is an ordered segment of a log. An operator partition is a share of processing work. They need not have the same count or the same assignment. A source with four partitions can feed two tasks, and a later operator can redistribute those records again.
A keyed exchange routes equal keys to the same downstream task so that one logical owner can update each key’s state. That owner can change after recovery or rescaling. The contract is about ownership, not about pinning North to a particular machine forever.
Order belongs to a boundary
Suppose North’s records arrive from two upstream tasks. The receiving task serializes state updates, but this does not create an event-time order between the two inputs. Per-channel order is not a global sequence. Addition may tolerate different arrival orders; “take the latest address” needs an explicit timestamp and tie-breaking rule.
A partitioning key is also a load decision. Keying by shop is convenient, but a shop that generates half the traffic can dominate one task. Random distribution balances work only by giving up direct ownership of the complete keyed state.
Worked case
Authored routing example. The mapping is explicit, not a claim about Kafka’s default partitioner or Flink’s hash function.
| Input | Source partition | Downstream key owner | Effect |
|---|---|---|---|
| a · North · £12 | P0 | Task A | North = 12 |
| b · South · £8 | P1 | Task B | South = 8 |
| c · North · £5 | P1 | Task A after exchange | North = 17 |
Follow the ownership before adding workers
Draw source assignments, network exchanges and state owners separately. Ask which operators are chained in one task and which communicate over channels. A slow task can be downstream of healthy source readers; adding source readers alone may not change its service capacity.
When changing partitioning, define how existing state and future records meet at their new owner. Restarting with another modulo function and empty memory is not state migration. The rescaling chapter explains the additional requirements.
Think it through
If both tasks compute a local North total, can you safely add their outputs whenever either changes?
Show hints
Hint 1
Distinguish an updated total from a new contribution.
Hint 2
Task A can emit 12 and later 17. Adding both counts its earlier contribution twice.
Show explanation
Not without a protocol for updates. Merge deltas, replace values identified by task and version, or use an aggregate with a defined merge operation and a consistent cut. Sending changing totals as if they were independent contributions double counts.
How frameworks implement this
Kafka partitions determine log ordering and consumer assignment. Flink keyBy redistributes operator input; it does not repartition the original topic. Spark Structured Streaming distributes stateful work through its execution and state-store machinery. None gives global event-time order merely because a key is present.
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.
Flink dataflow execution
Kafka introduction