AlgoViz

Dataflow engines

medium 35 min

A 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

ReadFilterGroupSum

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

ReadJoinCount

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

    critical

    All rows go to one process, which runs out of memory.

    Wrong
    rows = df.collect()save(rows)
    Right
    df.write.parquet(path)df.show(20)
  • Using a dataset twice without a cache

    major

    Each action runs the chain from the start.

    Wrong
    df.count()df.write.parquet(path)
    Right
    df.cache()df.count()df.write.parquet(path)df.unpersist()
  • Grouping before summing

    major

    Every row crosses the network, and all values of a key must be held.

    Wrong
    rdd.groupByKey().mapValues(sum)
    Right
    rdd.reduceByKey(add)
  • Appending on a rerun

    critical

    A job that failed half way and ran again counts rows twice.

    Wrong
    df.write.mode("append")
    Right
    df.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.