25 / 35 · Streams and State
State and checkpoints
Identify the data a streaming operator retains and restore it together with the matching source positions.
The window total is 150 before the sale of 70 arrives. The processor applies the sale and holds 220 in memory. It then crashes. Starting from offset 43 with a restored total of 150 would skip the sale. Restoring 220 and starting from 42 would apply the sale twice. State and progress need the same boundary.
State answers a future question#
A stateful operator retains information from earlier records. For a sum it can store the total per store and window. An average also needs a count. A deduplication operator remembers event identities within its policy. A stream join can retain sales waiting for a matching payment, then discard them once a defined time or retention rule rules out useful future matches. Each state item exists because a later event may need it. Apache Flink 2.1, Stateful Stream Processing
Keeping every unmatched record forever makes that state grow if matches do not arrive. An interval join can bound which event times are eligible to match. The cleanup rule then also has to specify what happens to records that arrive too late. Eviction is part of the result’s semantics as well as a memory decision.
A toy report has 10,000 stores, three active windows per store and 24 bytes of values per aggregate. That gives bytes of aggregate values. The estimate excludes keys, indexes, timers, retained join rows, serializer overhead and copies in checkpoints. More active windows or a longer replay-deduplication horizon can increase the real footprint.
Save one cut through the dataflow#
A checkpoint ties durable operator state to source positions. In the single-partition example, checkpoint C contains sum 150 and next offset 42. The 70-cent sale is at 42. Assume the input remains available and the operator makes a deterministic update. After the crash, restore C and read 42. The restored state reaches 220 and the next position becomes 43. The lost in-memory update is not part of the restored checkpoint. Apache Flink 2.1, State Persistence
Restore the total and the position together
With several input channels, a snapshot needs to respect the dependency between a sent record and its receipt. A consistent cut cannot include a receive while omitting the corresponding send. Distributed snapshot algorithms account for process state and messages in flight. In Flink’s aligned checkpoint approach, source barriers mark a cut. An operator with multiple inputs can pause an input that already delivered the barrier while it waits for barriers on the others. It snapshots after processing the pre-barrier input from each. Unaligned checkpoints can capture in-flight buffers instead of waiting for that alignment. Chandy and Lamport, 1985, §2 Carbone et al., 2015, §3 Apache Flink 2.1, Checkpointing
Count the cost of replay and snapshots#
In a simple case with completed checkpoints every 30 seconds, a crash just before the next checkpoint may require redoing almost 30 seconds of input. At a constant 1000 records per second, that is close to 30,000 records. If checkpoints fail or take longer to complete, the latest usable checkpoint can be older. More frequent checkpoints can reduce replay work but also increase checkpoint traffic and overhead. The bound concerns the assumed schedule, not a universal limit on recovery time.
An internally consistent checkpoint explains why the restored operator state contains each applied record’s contribution once. A database or file that already received output may have kept it independently of that checkpoint. The external effect needs a separate argument.
Next#
The internal total can be recovered without drift. End-to-end recovery adds a destination and follows the crash between its write and checkpoint completion.
References#
- Apache Software Foundation. Flink 2.1 Stateful Stream Processing. Describes keyed state, replay and aligned and unaligned checkpoints.
- K. Mani Chandy and Leslie Lamport. Distributed Snapshots: Determining Global States of Distributed Systems, ACM TOCS 3(1), 1985, §2. Defines process and channel state for a distributed snapshot.
- Paris Carbone et al. Lightweight Asynchronous Snapshots for Distributed Dataflows, 2015, §3. Describes barrier-based snapshots in dataflow graphs.