Implementations and comparison
Running totals in Flink
Follow a keyed DataStream reduction and see how Flink aligns checkpoint barriers across input channels.
Learn with an AI assistant
Copy the prompt and paste it into a chat with an AI assistant.
You are a tutor for the course "Stream Processing".Topic: "Running totals in Flink"In short: Follow a keyed DataStream reduction and see how Flink aligns checkpoint barriers across input channels.Topic text in Markdown: https://stream-processing-systems.github.io/implementations/running-totals-in-flink.mdBase your explanations on the topic text. First ask how I want to study: a short summary, a step-by-step walkthrough, or a deep dive with questions that check my understanding. Use the text's example and terms. If you add anything the text does not say, point it out.The usage page needs the latest token total for each account. A continuous SQL query describes the result. The team can also use Flink’s Java DataStream API to describe the processing steps themselves. What happens when contributions reach the same step from several upstream tasks and the job needs to recover their shared total?
Route contributions by key
Section titled “Route contributions by key”A Flink job connects a source, operators, and a sink. An operator is a processing step in the job. Here the source supplies pairs containing an account ID and the number of tokens from one accepted request. The operator adds each contribution to the account’s running total.
This Flink 2.3.0 Java excerpt starts with usage, a
DataStream<Tuple2<String, Long>> connected to the source.
DataStream<Tuple2<String, Long>> totals = usage .keyBy(row -> row.f0) .reduce((total, next) -> Tuple2.of(total.f0, total.f1 + next.f1));f0 is the account ID and f1 is the token count. keyBy routes records
for the same key to the same parallel instance of the next operator. In
streaming mode, reduce keeps the first pair for a key as its initial
state. It then combines that state with each new pair and emits the
updated total.12
When acme is at 1,400 and another 600 tokens arrive, the operator emits
(acme, 2000). A sink for the usage page would use acme as the key and
replace its row. The excerpt leaves the sink to the job configuration.
When all the accepted requests
have arrived, acme has 2,500 tokens and delta has 300. Every account
keeps its own total, and a delayed contribution still belongs to that total.
Align the checkpoint across input channels
Section titled “Align the checkpoint across input channels”A consistent checkpoint
pairs state with the input positions that produced it. After keyBy, even
an operator with one logical input can receive records over several input
channels, one from each upstream instance that sends it data. For example,
parallel readers may read separate files containing contributions for the
same account. Each reader can reach the checkpoint boundary at a different
time.2
DataStream checkpointing is off by default. The team enables it in the job configuration.3 With aligned checkpointing, Flink inserts a checkpoint barrier at a position in each source stream. A barrier travels with the records and carries the checkpoint ID. Once an operator receives a barrier on a channel, it holds back that channel’s later records while it waits for the same barrier on its other channels.2
Suppose channel A has 1,000 tokens for acme, barrier 7, then 400 tokens.
Channel B has 600 tokens for acme, then barrier 7. The running total starts
at zero. The 1,000 and 600 precede their channels’ boundaries, so checkpoint
7 must include them both.
| What the operator receives | What it does | acme total |
|---|---|---|
| A’s 1,000, then A’s barrier | Adds 1,000 and holds back A’s 400 | 1,000 |
| B’s 600 | Keeps reading B and adds 600 | 1,600 |
| B’s barrier | Captures the state for checkpoint 7 | 1,600 |
| A’s 400 after alignment | Resumes reading A and adds 400 | 2,000 |
This wait is barrier alignment. Suppose the job fails after adding the 400, with checkpoint 7 still the latest one the coordinator has confirmed. Flink restores 1,600 and resumes the replayable sources from their saved positions. The 400 is processed again and the total returns to 2,000. Had the operator saved 2,000 with those same positions, the replay would produce 2,400.2
A sink could already have received the first update to 2,000 before the failure. To recover the usage page safely, the job needs a replayable source, checkpoint storage that survives the failure, and a sink with a compatible transaction or safe retry protocol.34
What to remember
Section titled “What to remember”keyBysends contributions for an account to the same keyed operator instance. The rolling reduction emits updated totals.- Alignment waits for the same checkpoint barrier on every input channel. The resulting snapshot can be restored with the saved source positions.
- Recovery of an external result depends on the sink’s guarantees as well as checkpointing.
References
Section titled “References”© 2026 Grig Alex · CC BY-NC-SA 4.0
