Skip to content
Flink · How it works

Checkpoints and savepoints: how Flink keeps state safe

How checkpoint barriers give Flink a consistent snapshot without stopping the job, how state backends and incremental checkpoints affect cost, how exactly-once reaches sinks, and when to use a savepoint instead.

Flink's promise is that a job can fail (a machine dies, a pod is evicted, a bug throws an exception) and resume as if nothing had happened: no events lost, none counted twice. That promise rests on one mechanism, the checkpoint. Understanding how it works explains most of what you'll tune in production: checkpoint duration, state size, recovery time and end-to-end exactly-once.

What a checkpoint captures

A checkpoint is a consistent snapshot of the whole job: the state of every operator (counts, windows, join buffers, timers) and the position in every source (for Kafka, the offset of each partition). Consistent means all of it corresponds to the same point in the stream: every event before that point is reflected in the state, and none after it.

Barriers: a snapshot without stopping the world

  1. The job manager triggers checkpoint n. Each source records its position and injects a barrier into its output.
  2. Barriers flow with the data. When an operator has received barrier n on all its inputs, it snapshots its state and forwards the barrier. The snapshot is written to durable storage asynchronously, so processing continues while it uploads.
  3. A transactional sink pre-commits the data it has written since the last checkpoint.
  4. When every task has acknowledged, the checkpoint is complete, and Flink notifies the tasks; the sink commits its transaction.

On failure, Flink restarts the job from the latest completed checkpoint: state is restored, sources rewind to the recorded positions, and the events since then are processed again. Because the state was captured at exactly those positions, the replayed events are counted once.

Aligned and unaligned checkpoints

An operator with several inputs must wait for the barrier on all of them before snapshotting; inputs that have already delivered their barrier are held back meanwhile. That is alignment, and under backpressure it can take minutes, because the barrier queues behind buffered data. Unaligned checkpoints let the barrier overtake buffered data and store that in-flight data as part of the checkpoint. They complete quickly under backpressure, at the cost of larger checkpoints. If checkpoints slow down when the job is busiest, try them, and also fix the backpressure; see our tuning guide.

State backends and incremental checkpoints

BackendWhere state livesFits
HashMap (heap)Java objects on the heapSmall to moderate state; fastest access; full snapshot each checkpoint
RocksDB (embedded)Local disk, with a memory cacheLarge state, beyond memory; supports incremental checkpoints

With incremental checkpoints, each checkpoint uploads only the files that changed since the last one, which makes checkpoints of large state cheap. Recovery still needs the full state, so restore time grows with state size; local recovery (keeping a local copy of state on the task manager) shortens it when the same machine restarts. Flink 2.0 also introduced an experimental disaggregated state backend (ForSt) that keeps state primarily in remote storage; it is worth evaluating, with care, for very large state on cloud object storage.

Exactly-once, end to end

Inside Flink, checkpoints give exactly-once state. Reaching the outside world exactly once needs the sink's cooperation:

  • Transactional sinks such as the Kafka sink in exactly-once mode write each checkpoint's output in a transaction and commit it only when the checkpoint completes. Consumers must read with isolation.level=read_committed to see only committed data, and they see it one checkpoint interval later, so the interval becomes part of your end-to-end latency. The Kafka transaction timeout must exceed the checkpoint interval plus the longest expected recovery, or transactions are aborted and data is lost.
  • Idempotent sinks (an upsert by key into a database or search index) reach the same effect without transactions: replayed writes overwrite themselves.
  • At-least-once is often enough, and cheaper, when consumers deduplicate by event id.

Savepoints: checkpoints you control

A savepoint is a snapshot you trigger yourself, kept until you delete it, intended for planned operations: upgrading the job's code, changing its parallelism, moving to a new cluster or Flink version, or forking a job to test a change against real state. Checkpoints are for recovery and are managed by Flink; savepoints are for change and are managed by you.

Set a uid on every stateful operator. A savepoint maps state to operators by their ids. If you let Flink generate them, a small change to the job graph changes the ids, and the state can't be restored. Explicit uid() values make savepoints survive code changes.

A worked example: interval and transaction timeout

As an illustration, take a job writing to Kafka in exactly-once mode with a one-minute checkpoint interval. Downstream consumers reading committed data see each record up to about a minute after it was processed, plus the time the checkpoint takes, so a latency target of seconds rules this design out from the start. Separately, the Kafka broker caps producer transaction timeouts at transaction.max.timeout.ms, 15 minutes by default, while Flink's Kafka sink defaults its transaction timeout to one hour, so set transaction.timeout.ms explicitly or the producer will be rejected. The sink's transaction timeout has to fit under that cap and still be longer than a checkpoint interval plus the longest recovery you expect; if a recovery can take longer, raise the broker's cap rather than hoping it won't happen.

What happens during a restore

  1. Pick the snapshot. After a failure, the job manager takes the latest completed checkpoint; on a planned start, the savepoint you name.
  2. Redeploy the tasks and give each subtask the part of the state it owns, from durable storage or a local copy if local recovery applies.
  3. Rewind the sources to the positions recorded in the snapshot, for Kafka the offset of each partition.
  4. Settle the sinks. A transactional sink commits transactions belonging to the completed checkpoint and aborts any that were left open.
  5. Resume processing. Events after the snapshot are processed again, and the state reflects each of them once.

Settings worth deciding deliberately

  • Checkpoint interval: shorter means less to replay after a failure and lower exactly-once latency, but more overhead. Seconds to a few minutes is typical.
  • Minimum pause between checkpoints, so a slow checkpoint doesn't leave the job checkpointing all the time.
  • Checkpoint timeout, and how many consecutive failures are tolerated before the job fails over.
  • Retention: keep externalised checkpoints on cancellation, so a job cancelled by mistake can be restored.
  • Storage: durable, shared storage (object storage or HDFS), never a task manager's local disk.

Savepoints (or retained checkpoints) are the classic way to rescale a job; see scaling Flink jobs. On Kubernetes, the operator can take them for you on every upgrade; see the Kubernetes guide.

Questions we are often asked

Can I restore from a checkpoint instead of a savepoint?

Often yes, if the checkpoint was retained. Retained checkpoints are the fallback when a savepoint can't be taken because the job is unhealthy. Savepoints remain the planned path for upgrades, because they are under your control and are not cleaned up by Flink.

What if I removed an operator and its state is in the savepoint?

Flink refuses to restore by default, so state is never dropped silently. If dropping it is intended, start with the option to allow non-restored state.

How often should we test a restore?

Regularly, and before every Flink version upgrade. A savepoint that has never been restored is a hope, not a backup.

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