Skip to the page

Spark · senior

Caching, memory, plans and slow jobs

When a job is slow or killed, read the evidence: the plan, the stage that grew, the memory error type. Cache only reused data, avoid Python UDFs and fix the cause, not the symptom.

The notes

What you must know

  • cache() helps when the same DataFrame feeds two or more actions, is expensive to recompute and fits in memory. It hurts when data is used once, cached before a filter, or takes memory the joins need. persist() lets you choose the level; unpersist() when done. cache is lazy: it fills on the first action.
  • Checkpointing writes to reliable storage and cuts the lineage. Often better: restructure into one pass or write the intermediate table.
  • Reading a physical plan: PartitionFilters: [] means every partition is read. A filter on a cast of a timestamp instead of the partition column stops pruning and pushdown. Filter on the partition column itself. isFinalPlan=false is the plan before running.
  • Executors killed by YARN for exceeding physical memory is a different problem from java.lang.OutOfMemoryError: Java heap space. If the heap peaked at 9 of 16 GB, the extra memory is outside the heap, for example Python workers.
  • applyInPandas loads each group into one pandas DataFrame, so one huge group can blow memory. Raise spark.executor.memoryOverhead (or spark.executor.pyspark.memory), use fewer cores per executor, and avoid giant groups with built-ins, windows or salting.
  • Python UDFs move every row from the JVM to a Python worker and are a black box to the optimizer. Use built-in functions. The DataFrame API and SQL use the same optimizer, so Python itself is not the problem. Pandas UDFs are for logic that needs Python libraries.
  • UDF bugs to catch: a NULL input crashing the task, trim on every column turning numbers into strings, withColumn in a loop building a huge plan (use one select or withColumns), collect() pulling rows to the driver and computing the pipeline twice.
  • Slow after months with no code change: compare a good run and a bad run in the History Server (stage, input, shuffle, spill, plan). A dimension that grew past the broadcast threshold turns a broadcast join into a sort-merge join with huge shuffles. Prune columns to get a broadcast again, and guard hints with a size check.
  • Prevent: alert on job runtime against history, track dimension sizes, review plans after big data changes. Raising the broadcast threshold for the whole cluster is not a fix.

The red pen

Common traps: mistake → fix

  • Mistake: Cache every DataFrame to be faster

    Fix: it uses memory and can slow joins; cache reused data only.

  • Mistake: Double the executor memory fixes a killed executor

    Fix: it may be outside the heap.

  • Mistake: PySpark is slower than Scala

    Fix: the slowness is Python UDFs, not the API.

Easy to hard

Questions interviewers ask, easy to hard

  1. When does cache help and when does it hurt?
  2. Executors are killed for exceeding memory limits. What do you check before doubling memory?
  3. Read this physical plan: what is Spark doing wrong?
  4. A job with many Python UDFs takes 2 hours. How do you speed it up?
  5. A 20-minute job now takes 3 hours with no code change. How do you find out why?

Practise

Practise with these questions

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview