Back to Spark Streaming & Structured Streaming
Q21Question 21 of 22

Aligned vs unaligned checkpoints in Flink

Key Concepts
  • Aligned checkpoints wait for barriers on all input channels before snapshotting, producing small snapshots but amplifying backpressure.
  • Unaligned checkpoints (Flink 1.11+) snapshot immediately on the first barrier, including in-flight data, eliminating checkpoint stalls.
  • Unaligned snapshots are larger and recovery is heavier because in-flight records must be replayed.
  • The alignment-timeout hybrid starts aligned and switches to unaligned only if alignment takes too long, balancing snapshot size and reliability.
  • Unaligned checkpoints require exactly-once mode and may have version-specific restrictions.
  • Monitor alignment buffering time to detect when backpressure is causing checkpoint delays.

Definition

Aligned checkpoints wait for all barriers and exclude in-flight data but stall under backpressure, while unaligned checkpoints snapshot immediately including in-flight data to stay fast under backpressure.
  • Aligned checkpoint – Operator waits for the barrier on all input channels before snapshotting, buffering fast channels; produces small, clean snapshots but amplifies backpressure when one input is slow.
  • Unaligned checkpoint – Operator snapshots immediately when the first barrier arrives, including in-flight records from input and output buffers; dramatically shortens checkpoint duration under backpressure.
  • In-flight data – Records sitting in network buffers that are included in the unaligned snapshot and replayed on recovery.
  • Alignment timeout – Hybrid setting where Flink starts aligned and switches to unaligned only if alignment takes too long.
  • Trade-off – Unaligned checkpoints produce larger snapshots and heavier recovery but keep checkpoints reliable when the system is stressed.

The National Bank of India wants a consistent photo of its processing line. In aligned mode, a clerk fed by two conveyors who sees the colored barrier slip on one freezes that conveyor and waits for the other, which stalls the fast conveyor. In unaligned mode, the clerk takes the photo immediately when the first barrier arrives, including every slip currently in the in-trays and out-trays, so nothing stalls but the photo is bigger.

Understanding the Concepts

How does aligned checkpointing work?

In aligned checkpointing, when a barrier arrives on one input channel of a multi-input operator, the operator stops consuming that channel and buffers further records while continuing others. Only when all channels have delivered the barrier does the operator snapshot.

JAVA
// Aligned checkpointing (default)
StreamExecutionEnvironment env =
    StreamExecutionEnvironment.getExecutionEnvironment();
env.enableCheckpointing(10_000);
env.getCheckpointConfig().setCheckpointingMode(
    CheckpointingMode.EXACTLY_ONCE);  // uses aligned by default
DIAGRAM
Aligned checkpoint with 2 inputs:
  Input 1 (fast): [R1][R2][B42][R3][R4]  ← barrier arrives first
  Input 2 (slow): [C1]...........[B42]   ← barrier arrives late

  Step 1: B42 on Input 1 → stop Input 1, buffer R3, R4
  Step 2: Wait for B42 on Input 2 (slow!)
  Step 3: B42 on Input 2 → snapshot state
  Step 4: Forward B42, drain buffered R3, R4

  Problem: Input 1 was stalled for the entire wait!
  Backpressure propagates upstream on Input 1.

What is the backpressure amplification problem?

When one input is slow (due to skew or a slow upstream), the operator buffers the fast channel's records while waiting, which propagates backpressure upstream on the fast channel. Under heavy backpressure, aligned checkpoints can take very long or time out entirely.

JAVA
// Scenario: market-open spike
// Input 1 (transactions): 100K records/sec
// Input 2 (reference data): 1K records/sec (slow)

// Aligned CP: barrier on Input 1 at t=0
// Wait for Input 2's barrier... takes 30 seconds
// During those 30 seconds, Input 1 is buffered
// 3 million records buffered! Memory pressure.
// Backpressure sent upstream on Input 1.
// Checkpoint may time out.

How does unaligned checkpointing work?

With unaligned checkpoints (Flink 1.11+), the operator does not wait. When the first barrier arrives on any input, it immediately snapshots its state plus all in-flight records in its input and output buffers. The barrier can overtake buffered records.

JAVA
// Enable unaligned checkpoints
env.enableCheckpointing(10_000);
env.getCheckpointConfig().enableUnalignedCheckpoints();

// Or with alignment timeout (hybrid approach)
env.getCheckpointConfig().setAlignedCheckpointTimeout(
    Duration.ofSeconds(5));  // start aligned, switch after 5s
DIAGRAM
Unaligned checkpoint with 2 inputs:
  Input 1 (fast): [R1][R2][B42][R3][R4]
  Input 2 (slow): [C1][C2]...[C3][C4]

  Step 1: B42 on Input 1 → SNAPSHOT IMMEDIATELY
    Snapshot includes:
    - Operator state
    - In-flight records from Input 2 buffer: [C1, C2]
    - In-flight records from output buffer
  Step 2: Forward B42 downstream (overtakes C1, C2)
  Step 3: Continue processing all inputs (no stall!)

  Result: checkpoint completes in milliseconds, no backpressure
  Cost: larger snapshot (includes in-flight data)

How are they different?

| Aspect | Aligned | Unaligned |
|---|---|---|
| Wait for all barriers | Yes | No |
| In-flight data in snapshot | No | Yes |
| Snapshot size | Smaller | Larger |
| Backpressure behavior | Amplifies backpressure | No additional backpressure |
| Checkpoint speed under load | Slow or timeout | Fast |
| Recovery cost | Lower (no in-flight replay) | Higher (replay in-flight) |
| Best for | Normal load, no backpressure | Backpressured pipelines |

JAVA
// Aligned: small snapshot, slow under backpressure
// Unaligned: large snapshot, fast under backpressure

What is the alignment timeout hybrid?

The practical sweet spot is a hybrid: start aligned and automatically switch to unaligned if alignment takes too long. This keeps snapshots small during normal operation and pays the larger-snapshot cost only when needed.

JAVA
// Hybrid: aligned by default, unaligned after 5 seconds
env.getCheckpointConfig().enableUnalignedCheckpoints();
env.getCheckpointConfig().setAlignedCheckpointTimeout(
    Duration.ofSeconds(5));

// Normal operation: alignment completes in < 5s → small aligned snapshot
// Under backpressure: alignment takes > 5s → switches to unaligned
//   → snapshot includes in-flight data → completes fast

What are the restrictions?

Unaligned checkpoints require exactly-once mode and have some restrictions depending on the Flink version. They may not combine with certain features, so check the release notes.

JAVA
// Unaligned checkpoints require EXACTLY_ONCE mode
env.getCheckpointConfig().setCheckpointingMode(
    CheckpointingMode.EXACTLY_ONCE);  // Required
env.getCheckpointConfig().enableUnalignedCheckpoints();

// AT_LEAST_ONCE mode does NOT support unaligned checkpoints
// env.getCheckpointConfig().setCheckpointingMode(
//     CheckpointingMode.AT_LEAST_ONCE);
// env.getCheckpointConfig().enableUnalignedCheckpoints(); // ERROR

What are common mistakes?

The most common mistake is not enabling unaligned checkpoints when the job experiences backpressure and aligned checkpoints are timing out. Another is enabling unaligned checkpoints unnecessarily, adding snapshot size overhead when the job is not backpressured.

JAVA
// WRONG -- aligned checkpoints timing out under backpressure
env.enableCheckpointing(10_000);
// Checkpoints fail repeatedly → no recovery point → dangerous!

// CORRECT -- enable unaligned with alignment timeout
env.enableCheckpointing(10_000);
env.getCheckpointConfig().enableUnalignedCheckpoints();
env.getCheckpointConfig().setAlignedCheckpointTimeout(
    Duration.ofSeconds(5));

What should a senior engineer know?

Unaligned checkpoints trade larger state and heavier recovery for reliable, fast checkpointing under load. The alignment-timeout hybrid is the real-world default because it keeps snapshots small in normal operation and only pays the unaligned cost when the system is struggling. Monitor alignment buffering time as a metric to detect when unaligned is kicking in.

Visual Overview

DIAGRAM
  Aligned vs Unaligned Checkpoint

  Aligned (waits for all barriers):
  Input 1: ─[R1][R2][B42]─────[R3][R4]──▶
                      ▲  STALL  ▲
  Input 2: ─[C1]──────────[C2][B42]──────▶
                                ▲
                          All barriers received
                          NOW snapshot
  ⚠ Input 1 stalled, backpressure upstream

  Unaligned (snapshots immediately):
  Input 1: ─[R1][R2][B42][R3][R4]───────▶
                      ▲
              SNAPSHOT NOW (includes in-flight C1, C2)
  Input 2: ─[C1][C2]────────[B42]───────▶
                              ▲
              B42 overtakes C1, C2
  ✓ No stall, no additional backpressure
  ✓ Larger snapshot (includes in-flight data)

  Hybrid (alignment timeout = 5s):
  Normal:       aligned checkpoint (small)
  Backpressure: switch to unaligned after 5s (fast)

Key Takeaway

Aligned and unaligned checkpoints are two modes for how multi-input operators snapshot during Flink's barrier-based checkpointing. In aligned mode, when the barrier arrives on one input, the operator stalls that input and waits for barriers on all other inputs before snapshotting. This gives clean, small snapshots but amplifies backpressure because fast channels stall while waiting for slow ones, and under heavy load aligned checkpoints can time out.

Unaligned checkpoints fix this by snapshotting immediately when the first barrier arrives, including the in-flight records in input and output buffers as part of the snapshot. The barrier can overtake buffered records. Because there is no waiting, checkpoints complete quickly even under severe backpressure. The cost is larger snapshots and heavier recovery because in-flight records must be replayed.

The practical sweet spot is the alignment-timeout hybrid: start each checkpoint aligned, and automatically switch to unaligned only if alignment takes too long. This keeps snapshots small during normal operation and pays the larger-snapshot cost only when the system is struggling.

At the National Bank of India an enrichment job joining fast transactions with slow reference data experienced backpressure during market-open spikes. Aligned checkpoints timed out repeatedly. Enabling unaligned checkpoints with a five-second alignment timeout solved the problem: calm periods produced small aligned snapshots, and spikes produced slightly larger unaligned ones that completed reliably.

Remember one sentence: Aligned checkpoints wait for all barriers and amplify backpressure, unaligned checkpoints snapshot immediately with in-flight data to stay fast under load, and the alignment-timeout hybrid is the real-world default.

---

Share
Use ← → arrow keys to navigate