Apache Flink
hard 28 minApache 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.
Operations
One event
From the topic to the result, with the state that stays behind.
Step 1 of 5
Submit
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.
In the real world
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
- Hot task
- 55%
- Other tasks
- 50%
Every task is moderately busy.
Step 1: Normal
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Calling it exactly-once end to end
criticalCheckpoints protect Flink's state. A plain sink repeats its writes after a restart.
Wrongsink.setDeliveryGuarantee(NONE) # the defaultRightsink.setDeliveryGuarantee(EXACTLY_ONCE)# or upsert by a stable idForgetting to enable checkpoints
criticalThe interval has no default. Without it there is nothing to recover from.
Wrongenv.execute()Rightexecution.checkpointing.interval: 60 sState that never expires
majorEvery key ever seen stays. Checkpoints grow until they time out.
Wrongstate.update(seen)Rightdescriptor.enableTimeToLive(ttl_7_days)No uid on operators
majorAfter a code change the savepoint cannot be matched to the operators.
Wrongstream.process(fn)Rightstream.process(fn).uid("count-per-user")
Interview tips
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.