"The Flink job is slow" usually means one of three things: it can't keep up with its input (lag grows), its output is late (latency is high), or its checkpoints take so long they fail. The instinct is to add parallelism or memory. Sometimes that's right. More often the job is limited by one operator, for one reason, and finding it is quicker than guessing.
This guide is the order in which we look. Most of it uses what the Flink web UI and metrics already show.
Step 1: find the bottleneck with backpressure
When an operator can't keep up, its input buffers fill and the operators upstream of it can't send: they are backpressured.
Flink shows, per subtask, how much time each spends busy, backpressured and idle (the
busyTimeMsPerSecond, backPressuredTimeMsPerSecond and idleTimeMsPerSecond metrics, and colours in the
job graph). Walk the graph from the source: the bottleneck is the first operator that is busy, not backpressured, with backpressured operators
upstream of it. Everything else is a symptom.
Note that operators chained into one task share a thread and show as one. To see which of them is slow, temporarily disable chaining for that part of the job, or look at the per-operator metrics.
Step 2: is it skew?
Open the bottleneck operator's subtasks. If one or two subtasks are busy and the rest are mostly idle, the problem is skew: some keys carry far more records than others, and their subtasks do most of the work. Adding parallelism won't help, because the hot key still lands on one subtask. Fixes:
- a more granular key, if the business logic allows it;
- two-stage aggregation: pre-aggregate on the key plus a random salt, then combine by key;
- for joins against a hot key, broadcasting the small side instead of keying both.
If all subtasks are equally busy, the operator is simply doing too much work per record. Read on.
Step 3: serialization
Every record crossing a network boundary (a keyBy, a rebalance) is serialized. Flink is fast with types it understands (POJOs
that follow its rules, Avro, Tuples, rows) and slow when it falls back to Kryo for generic types. Check the logs for the "will be processed as
GenericType" message, and disable generic types in tests so any fallback fails loudly. Moving large payloads you don't need through the job is
the other common cost: project to the fields you use early.
Step 4: state access
With RocksDB, every state read and write goes through serialization and possibly disk. Signs that state is the bottleneck: high busy time in a stateful operator, disk I/O on task managers, and RocksDB metrics showing cache misses. Fixes:
- give RocksDB enough managed memory for its block cache and write buffers, and fast local disks;
- use
MapStaterather than a largeValueStateholding a map, so each access touches one entry, not the whole structure; - expire state you no longer need with TTL and timers;
- if state is small, the heap state backend is faster than RocksDB.
Step 5: calls to the outside world
A database lookup or HTTP call per record inside a map makes the operator wait on the network for every event. Use
Async I/O to keep many requests in flight, cache results where they can be slightly stale, or turn the lookup data into a
stream and join it as state, which is usually the fastest of all.
Step 6: the sink
If the sink is the busy operator, the limit may be the destination: a database's write capacity, a search cluster's indexing rate, Kafka
producer settings. Batch writes, tune the producer (linger.ms, batch.size, compression), and check the destination's own
metrics before scaling Flink.
Step 7: checkpoints
Slow or failing checkpoints under load are usually backpressure in disguise: aligned barriers queue behind full buffers. Fix the bottleneck first. Then:
- enable unaligned checkpoints so barriers can overtake buffered data;
- enable buffer debloating, which sizes network buffers to the throughput so less data is in flight;
- use incremental checkpoints with RocksDB for large state;
- check the checkpoint storage itself: throttled object storage makes every checkpoint slow.
See checkpoints and savepoints for how these work.
Walking the steps
A job reads orders from Kafka, enriches each with customer data and writes to a search index. Lag grows every afternoon. The job graph shows the source backpressured and the enrichment operator busy, so enrichment is the bottleneck (step 1). Its subtasks are evenly busy, so it isn't skew (step 2). The task managers' CPU is low while the operator is busy, which points to waiting rather than computing: the enrichment makes a synchronous database call per order (step 5). Switching to Async I/O with a small cache removes the bottleneck, and the next busy operator is the sink, which is then tuned with larger batches (step 6).
All names and details are fictional.
Only then: scale
When the bottleneck is an evenly loaded operator doing necessary work, add parallelism to it, remembering the limits set by the maximum parallelism, the number of source partitions and the sink. See scaling Flink jobs.
| Symptom | Look first at |
|---|---|
| One subtask busy, others idle | Key skew |
| All subtasks busy, GenericType in logs | Serialization |
| Stateful operator busy, disk I/O high | RocksDB memory and state access pattern |
| Operator busy but task manager CPU low | Synchronous external calls (the thread is waiting on the network) |
| Sink busy | Destination capacity, batching |
| Checkpoints slow only at peak | Backpressure, then unaligned checkpoints |
Questions we are often asked
Will more memory make the job faster?
Only if memory is the limit. More managed memory helps a RocksDB job whose cache is too small for its working set. More heap helps a job spending time in garbage collection. Neither helps a job waiting on a database or held back by one hot key.
Is object reuse worth turning on?
Object reuse lets Flink pass the same object between chained operators instead of copying it, which saves allocation. It is safe only when no operator keeps or changes a record after passing it on. Turn it on after reviewing the code, not as a first step.
How do we know a change helped?
Compare the same metrics before and after under the same replayed traffic: busy and backpressured time of the bottleneck, records per second, checkpoint duration and end-to-end latency. A change that moves the bottleneck to another operator is progress, and the walk starts again from step 1.