Course contents 23 / 35

23 / 35 · Streams and State

From batch to streaming

Compare a result over a closed input with a running result that is emitted and revised while the input grows.

At the end of the day, the report reads a fixed set of sales and sums them. The shop now wants to see that same total while orders continue arriving. The report needs a rule for what it publishes before the input is complete and how a new event changes an earlier result.

Close the input or maintain a running result#

A bounded input has a defined end. A manifest of yesterday’s files is bounded even if it contains a billion rows. A batch job can read the entire manifest and publish its sum. A sales log that the system will keep appending to is unbounded from the job’s perspective. Waiting for its end would postpone the result indefinitely. The same log can supply a bounded input if we choose finite start and end offsets. Apache Flink 2.1, Overview

Suppose a store’s new daily total starts at zero and receives amounts 100, 50 and 70 cents at 10, 40 and 110 seconds after the processing clock starts. All three are distinct events for the same day. A batch over the closed set gives 100+50+70=220100+50+70=220. An incremental sum can apply each amount to the previous state. With one unit of update work per arrival in this toy model, it keeps one total. Recomputing at every release instead scans all events visible at that release. Akidau et al., 2015, §§1–2

Choose when to release the result#

An engine can update state per event and emit promptly, or process arrivals in microbatches. A microbatch chooses a bounded prefix of new input at each trigger, applies it to saved state and produces updates. Spark Structured Streaming models a stream as an input table that keeps receiving rows. Its default execution uses microbatches. That scheduling choice does not establish the time window a sale belongs to. Apache Spark 4.0.1, Programming Model

In the toy report, assume releases at 60 and 120 seconds and zero computation time. At 60 seconds two events are visible, so the report gives 150. The 70-cent event arrives at 110 seconds and is included at 120 seconds. The second result is 220. The second release waits ten seconds from that arrival in this model. Real delay also includes scheduling, work already queued and destination time.

Figure

Two releases of the same daily total

Two releases of the same daily totalFirst release at 60 s · 100 + 50 = 150. Arrival at 110 s · +70. Release at 120 s · 220First release at 60 s100 + 50 = 150Arrival at 110 s+70Release at 120 s220
At 60 seconds the total accounts for two events. The third arrives later and is included at the next trigger.

A release may contain a replacement for the total under key (store, day) or a delta to add. Publishing absolute totals 150 and 220 to a consumer that sums them gives 370, even though the correct final amount is 220. Use a sink that replaces that key, or send only the changes of 150 and 70 with unique revision identities. The output contract must specify how revisions compose. Akidau et al., 2015, §2.3

When an early result can change#

Processing speed alone cannot guarantee the report is complete. A disconnected register can deliver an earlier sale after the latest release. A system can publish tentative results and revise them, or wait according to a stated completeness rule and tolerate longer delays. In both cases we need to distinguish the time of the sale from the time the processor receives it. Akidau et al., 2015, §2

Next#

The delayed sale belongs to the time of its purchase. Time, windows and late data assigns it to an event-time interval and shows when the old total can be revised.

References#

  1. Tyler Akidau et al. The Dataflow Model, PVLDB 8(12), 2015, §§1–2. Separates input boundedness, emission and refinement of results.
  2. Apache Software Foundation. Apache Flink 2.1 Overview. Discusses bounded and unbounded streams and stateful computation.
  3. Apache Software Foundation. Spark 4.0.1 Structured Streaming, Overview and Programming Model. Explains incremental queries and microbatch execution.