SpinTheMic
Pro

Apache Spark · Beginner

What are narrow and wide transformations in Spark?

By Anurag Pandey, Lead Data Engineer ·

The short answer

In a narrow transformation each output partition depends on one input partition, so no data moves between executors. A wide transformation needs data from many partitions, which causes a shuffle and starts a new stage.

Practice this out loud

Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.

Say it like this

About 50 seconds out loud. Use it as a model, then put it in your own words.

The difference is whether data has to move across the cluster. In a narrow transformation, each output partition is built from a single input partition. Filter, select and withColumn are narrow: every task works on its own partition, and Spark can chain several of them in one stage. In a wide transformation, an output partition needs rows from many input partitions. groupBy, distinct, most joins and orderBy are wide, because rows with the same key have to be brought together. That data movement is a shuffle: data is written out, sent over the network and read back, which makes it the most expensive part of most jobs. Each shuffle also creates a stage boundary, which is why I read the stages in the Spark UI to see where the shuffles are.

What interviewers look for

  • You define both in terms of partition dependencies.
  • You give correct examples of each.
  • You connect wide transformations to shuffles and stage boundaries.

Common mistakes

  • Calling every join wide. A broadcast join avoids shuffling the large table.
  • Listing examples without explaining why a shuffle is expensive.
  • Confusing narrow and wide with transformations and actions.

Follow-up questions to expect

  1. 1Is a join always a wide transformation?
  2. 2How can you see the stages of a job?
  3. 3How would you reduce the number of shuffles in a job?
Practice this out loud

Opens this question on SpinTheMic. Start without signing up; sign in free for an AI report card.

Checked against

All interview question guides