Skip to main content

Spark UI & Job Debugging Techniques โ€” Monitor and Optimize PySpark Jobs

At NeoMart, monitoring PySpark jobs is crucial:

  • Some jobs run slower than expected
  • Some tasks fail silently or consume too much memory
  • Optimizing joins, aggregations, and shuffles requires visibility

The Spark UI is the most powerful tool for analyzing job execution, stages, tasks, and memory usage.


1. Accessing Spark UIโ€‹

  • If running locally: http://localhost:4040
  • On cluster: Access through Spark history server or Databricks UI
  • Tabs to focus on:
    • Jobs โ†’ high-level view of actions
    • Stages โ†’ detailed breakdown of tasks
    • Storage โ†’ cached DataFrames/RDDs
    • SQL โ†’ executed SQL queries and plans
    • Environment โ†’ configuration and JVM details

2. Understanding Jobs and Stagesโ€‹

  • Job: Triggered by an action (e.g., count(), show())
  • Stage: Set of tasks that can run in parallel without shuffle
  • Task: Execution unit processing a partition

Example:

df_filtered = df.filter(F.col("price") > 100)
df_filtered.count() # triggers a job
  • One action โ†’ One job
  • Spark UI โ†’ see stages, number of tasks, time, and shuffle info

3. DAG Visualizationโ€‹

  • Spark builds Directed Acyclic Graph (DAG) for transformations

  • Narrow vs wide transformations show differently:

    • Narrow: straight line โ†’ no shuffle
    • Wide: nodes merge โ†’ shuffle happens

Story: NeoMart analysts visualize DAG to spot shuffle-heavy operations slowing jobs.


4. Storage Tab โ€” Caching and Persistingโ€‹

  • Shows all cached DataFrames and RDDs

  • Displays:

    • Storage level (memory/disk)
    • Number of cached partitions
    • Memory usage
df.cache()
df.count()
  • Check Storage tab โ†’ confirm DataFrame cached

5. SQL Tab โ€” Query Monitoringโ€‹

  • For DataFrames registered as temp views or tables, Spark SQL tab shows:

    • Executed queries
    • Physical plan
    • Execution time
df.createOrReplaceTempView("products")
spark.sql("SELECT AVG(price) FROM products").show()
  • UI shows aggregation stage and tasks executed

6. Debugging Common Issuesโ€‹

  1. Long-running tasks:

    • Often due to data skew โ†’ consider salting or repartitioning
  2. High shuffle write/read:

    • Use broadcast joins for small tables
  3. Executor OOM (Out of Memory):

    • Persist intermediate results to disk
    • Increase executor memory
  4. Stragglers:

    • Skewed keys โ†’ repartition or salt

7. Using Spark History Serverโ€‹

  • Tracks completed jobs for offline analysis

  • Steps:

    1. Enable event logging:

      spark.conf.set("spark.eventLog.enabled", "true")
      spark.conf.set("spark.eventLog.dir", "/tmp/spark-events")
    2. Open history server โ†’ view past jobs, DAGs, stages, tasks

Story: NeoMart can analyze nightly ETL jobs and spot inefficient transformations even after job completion.


8. Tips for Effective Job Debuggingโ€‹

โœ” Use Spark UI DAG to identify wide transformations
โœ” Monitor shuffle read/write bytes โ†’ optimize joins and aggregations
โœ” Cache frequently reused DataFrames
โœ” Check task distribution โ†’ prevent stragglers
โœ” Use SQL tab for complex query optimization


Summaryโ€‹

Using Spark UI and history server, you can:

  • Monitor jobs, stages, and tasks
  • Visualize DAGs for performance insight
  • Debug skew, shuffle, and memory issues
  • Optimize iterative and large-scale pipelines

NeoMart engineers rely on Spark UI to save hours of troubleshooting and make PySpark pipelines production-ready.


Next Topic โ†’ Catalyst Optimizer & Tungsten Execution Engine