Sizing the Driver

The driver runs your Python program, plans each query, and schedules every task, while the executors do the reading and the computing. Planning and scheduling take little memory. What fills a driver's heap is data the job sends back to it, so you size a driver from the output of a job, and the size of its input barely enters into it.

Data that travels back to the driver

What the job doesWhat lands on the driver
`collect()` or `toPandas()`Every row of the result
`take(10)` or `first()`The rows asked for, and no more
`count()`One number from each task, added up
A broadcast joinThe whole small side, gathered before it goes out to the executors
Every finished taskA status report with the task's metrics

A broadcast join collects the whole small table on the driver before sending a copy to every executor, so broadcasting a 2GB table makes the driver hold all 2GB. Status reports are small one at a time, but a stage with 100,000 tasks sends 100,000 of them.

Watching the result shrink

The animation below rolls 12 view rows up into minutes per title and collects the result. Count the rows at each step: the groupBy shuffles them by title across 3 tasks, and the driver receives 1 row per title, 4 in all.

from pyspark.sql import functions as F

per_title = views.groupBy("title").agg(F.sum("minutes").alias("total_minutes"))
per_title.collect()
views
view_idtitleminutes
1Ocean Quest42
2Night Shift15
3Cold Harbor60
4Ocean Quest30
5Paper Moon25
6Night Shift48
7Cold Harbor12
8Ocean Quest55
9Paper Moon40
10Night Shift20
11Cold Harbor33
12Paper Moon18

Every reduction placed before collect() runs on the executors and shrinks what crosses to the driver. Put the same collect() ahead of the groupBy and all 12 rows arrive; at production scale that is the gap between a few kilobytes and many gigabytes.

Run the cell to compare the 2 results you could collect from order_items. The first column counts the rows a raw collect() would bring back; the second counts the rows the driver receives after a groupBy on product_id.

Those 2 counts are the 2 sizes a collect() could bring back from the same table. Which one the driver gets depends on where the action sits in the chain: items.collect() sends every line item, while items.groupBy("product_id").agg(F.sum("quantity")).collect() runs the grouping on the executors first and sends 1 row per product. Everything chained before the action runs on the executors, and only the action's result crosses to the driver.

Driver limits that both start at 1GB

spark.driver.memory sets the driver's heap and defaults to 1g. spark.driver.maxResultSize caps the total serialized size of the results 1 action pulls from all partitions, and it also defaults to 1g. When a collect() goes over the cap, Spark aborts the job with an error that names the setting and the size reached, before the driver's heap overflows. Setting the cap to 0 removes it, which trades that clear error for a driver that can run out of memory.

Estimating the result before you collect

Multiply rows by bytes per row. The nightly rollup's day holds about 1 billion view events, and 3 columns of a view come to roughly 60 bytes once serialized:

What you collectRowsSerialized sizeWithin the 1g defaults?
Raw views, 3 columns1,000,000,00060GBNo: far over `maxResultSize`
Minutes per title per hour480,00028.8MBYes
Minutes per title20,0001.2MBYes

The serialized size is what maxResultSize measures. Once the rows land, the driver turns them into objects that take several times that space, and a Python program then copies them into Python rows in its own process. Leave room for both when you size the driver, set maxResultSize a little above the largest result you mean to collect, and keep both at 1g when the largest thing coming back is a summary.

spark.driver.cores defaults to 1. The driver schedules tasks and runs your own code between actions, so extra driver cores pay off only when that code does heavy work on collected data. A job that writes its output from the executors rarely needs more than 1 or 2.

Do
  • Aggregate or filter on the executors before any collect()
  • Write large results to storage from the executors with df.write
  • Raise driver memory and maxResultSize together, sized from the result
  • Use take(n) to inspect a few rows instead of collecting the whole result
Avoid
  • Set maxResultSize to 0 to make the error go away
  • Give the driver 64g so that a billion-row collect() survives
  • Size the driver from the input; the output is what reaches it
  • Broadcast a table of several GB, which the driver gathers first

A notebook calls toPandas() on a 2.5GB result and fails with an error naming spark.driver.maxResultSize. Which plan fixes it?

About this lesson

This section is part of the Cluster Sizing: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.