Skip to content
tezvyn:
Menu

Spark transformations versus actions

Source: interviewEasyHow cards are made

Summary

Spark's lazy execution model.

Key points

transformations like map and filter are lazy and build a lineage DAG returning a new RDD; actions like count or collect trigger execution and return a value to the driver.

What's really being asked

This confirms you understand Spark's defining feature, lazy evaluation, and why deferring work until an action enables whole-job optimization.

The full answer

A transformation creates a new dataset from an existing one and is lazy: calling it does not run any computation, it only records the operation in Spark's lineage graph, the directed acyclic graph of dependencies. Examples include map, filter, flatMap, groupByKey, and join, each returning a new RDD or DataFrame. An action triggers actual execution of the accumulated transformations and either returns a value to the driver program or writes data out. Examples include count, collect, take, first, reduce, and saveAsTextFile. The distinction is fundamental because laziness lets Spark see the entire chain of transformations before executing, so the Catalyst optimizer and the DAG scheduler can pipeline narrow transformations into stages, push down filters, and avoid materializing intermediate results, all of which improve performance and reduce data movement.

The mistakes people make

Believing a map or filter computes results immediately. Calling collect a transformation. Thinking lineage is only for fault tolerance and not optimization. Assuming every line of Spark code launches a job; only actions do.

What usually comes next

What is the difference between a narrow and a wide transformation. How does lineage provide fault tolerance. Why can calling collect on a huge dataset crash the driver.

A concrete example

Given an RDD of log lines, calling filter to keep error lines and then map to extract timestamps does nothing observable; Spark just extends the DAG. Only when you call count does Spark launch a job, read the data, apply the filter and map in a pipelined stage, and return the number of error lines. If you never call an action, the data is never read at all, which surprises engineers who expect eager execution.

Interview question

Why does Spark's distinction between transformations and actions improve performance?

  • a.Transformations always avoid network shuffles
  • b.Actions cache all intermediate results automatically
  • c.Laziness lets Spark see the full DAG and optimize before any executionCorrect
  • d.Transformations run on the driver, freeing executors for actions
Why?

Because transformations are lazy, Spark can analyze the entire chain and optimize it, for example pipelining stages and pushing down filters, before an action triggers execution. Transformations run on executors, and not all avoid shuffles.

Just read this? Test yourself on what you have been reading.

Read the original → spark.apache.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles