Dataflow engines
medium 35 minA dataflow engine runs a whole program as one graph of operators.
See it run
From a lazy plan to stages and tasks, and what happens when a machine is lost.
Operations
From code to result
Six moments of one query: the graph, the optimiser, stages, tasks, shuffle and the final sum.
Step 1 of 6
Build the graph
Nothing has run
Read, filter, group and sum are only described.
Step 1: Build the graph
rows = read("orders") # lazyrecent = rows.filter(day >= "2024-01-01")totals = recent.group_by("cat").sum("amount")result = totals.collect() # action: now it runs- Moves
- One shuffle
- Runs when
- At the action
Nothing runs in the first three lines. The last line starts everything.
In the real world
The same dataflow engines doing real jobs. Pick one and step through it.
Without a cache, every action starts from the beginning.
Step 1 of 4
First action
70 s
Count runs the whole chain: 60 seconds to prepare and 10 to count.
Step 1: First action
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Collecting everything
criticalAll rows go to one process, which runs out of memory.
Wrongrows = df.collect()save(rows)Rightdf.write.parquet(path)df.show(20)Using a dataset twice without a cache
majorEach action runs the chain from the start.
Wrongdf.count()df.write.parquet(path)Rightdf.cache()df.count()df.write.parquet(path)df.unpersist()Grouping before summing
majorEvery row crosses the network, and all values of a key must be held.
Wrongrdd.groupByKey().mapValues(sum)Rightrdd.reduceByKey(add)Appending on a rerun
criticalA job that failed half way and ran again counts rows twice.
Wrongdf.write.mode("append")Rightdf.write.mode("overwrite") # only this date's folder
Interview tips
How to explain it
- Describe first, run at the action.
- Stages end at shuffles.
- Lost partitions are made again.
Memory trick: LTASE
- Lazy, Triggered by actions.
- Arbitrary graph, Stages at shuffles.
- Execute in parallel.
Make it faster
- Filter and select early.
- Broadcast the small table.
- Cache what is used twice.
Watch
- Shuffle size per stage.
- The longest task against the median.
- Spill to disk.