Skip to content
Flink · How it works

Scaling Flink jobs: parallelism, key groups and rescaling

How Flink spreads keyed state across parallel subtasks with key groups, why maximum parallelism is a one-time decision, how to rescale from a savepoint, and what the adaptive scheduler and the autoscaler add.

A Flink job runs as many parallel copies of each operator, called subtasks. Scaling a stateless job is easy: add subtasks. Scaling a stateful job is harder, because each subtask owns part of the state, and the state has to move when the number of subtasks changes. Flink handles that for you, within limits that are set the day the job first runs. This article explains the mechanism and the decisions it implies.

Parallelism, slots and the job graph

Each operator has a parallelism, the number of subtasks it runs as. Subtasks run in task slots on task managers; by default one slot can hold one subtask of every operator in a pipeline, so a job needs as many slots as its highest operator parallelism. Operators with the same parallelism and a direct, unpartitioned connection are chained into one task, which avoids serialization between them. A keyBy breaks the chain: records are hashed by key and sent across the network to the subtask that owns that key.

Key groups: how keyed state is divided

Flink doesn't assign keys directly to subtasks. It hashes each key into one of a fixed number of key groups, and each subtask owns a contiguous range of key groups. The number of key groups equals the operator's maximum parallelism. When the job is rescaled, Flink reassigns key-group ranges to the new subtasks and each one restores only the key groups it now owns. That is what makes rescaling efficient: whole key groups move, and no key's state is ever split.

Maximum parallelism is a one-time decision

  • You can never scale an operator beyond its maximum parallelism.
  • Changing the maximum parallelism changes how keys map to key groups, so existing keyed state can't simply be restored; it needs an offline state migration (for example with the State Processor API).
  • If you don't set it, Flink derives it from the initial parallelism (as 1.5 × parallelism rounded up to a power of two, with a floor of 128 and a ceiling of 32,768), which is fine for small jobs and a trap for jobs that will grow a lot.
  • A very high value costs a little: more key groups mean slightly more overhead in state handling and checkpoint metadata.

Set it explicitly, to a value comfortably above the largest parallelism you can imagine needing. A value that divides evenly by likely parallelisms (powers of two, or numbers like 720) keeps key groups balanced across subtasks.

A worked example: why the number matters

Suppose a job starts at parallelism 16 and nobody sets a maximum. Flink derives one from the initial parallelism, which for a job this size gives 128 key groups. That is fine while the job stays at 16 or 32: each subtask owns the same number of key groups. Now traffic grows and the team scales to 48. 128 key groups don't divide evenly by 48, so some subtasks own three key groups and others own two, and the busiest subtasks do about half as much work again as the quietest. And the job can never go beyond 128 without a state migration. A maximum of, say, 720 (which divides evenly by 48) or 1,024 (about 5% imbalance at 48) set on day one would have avoided the ceiling and most of the imbalance, at very little cost.

Rescaling a job

The classic way to rescale is from a savepoint:

  1. Take a savepoint (and, typically, stop the job with it).
  2. Start the job from the savepoint with the new parallelism.
  3. Flink redistributes key groups, and operator state such as source offsets, across the new subtasks.

This needs explicit uid()s on stateful operators so the state maps back to them, and a pause while the job restarts, which for large state is dominated by restoring state.

Adaptive scheduling and autoscaling

  • The adaptive scheduler lets a job adjust its parallelism to the slots available, instead of failing when it can't get them all. In reactive mode (standalone application clusters), adding or removing task managers rescales the job automatically.
  • The autoscaler in the Flink Kubernetes Operator watches each operator's processing rate and busy time, works out the parallelism each needs to keep up with its input, and rescales the job, per operator, within limits you set. See our Kubernetes guide.
Scaling doesn't fix skew. If one key carries a large share of the traffic, the subtask that owns its key group is the bottleneck at any parallelism. Look at per-subtask records and busy time before adding capacity; the fix is usually in the key, or a two-stage aggregation that pre-aggregates on a salted key.

The sources and sinks set limits too

A Kafka source can't usefully run more subtasks than its topic has partitions: extra subtasks sit idle. Sinks have their own limits: a database that accepts a few thousand writes per second won't take more because Flink sends them from more subtasks. Plan parallelism along the whole path, and size Kafka partitions with Flink's needs in mind.

A scaling checklist

  1. Set an explicit maximum parallelism on every stateful job, before its first production run.
  2. Set uid() on every stateful operator.
  3. Check for skew before scaling out.
  4. Match source parallelism to partitions, and check the sink's capacity.
  5. Practise a savepoint-and-rescale on a copy of production state, and time it.

For finding what actually limits a job's throughput, see tuning a slow Flink job.

Questions we are often asked

Can we scale down as well as up?

Yes. Rescaling works in both directions: key groups are redistributed over fewer subtasks. Make sure the smaller job still has enough memory for the state each subtask now holds.

What happens to non-keyed state, such as Kafka offsets?

Source state is redistributed too. For the Kafka source, the partitions and their offsets are reassigned across the new source subtasks, so reading resumes where it stopped.

How do we know it is time to rescale?

Sustained signs, not one busy hour: consumer lag that grows through every peak, the bottleneck operator's busy time near its maximum on all subtasks, and no skew or external call to blame. If the load is predictable, scale ahead of the peak rather than during it.

How long does a rescale take?

For a stateful job, mostly the time to take the savepoint and restore state on the new subtasks, which grows with state size and storage speed. Time it on a copy of production state so the pause is known before it is needed.

Building or rescuing a Flink job?

The Flink POD designs, builds and operates Apache Flink pipelines: event time, state, checkpoints, Kubernetes and the tuning that keeps them fast, inside your environment.

Flink POD

Keep reading

Back to blog