KatInterview

Spark Internals and Performance Tuning for Lake Pipelines

System DesignModel answer
HardModel answerSystem Designsparkpysparkdata-engineering

Last updated

Explain how Spark defers and runs work across a cluster, then show how that execution model guides a measured approach to tuning slow batch and streaming jobs on Parquet/Delta data.

You own several production pipelines built with PySpark (Spark 3.x, Adaptive Query Execution available). They read multi-terabyte Parquet/Delta tables on object storage, partitioned by date, and write curated tables for downstream consumers. Some pipelines are batch; others are Structured Streaming jobs with state. Clusters have tens to hundreds of executor cores, and the data mixes huge fact tables with small dimension tables.

Answer the following as you would in a technical discussion. The execution model in Parts 1 and 2 should be the reasoning behind your tuning choices in Part 3. The goal is lower wall-clock time and lower executor-hours, not just a job that finishes.

Before tuning, state which clarifying questions you would ask. Examples are: batch or streaming, and what the freshness target is; the source file format, partitioning and file sizes; whether the pain is one slow stage, the whole job, or intermittent OOM/spill; cluster shape and dynamic allocation; whether the job is read-, join- or write-heavy; and whether skewed keys are known.

Part 1: Laziness. What does it mean that Spark is lazy? Separate transformations from actions, explain why the engine is designed this way, and give a concrete optimization that laziness makes possible.

Part 2: Distributed execution. How does Spark spread work over a cluster? Describe the roles of the driver, cluster manager and executors, the job/stage/task hierarchy, how partitions relate to parallelism, and why some operations (shuffles) cost far more than others.

Part 3: Optimization. How would you speed up or cut the cost of a slow Spark job? Start with a methodology, then cover the main levers: execution plans, shuffles, joins, partitioning, caching, file layout, skew, Python/UDF overhead and streaming.

Hints

Hint 1

Tie everything to one idea: Spark builds a whole plan lazily, and the dominant cost is moving data between executors (shuffles). Plan building, stage boundaries and tuning all follow from that.

Hint 2

For tuning, begin with evidence: find the slow stage in the Spark UI (task time spread, shuffle read/write, spill), then read the physical plan with explain. Only then pick a lever.

Hint 3

For skew, distinguish symptoms (a few tasks far longer than the median) from fixes (AQE skew join, salting, isolating hot keys, broadcasting the small side). For streaming, think about bounded state, watermarks, triggers, and input rate versus processing rate.