Interview's MateData Engineer
Interview's Mate

Data Engineer

SQL · Spark · Lakehouse · Streaming

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Data Engineer topics
Lesson 64/ 160
04. Big Data Processing & Apache Spark

Transformations vs Actions & Lazy Evaluation

Know which APIs build a plan (transformations) versus trigger computation (actions) under lazy evaluation.

BeginnerTransformations · Actions · Lazy3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Immutable DataFrame transformation graph. Next, catalyst prunes and combines expressions. Finally, sink task results become one query outcome.

Think of it like…

Transformations are notes on a shopping route; the route is planned lazily until an action says to leave the warehouse and acquire the goods.

Where you’ve seen it

clean = orders.filter(F.col('status')=='paid').select(...); clean.explain() creates no job; clean.write.mode('overwrite').parquet(out) is the action that scans and commits partitions.

Words you’ll hear

Transformations
Narrow or wide plan operators that return a new distributed dataset reference without launching computation.
Actions
An operation that requires a result, such as count, collect, show, or a write, and triggers execution.
Lazy
Deferring computation so the optimizer sees all possible filters, projections, and joins before choosing physical work.
Operational boundary
A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used. Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

Visual diagram

Loading interactive diagram

Theory & deep dive

Transformations: the working mechanism

Spark transformations add nodes to an immutable logical plan without immediately running work; actions such as collect, count, write, or foreach force that plan and may recompute unpersisted branches.

Immutable DataFrame transformation graph. Catalyst prunes and combines expressions, then action starts jobs and stages; this exposes the state governed by Lazy.

Transformations vs Actions & Lazy Evaluation in one concrete design

clean = orders.filter(F.col('status')=='paid').select(...); clean.explain() creates no job; clean.write.mode('overwrite').parquet(out) is the action that scans and commits partitions.

Key takeaway: Spark transformations add nodes to an immutable logical plan without immediately running work

Transformations vs Actions & Lazy Evaluation: failure mode and production response

A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used.

Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure. Track Actions as "count job triggers, not Python lines", and make Recomputation visible to the dataset owner.

Key takeaway: Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

How it works, step by step

Step 1 of 4
Transformations

1. Build

Immutable DataFrame transformation graph. Spark transformations add nodes to an immutable logical plan without immediately running work; actions such as collect, count, write, or foreach force that plan and may recompute unpersisted branches.

immutable DataFrame transformation graph
Key numbers at this step
Actions
count job triggers, not Python lines
Actions
An operation that requires a result, such as `count`, `collect`, `show`, or a write, and triggers execution.

When to use it & trade-offs

What it is

Spark transformations add nodes to an immutable logical plan without immediately running work; actions such as collect, count, write, or foreach force that plan and may recompute unpersisted branches. clean = orders.filter(F.col('status')=='paid').select(...); clean.explain() creates no job; clean.write.mode('overwrite').parquet(out) is the action that scans and commits partitions.

Why it matters: A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used. Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

When to reach for it

  • Know which APIs build a plan (transformations) versus trigger computation (actions) under lazy evaluation.
  • Choose Transformations because it provides this capability: Narrow or wide plan operators that return a new distributed dataset reference without launching computation.
  • Keep evidence for Actions at "count job triggers, not Python lines" so the last production stage-Commit - sink task results become one query outcome-remains testable.

When to avoid or be careful

  • A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used.
  • Do not conflate Actions with Transformations. The second concept contributes different behavior: An operation that requires a result, such as count, collect, show, or a write, and triggers execution.
  • Release only after this guardrail is in place: Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

Transformations / Actions / Lazy - roles, limits, and operating cost

Transformations
Best for: Narrow or wide plan operators that return a new distributed dataset reference without launching computation.

Strengths: Narrow or wide plan operators that return a new distributed dataset reference without launching computation. Spark transformations add nodes to an immutable logical plan without immediately running work.

Watch out: A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used.

Actions
Best for: An operation that requires a result, such as count, collect, show, or a write, and triggers execution.

Strengths: An operation that requires a result, such as count, collect, show, or a write, and triggers execution. In this workflow it supports: Trigger - action starts jobs and stages.

Watch out: The operating cost is real: unpersisted graph reruns per action branch.

Lazy
Best for: Deferring computation so the optimizer sees all possible filters, projections, and joins before choosing physical work.

Strengths: Deferring computation so the optimizer sees all possible filters, projections, and joins before choosing physical work. Its output can be checked with Plan depth.

Watch out: Operating guardrail: Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Transformations, and how would Actions reveal it?”
  • “Why is Actions distinct from this behavior: deferring computation so the optimizer sees all possible filters, projections, and joins before choosing physical work?”
  • Production clue: A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used.
  • Design probe: Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

Benchmarks & rules of thumb

Actions -
count job triggers, not Python lines
Recomputation -
unpersisted graph reruns per action branch
Plan depth -
deep chains are optimizer-visible until a barrier or external function

Common mistakes & follow-ups

Common interview mistakes

  • A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used.
  • Reporting success from a job exit code without checking Actions and Recomputation.
  • Treating this example as complete without replay, rollback, or access boundaries: clean = orders.filter(F.col('status')=='paid').select(...); clean.explain() creates no job; clean.write.mode('overwrite').parquet(out) is the action that scans and commits partitions.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: Narrow or wide plan operators that return a new distributed dataset reference without launching computation.
  • What does the recovery plan do when this occurs? A notebook line ending in df.filter(...) appears to run but has not read data, and two downstream actions can repeat every expensive shuffle unless a validated cache or checkpoint is used.
  • When is Transformations insufficient for Recomputation, and what does adding Actions cost in Actions?
Interview tip: Lead with the data path and failure boundary: Spark transformations add nodes to an immutable logical plan without immediately running work; actions such as collect, count, write, or foreach force that plan and may recompute unpersisted branches. Then make the answer operational: Inspect explain, place narrow filters/projections before actions, keep the final action explicit, and persist only reused intermediate DataFrames whose recomputation cost exceeds storage and memory pressure.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Transformations vs Actions & Lazy Evaluation with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Apache Spark Architecture (Driver, Executors, Cluster Manager, DAG Scheduler)
Next topic
Jobs, Stages, Tasks & Narrow vs Wide Dependencies