Lesson 6 of 9 Data Engineering Practices

Understanding How Distributed Engines Divide and Move Data

This lesson explains how Spark, Flink, Redshift and Athena spread work across many machines, which explains most performance problems and their fixes.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how drivers, executors and partitions divide work.
  • Reduce shuffles with broadcast joins and partitioning.
  • Decide whether to scale up or scale out.

How distributed engines work

Engines such as Spark process data too large for one computer by splitting it into partitions, which executors on many nodes process in parallel. A driver process plans the job, divides it into tasks and coordinates the executors. Operations that need related rows together, such as joins, groupBy and sorts, cause a shuffle, moving data across the network between nodes; shuffles are often the slowest and most expensive part of a job.

When one key has far more rows than the others, the task that handles it runs much longer than the rest, and the whole job waits for this straggler; this is data skew. Engines are fault tolerant because they record how each partition was computed, its lineage, so a failed task is simply re-run. The following table summarises the concepts.

ConceptMeaning
PartitionsChunks of data processed in parallel by executors on many nodes
DriverThe process that plans the job and coordinates executors
ShuffleMoving data across the network for joins, groupBy and sorts; expensive
Data skewOne key has far more rows, creating straggler tasks
Fault toleranceFailed tasks are re-run from their lineage

The following diagram shows the driver, the executors and a shuffle between two stages.

How a distributed engine runs a job. The driver plans the job and sends tasks to executors, each of which processes its own partitions. A groupBy or join needs all rows with the same key together, so a shuffle moves rows across the network between executors, which is often the slowest part of the job.
Keys and executor counts are illustrative.
Match the pairs

Distributed engine concepts

Exam skills 1.4.10

Match each concept with its meaning.

Log in to try this activity Interactive activities are free with an account.

Reducing shuffles

A shuffle moves data between nodes so that related rows end up together, for example all rows with the same key for a join or aggregation. Shuffles involve writing data to disk, sending it across the network and reading it again, so they are usually the most expensive part of a distributed job, and reducing them is the main way to speed jobs up.

When one side of a join is small, such as a lookup table, a broadcast join sends a copy of it to every node, so the large table never moves. Filtering and aggregating before joining reduces the volume that has to be shuffled. When the same large join runs repeatedly, storing both tables partitioned or bucketed by the join key lets the engine skip the shuffle. Adaptive query execution in Spark adjusts the number of partitions and the join strategy at run time based on the actual data sizes, which fixes many shuffle problems automatically.

The following diagram compares a shuffle join with a broadcast join of a large table and a small table.

A shuffle join compared with a broadcast join. Joining the 2 TB orders table with a 50 MB categories table by shuffle moves both tables across the network so that matching keys meet. Broadcasting the small table sends a copy to every executor, so each joins its own partitions of orders locally and the 2 TB table never moves.
Broadcasting suits only a table small enough to copy to every executor.
Select all that apply

Reducing shuffles

Exam skills 1.4.10 · 1.4.1

Identify the techniques that reduce shuffles.

Log in to try this activity Interactive activities are free with an account.

Scaling up or out

Adding more machines, scaling out, speeds up a job only when its work can be divided among them. Some work cannot be divided. A single large file in a format that cannot be split, such as a gzip-compressed CSV, is read by one task. A single very frequent key, a hot key, sends most of the data to one task in a join or aggregation. Work done on the driver runs on one machine regardless of cluster size. In each case, adding executors adds cost but not speed, and the data layout or the code must change instead, for example by using splittable formats or by salting hot keys.

Conversely, work that is well partitioned scales almost linearly: twice the workers finish in about half the time for about the same total cost. That property makes serverless engines such as AWS Glue and EMR Serverless cost-effective for well-designed jobs.

Sort into groups

Will more workers help?

Exam skills 1.4.10

Decide whether adding executors speeds up each job.

Log in to try this activity Interactive activities are free with an account.
Worked example

Avoiding a shuffle with a broadcast join

A Glue job joins a 2 TB orders table with a 50 MB table of product categories. The Spark UI shows most of the run spent shuffling both tables across the network so that matching keys meet on the same executor. The fix broadcasts the small table to every executor, as follows.

from pyspark.sql.functions import broadcast

enriched = orders.join(broadcast(categories), "category_id")

Each executor now joins its own partitions of orders with a local copy of the categories, and the 2 TB table is never moved. Adaptive query execution can make the same choice automatically when its statistics show the table is small enough.

Check yourself

A few tasks take much longer than all the others. What is the likely cause?

Show answer

Data skew: some keys hold far more data. Salting or repartitioning spreads it out.

Exam Tips

Remember for the exam

  • Minimise shuffles, caused by joins, groupBy and sorts, for better performance.
  • A small lookup table in a Spark join calls for a broadcast join.
  • A few tasks taking much longer than the rest indicate data skew; use salting or repartitioning.
  • Adaptive query execution adjusts joins and partitions at run time.
  • More executors do not help when work is concentrated on one key, file or the driver.