Spark execution model
Jobs → stages → tasks, narrow vs wide transformations, why shuffles create stage boundaries, AQE. The Spark UI is the interview whiteboard.
The mental model
Spark is a **planner that turns your query into an assembly line and stops the line every time work has to cross between workstations**. You write what looks like a single program — read.filter.groupBy.count — but Spark doesn't run it line by line. It builds a lazy plan, waits until you force a result (an *action*), then breaks the plan into stages at exactly the points where data must move between machines. Those movement points are called shuffles, and understanding Spark performance is mostly understanding where the shuffles are and how big they are.
The analogy that sticks: a factory. Your DataFrame operations are stations on an assembly line. **Narrow transformations** (filter, map, select) are stations where each worker finishes a part using only what's on their own bench — no part ever leaves the bench, so the whole run of narrow stations is one uninterrupted **stage**. **Wide transformations** (groupBy, join, distinct, repartition) are stations where a worker needs parts from *every other worker* — every widget for "customer 5" must end up at one station to be summed. That regrouping means physically shipping parts across the factory floor: the shuffle. You cannot start the sorting station until every upstream worker has shipped their parts, so a shuffle is a hard wall — a stage boundary.
Why this matters more than any other Spark concept: a shuffle writes intermediate data to disk on the sending side, transfers it over the network, and re-reads it on the receiving side. It is the single most expensive thing Spark does, and it's the thing your code causes without naming it. "The job is slow" almost always decodes to "there is a shuffle that is bigger, more skewed, or more numerous than it needs to be." The Spark UI is where you see the stages, the shuffle sizes, and the one straggler task holding everything up — which is why interviewers treat the Spark UI as the whiteboard.
The one idea that explains most of it: jobs → stages → tasks, split by shuffles. An *action* triggers a job; the job is cut into stages at every shuffle; each stage runs as tasks, one per partition, in parallel across executor cores. Get that hierarchy and you can read any Spark job's behaviour.
- Deep dive
- Internal architecture
- Real-world use cases
- Best practices
- Common mistakes
- Performance
- Security
- Production troubleshooting
- Advantages & disadvantages
- Alternatives
- Interview expectations
- Interview questions
- Facts to remember
- Cheat sheet
- Quick revision