The cluster was fine. One small line crashed the job. Here is why, and the habits that stop it.
The job ran fine for weeks. Then one morning it failed in under a minute.
The error said the driver ran out of memory. Nobody had changed the cluster. Nobody had added more data than usual.
So what happened? Someone added one small line to the notebook. It was df.collect().
This story is very common. And once you understand how the driver works, it becomes very easy to prevent.
Think of a Spark cluster like a small kitchen.
The driver is the head chef. It reads the recipe, plans the work, and tells everyone what to do. The executors are the cooks. They do the chopping and cooking.
The head chef has a small table. The cooks have big kitchens.
When you ask Spark to bring all the results back to the driver, you are asking every cook to put every plate on the head chef's small table. At some point the table is full. That is a driver out of memory error.
flowchart LR
E1[Executor 1] -->|rows| D[Driver: small memory]
E2[Executor 2] -->|rows| D
E3[Executor 3] -->|rows| D
D --> X[Out of memory]collect() pulls every row into the driver. It is fine for 100 rows. It is dangerous for 100 million rows.
The same is true for toPandas(). It also moves the whole DataFrame to the driver.
When Spark broadcasts a table, the driver first builds that table in its own memory. Then it sends it out to the executors.
If the "small" table is actually large, the driver fills up before any cook gets the copy.
The driver keeps track of every file a query reads. A table with two million tiny files means two million entries the driver has to plan around.
The query may fail before a single row is read.
A for loop over rows in plain Python runs on the driver, not on the cluster. It is slow, and it keeps everything in one place.
Long chains of withColumn inside a loop, or hundreds of unions, can make the query plan itself very large. The driver has to hold the plan in memory.
The signs are usually clear once you know them.
| What you see | What it usually means |
|---|---|
| Fails quickly, error mentions driver | Too much data pulled to the driver |
| "The spark driver has stopped unexpectedly" | Driver memory or a crash on the driver |
| Spark UI shows no long running tasks | The work never reached the executors |
| Fails right after a broadcast | The broadcast table is too big |
| Long pause before the first stage starts | Many files or a very large plan |
The key idea: if executors are calm and the job still fails, look at the driver first.
Each habit below replaces a risky pattern with a safe one.
Look at a sample, not everything. Use display(df) or df.limit(20) when you want to check data.
Write results to a table. If you need the full result, save it as a Delta table. Do not bring it home to the driver.
Aggregate before you collect. If you really need values on the driver, reduce them first. A count or a small summary is safe.
Let Spark choose joins. Do not force a broadcast hint unless you know the table is small.
Keep files healthy. Run OPTIMIZE on tables with many small files, or use liquid clustering.
Replace Python loops with DataFrame operations. Let the cluster do the work.
This lab runs on Databricks Free Edition. It shows the safe and risky patterns side by side, using a small table so nothing actually crashes.
First, build a sample table.
from pyspark.sql import functions as F
orders_df = (
spark.range(0, 2_000_000)
.withColumn("customer_id", (F.col("id") % 5000).cast("int"))
.withColumn("amount", (F.rand() * 100).cast("double"))
)
orders_df.write.mode("overwrite").saveAsTable("workspace.default.driver_lab_orders")Now compare two ways to get the top customers.
The risky way pulls two million rows to the driver, then sorts in Python.
# Risky: every row travels to the driver
all_rows = spark.table("workspace.default.driver_lab_orders").collect()
print(len(all_rows))The safe way lets the cluster do the work and returns only ten rows.
orders = spark.table("workspace.default.driver_lab_orders")
top_customers_df = (
orders
.groupBy("customer_id")
.agg(F.sum("amount").alias("total_amount"))
.orderBy(F.col("total_amount").desc())
.limit(10)
)
display(top_customers_df)The same idea in SQL:
SELECT
customer_id,
SUM(amount) AS total_amount
FROM workspace.default.driver_lab_orders
GROUP BY customer_id
ORDER BY total_amount DESC
LIMIT 10Watch the time each one takes. The safe version is faster, and it stays safe even when the table grows a thousand times larger.
When you are done, clean up.
DROP TABLE IF EXISTS workspace.default.driver_lab_ordersDriver crashes are often the first serious production bug a new data engineer meets. They feel scary because the error appears so fast.
But the fix is rarely a bigger cluster. The fix is a better habit. Keep the heavy work on the executors, and send only small answers back to the driver.
If you remember one thing, remember the kitchen. The cooks do the work. The head chef only needs the summary.
Happy holidays, and happy building.
Team BricksNotes