AlgoViz

Apache Flink

hard 28 min

Apache Flink runs programs that never end: they read events one at a time, remember things per key, and survive crashes by taking consistent snapshots while the data keeps moving.

See it run

Follow an event, watch a barrier cut the stream, crash a task, and see when a window is allowed to fire.

One event

From the topic to the result, with the state that stays behind.

Step 1 of 5

Submit

Flink job and storageJobManagercoordinatesKafkainput topicSource taskreadsWindow taskkeyed stateSink taskwritesObject storagecheckpoints

Tasks deployed

The client gives the job to the JobManager, which deploys tasks into slots.

Step 1: Submit

events = read(kafka)keyed = events.key_by(user)counts = keyed.window(minutes(1)).count()counts.write(sink)
Delay
Milliseconds, plus the window
Protects
Nothing yet
Micro-batch
waits for the batch

keyBy is the expensive step: it moves records between machines. Filter before it.

The same apache flink doing real jobs. Pick one and step through it.

One customer sends half of all events.

Step 1 of 4

Normal

Flink job and storageJobManagercoordinatesKafkainput topicSource taskreadsWindow taskkeyed stateSink taskwritesObject storagecheckpoints
Hot task
55%
Other tasks
50%

Every task is moderately busy.

Step 1: Normal

The bugs interviewers watch for, with the fix beside each.

  • Calling it exactly-once end to end

    critical

    Checkpoints protect Flink's state. A plain sink repeats its writes after a restart.

    Wrong
    sink.setDeliveryGuarantee(NONE)  # the default
    Right
    sink.setDeliveryGuarantee(EXACTLY_ONCE)# or upsert by a stable id
  • Forgetting to enable checkpoints

    critical

    The interval has no default. Without it there is nothing to recover from.

    Wrong
    env.execute()
    Right
    execution.checkpointing.interval: 60 s
  • State that never expires

    major

    Every key ever seen stays. Checkpoints grow until they time out.

    Wrong
    state.update(seen)
    Right
    descriptor.enableTimeToLive(ttl_7_days)
  • No uid on operators

    major

    After a code change the savepoint cannot be matched to the operators.

    Wrong
    stream.process(fn)
    Right
    stream.process(fn).uid("count-per-user")
  • How to explain it

    • Records flow, each key has one owner with state.
    • Barriers cut the stream for a snapshot.
    • After a crash, state and position go back together.
  • Memory trick

    • The marker on the belt.
    • Before the marker: in the notebook.
    • After the marker: ride again.
  • Reach for it when

    • You need memory per key and a fast answer.
    • Events are late or out of order.
    • Counts must survive failures.
  • Be careful when

    • The job is already at its limit.
    • One key dominates.
    • Nobody will operate the cluster.