Q&A-1Explain how a distributed Spark job is executed.
Answer
Assumptions: Spark 3.5, sort-based shuffle, Adaptive Query Execution on (default since 3.2), YARN or Kubernetes, input on HDFS/S3. Numbers below are the defaults unless stated.
The 90-second answer
A Spark job is one driver process plus a pool of executors. Transformations (map, filter, join) only build a lazy logical plan: nothing runs. An action (count, collect, save) hands that plan to Catalyst, which produces a physical plan; DAGScheduler then cuts the plan into stages at every wide dependency, i.e. every shuffle. Inside a stage, narrow operators pipeline in a single pass over each partition, so no intermediate result is materialized.
Each stage becomes one task per partition, launched by TaskScheduler onto executor slots. A task reads its input split (an HDFS block, or shuffle blocks from the previous stage), computes, spills to local disk when it outgrows its memory share, and writes shuffle files or final output. The driver holds the DAG, stage/task state and shuffle metadata, never the data, which is why collect() on a large result OOMs the driver rather than the executors.
Trace: what actually gets scheduled
rdd = sc.textFile("hdfs:///logs/*.gz", 2000) # 2000 partitions
counts = (rdd
.flatMap(lambda l: l.split())
.map(lambda w: (w, 1))
.reduceByKey(lambda a, b: a + b, 200) # wide dependency
.filter(lambda kv: kv[1] > 100)
.count()) # action| Stage | Tasks | Work per task | Boundary |
|---|---|---|---|
| 0 | 2000 | read one HDFS block, flatMap, map, write 200 shuffle buckets | shuffle write |
| 1 | 200 | fetch its bucket from all 2000 map outputs, reduceByKey, filter, emit partial count | shuffle read |
| — | — | driver sums 200 partial counts | action result |
flatMap and map never get their own stage: they pipeline inside stage 0. reduceByKey is the only shuffle boundary, so it is the only stage cut. Shuffle block count is 2000 × 200 = 400,000 files, which is why shuffle-heavy jobs are I/O-bound on small files, not CPU-bound.
Trade-offs that decide the plan
repartition(n)vscoalesce(n).repartitiondoes a full shuffle to redistribute evenly;coalescemerges adjacent partitions with no shuffle. Usecoalesceto shrink before a write,repartitionwhen you need even key distribution or more parallelism.- Broadcast vs sort-merge join.
spark.sql.autoBroadcastJoinThresholddefaults to 10 MB; below it the build side is shipped to every executor and the join needs no shuffle. Above it, sort-merge shuffles both sides: more network, but it scales. spark.sql.shuffle.partitionsdefaults to 200. Correct for small data, wrong for a 1 TB shuffle: 200 partitions means ~5 GB per reduce task. AQE coalesces partitions down towardspark.sql.adaptive.advisoryPartitionSizeInBytes(64 MB) at runtime, so the static 200 is a starting point, not the final count.
Failure modes and how you see them
- Skew. One reduce partition holds most keys; that task runs 10× longer while the rest idle. Visible in the Spark UI stage page as max task duration far above the median. AQE skew-join handling (
spark.sql.adaptive.skewJoin.enabled, default true) splits oversized partitions; salting the key is the manual fallback. - Executor OOM. A task's working set exceeds its memory share (
spark.memory.fractiondefaults to 0.6 of heap). Spill metrics in the UI climb before the kill: spilling is the warning, not the failure. - Shuffle fetch failure. An executor dies and its map outputs are gone, so downstream tasks report
FetchFailedand Spark re-runs the map stage.spark.stage.maxConsecutiveAttempts(default 4) bounds the retries before the job fails. - Task retries.
spark.task.maxFailuresdefaults to 4 attempts per task; lineage recomputes only the lost partition.
What would change this answer: a pure DataFrame/SQL job lets AQE rewrite the physical plan mid-execution, and a Structured Streaming job repeats the whole stage graph once per micro-batch.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.