The streaming kitchen

A collection of Apache Flink® SQL recipes for pizza shops, and the science behind them

Each recipe answers a question about the business, and an animation shows how Flink SQL executes it, step by step: the events and watermarks flowing through the job graph, the state Flink is managing, and what the timers are doing.

How this works

The animations presented here are driven by a small, deterministic Flink simulator, written in JavaScript. The simulation is checked against Flink in two ways:

  • Each recipe's SQL is replayed on a local Flink mini-cluster, and every sink's rows, with their row kinds, are compared with the simulator's.
  • Each operator is run in Flink's test harness, and fed exactly what the simulator's operator received. After every step, the verifier checks what the operator emitted, the keys in state, every timer, and the stored values.

Before you start

Here's what these recipes assume you know about SQL, Apache Kafka®, and stream processing.

SQL
GROUP BY, joins, and window functions such as ROW_NUMBER() OVER (PARTITION BY … ORDER BY …), which deduplication relies on.
Kafka
Topics, keys, and partitions. For the upsert examples, how a compacted topic keeps the latest value for each key, and how a null value (a tombstone) deletes it.
Event time and watermarks
Why events arrive out of order, and how a watermark decides when a time-based result, such as a window, is complete.
Changelogs
A Flink SQL query over a stream produces a stream of changes, not a result set. Understanding why some results are append-only, and others are updated, is the key to most of these recipes.

Watch

Videos from Confluent Developer.

Read

From the Apache Flink® documentation.

If this is all new to you, start with filtering and projection, then deduplication, the tumbling window, and non-windowed aggregation. The later recipes build on these.

If you already know your way around Flink SQL, here are some highlights to explore:

How to read the diagrams

Every recipe is a Flink job with Apache Kafka® sources and sinks. In between are the operators chosen by the Flink SQL planner.

Stream types

Each chip lists the row kinds a stream can carry.

  • +I append-only: only inserts. Each record is a new fact.
  • +I +U −D upsert: inserts, updates and deletes, by key. An update replaces the row with the same key.
  • +I −U +U −D retract: every update is sent as a pair: retract the old row, then add the new one.
Row kinds
  • +I insert
  • −U retract the old version
  • +U the new version
  • −D delete
Watermarks
  • WM 12:00:04 travels with the data and says no record at or before this time should follow.

Operators that wait for time use watermarks to know when to act.

Operators
  • stateless handles each record on its own, and keeps nothing.
  • stateful, bounded keeps keyed state that stays bounded: freed by watermark, or a small set of keys.
  • stateful, needs a TTL keeps keyed state that grows without limit, unless you set a state TTL.
  • added by the planner isn't in your SQL, but the Flink SQL planner needs it to produce correct results.

Calc nodes that only drop columns, or set up a time attribute, are left out.

hash(key) under an edge means the records are repartitioned by that key, ensuring that every record for a key will reach the same parallel instance.

State

Each state panel's tag says what keeps its state from growing, or that nothing does.

  • freed by watermark timers clear this state as the watermark advances.
  • small set of keys one entry per key, and the keys, such as stores, grow very slowly, or not at all.
  • grows with keys one entry per key, and new keys, such as order IDs, keep arriving.
  • grows with rows an entry for every row, kept unless that row is retracted.

A panel with a torn lower edge holds state that grows without limit, unless you set a state TTL. Where a panel holds more than one kind, each row is marked with its own.

Each diagram has a Help button that shows this key beside the diagram, and hovering over a stream type, a watermark, a stateful operator, or a state tag explains it.

Summary

Every recipe at a glance: the state it keeps, what it uses watermarks for, the watermark it sends downstream, the stream it writes, and the operators the Flink SQL planner adds. Each row links back to its animation.