Event Streaming LabPython · Bytewax

Chapter 41 · Reading

Why did more workers not help?

State migration and the limit of a single hot key.

Prerequisites Where does a key live?; When is it safe to forget? Find chapters in the course contents.

What you’ll learn

Explain redistribution of keyed state and distinguish partition imbalance from key-level skew.

The situation

The shop adds four workers. Most tasks become idle, but one still falls behind. Half of all events share the same customer account key, so the busiest state owner remains the busiest state owner.

Move state with its future input

Rescaling a stateful job is more than starting extra processes. The restored state must be assigned to the same new owners as future records for those keys. Timers, source splits and operator state also need compatible redistribution. Stable operator identities and compatible serializers help the framework reconnect saved state to the new job.

Flink organizes keyed state into key groups, its redistribution units, associated with maximum parallelism. A rescale redistributes those units across operator subtasks. Maximum parallelism is not merely the current worker count; changing state layout or compatibility constraints requires a supported migration strategy.

A hot key is not an empty partition

If key X alone needs more than one task can process, redistributing other keys will not solve X’s serial ownership bottleneck. First distinguish one overloaded source partition, one skewed operator task and one genuinely hot logical key. Each suggests a different intervention.

For sums and other aggregates with a correct merge rule, add a shard label to a busy key, compute subtotals for those labels and combine them. This is called key salting. But order-sensitive logic, per-key timers and non-mergeable state cannot automatically use that transformation. The final combining stage may itself become a bottleneck.

Worked case

Illustrative ownership cases, not benchmark results or a universal rescaling recommendation.

DistributionOrdinary keysHot key XConsequence
2 workersSplit across 2One ownerOwner can saturate
4 workersSplit across 4Still one ownerOrdinary load improves; X may not
Salted two-stage sumPartial groupsSeveral partial ownersValid only with a correct merge contract

Count the cost of the move

Plan for checkpoint size, state download, local-state reconstruction, source replay and warm-up. A successful savepoint restore proves compatibility for that restore; it does not establish that the recovery time meets a service objective under peak load.

Compare both the new steady state and the transition. Look for skew by key, state size per task and source distribution. Avoid adding parallelism while an unrelated sink transaction limit or external rate cap is the true constraint.

Think it through

Can you salt a per-customer fraud state machine as freely as a per-customer sum?

Show hints
  1. Hint 1

    A sum has an associative merge operation.

  2. Hint 2

    A state machine can depend on the order and complete history of its transitions.

Show explanation

Generally no. Splitting one customer’s history can change which transitions are observed and in what order. You need a domain-specific decomposition with proven semantics, or a different key/business contract, rather than applying salting mechanically.

How frameworks implement this

Flink keyed-state rescaling relies on key-group redistribution and restore compatibility. Kafka topic partition changes affect routing and consumer assignment but do not independently migrate Flink state. Spark state partitioning and supported changes have their own checkpoint/query compatibility rules.

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.

This mark tracks what you’ve read.