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 75/ 160
04. Big Data Processing & Apache Spark

Caching, Persistence Levels & Memory Management (OOM Debugging)

Persist hot DataFrames at the right storage level and debug OOMs with memory fractions and spill clues.

AdvancedCache · Persistence · OOM3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Multiple actions and materialization cost. Next, memory, serialized, disk, and replication. Finally, unpersist, spill, right-size, and fix skew.

Think of it like…

Caching is reserving shelf space for frequently reused parts, but oversized pallets push active work off the floor; unpersist is clearing the shelf when the production batch ends.

Where you’ve seen it

df.persist(StorageLevel.MEMORY_AND_DISK_SER) materializes only on an action; after two branches use curated, call unpersist(); an aggregate's spill records and killed-container reason identify execution-memory pressure.

Words you’ll hear

Cache
A lazy promise to store computed partitions at a selected storage level; it does not run until an action.
Persistence
Storage policies combining memory/disk and serialized/deserialized representations with recomputation behavior.
OOM
An executor termination or task failure from unavailable managed or process memory, sometimes caused by Python objects outside Spark regions.
Operational boundary
Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM. Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

Visual diagram

Loading interactive diagram

Theory & deep dive

Cache: the working mechanism

Spark cache/persist stores a DataFrame in configured execution blocks-memory deserialized, memory serialized, disk, or combinations-while the engine dynamically shares unified memory between execution and storage.

Multiple actions and materialization cost. Memory, serialized, disk, and replication, then explicit action and inspect persisted blocks; this exposes the state governed by OOM.

Caching, Persistence Levels & Memory Management in one concrete design

df.persist(StorageLevel.MEMORY_AND_DISK_SER) materializes only on an action; after two branches use curated, call unpersist(); an aggregate's spill records and killed-container reason identify execution-memory pressure.

Key takeaway: Spark cache/persist stores a DataFrame in configured execution blocks-memory deserialized, memory serialized, disk, or combinations-while the engine dynamically shares unified memory between execution and storage

Caching, Persistence Levels & Memory Management: failure mode and production response

Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM.

Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs. Track Cached fraction as "persisted bytes actually in memory versus disk", and make GC fraction visible to the dataset owner.

Key takeaway: Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

How it works, step by step

Step 1 of 4
Cache

1. Prove reuse

Multiple actions and materialization cost. Spark cache/persist stores a DataFrame in configured execution blocks-memory deserialized, memory serialized, disk, or combinations-while the engine dynamically shares unified memory between execution and storage.

multiple actions and materialization cost
Key numbers at this step
Cached fraction
persisted bytes actually in memory versus disk
Persistence
Storage policies combining memory/disk and serialized/deserialized representations with recomputation behavior.

When to use it & trade-offs

What it is

Spark cache/persist stores a DataFrame in configured execution blocks-memory deserialized, memory serialized, disk, or combinations-while the engine dynamically shares unified memory between execution and storage. df.persist(StorageLevel.MEMORY_AND_DISK_SER) materializes only on an action; after two branches use curated, call unpersist(); an aggregate's spill records and killed-container reason identify execution-memory pressure.

Why it matters: Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM. Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

When to reach for it

  • Persist hot DataFrames at the right storage level and debug OOMs with memory fractions and spill clues.
  • Choose Cache because it provides this capability: A lazy promise to store computed partitions at a selected storage level; it does not run until an action.
  • Keep evidence for Cached fraction at "persisted bytes actually in memory versus disk" so the last production stage-Relieve - unpersist, spill, right-size, and fix skew-remains testable.

When to avoid or be careful

  • Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM.
  • Do not conflate Persistence with Cache. The second concept contributes different behavior: Storage policies combining memory/disk and serialized/deserialized representations with recomputation behavior.
  • Release only after this guardrail is in place: Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

Cache / Persistence / OOM - roles, limits, and operating cost

Cache
Best for: A lazy promise to store computed partitions at a selected storage level; it does not run until an action.

Strengths: A lazy promise to store computed partitions at a selected storage level; it does not run until an action. Spark cache/persist stores a DataFrame in configured execution blocks-memory deserialized, memory serialized, disk, or combinations-while the engine dynamically shares unified memory between execution and storage.

Watch out: Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM.

Persistence
Best for: Storage policies combining memory/disk and serialized/deserialized representations with recomputation behavior.

Strengths: Storage policies combining memory/disk and serialized/deserialized representations with recomputation behavior. In this workflow it supports: Materialize - explicit action and inspect persisted blocks.

Watch out: The operating cost is real: executor timeline time spent in garbage collection.

OOM
Best for: An executor termination or task failure from unavailable managed or process memory, sometimes caused by Python objects outside Spark regions.

Strengths: An executor termination or task failure from unavailable managed or process memory, sometimes caused by Python objects outside Spark regions. Its output can be checked with Spill bytes.

Watch out: Operating guardrail: Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

Interview signals & numbers to know

When this topic comes up

  • “Where is the failure boundary for Cache, and how would Cached fraction reveal it?”
  • “Why is Persistence distinct from this behavior: an executor termination or task failure from unavailable managed or process memory, sometimes caused by Python objects outside Spark regions?”
  • Production clue: Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM.
  • Design probe: Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

Benchmarks & rules of thumb

Cached fraction -
persisted bytes actually in memory versus disk
GC fraction -
executor timeline time spent in garbage collection
Spill bytes -
execution state overflow to local shuffle/sort disk

Common mistakes & follow-ups

Common interview mistakes

  • Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM.
  • Reporting success from a job exit code without checking Cached fraction and GC fraction.
  • Treating this example as complete without replay, rollback, or access boundaries: df.persist(StorageLevel.MEMORY_AND_DISK_SER) materializes only on an action; after two branches use curated, call unpersist(); an aggregate's spill records and killed-container reason identify execution-memory pressure.

Likely interviewer follow-ups

  • How would you prove this contract instead of only checking that the command ran: A lazy promise to store computed partitions at a selected storage level; it does not run until an action.
  • What does the recovery plan do when this occurs? Caching a one-use table wastes RAM, MEMORY_ONLY drops partitions under pressure and later recomputes, and execution OOMs can come from a giant group/broadcast despite apparent free system RAM.
  • When is Cache insufficient for GC fraction, and what does adding Persistence cost in Cached fraction?
Interview tip: Lead with the data path and failure boundary: Spark cache/persist stores a DataFrame in configured execution blocks-memory deserialized, memory serialized, disk, or combinations-while the engine dynamically shares unified memory between execution and storage. Then make the answer operational: Cache only reused, materialized branches proven worthwhile, choose MEMORY_AND_DISK when recomputation is expensive, unpersist promptly, right-size executor memory, and inspect heap, GC, storage blocks, shuffle spill, and Python/executor logs.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Caching, Persistence Levels & Memory Management (OOM Debugging) with data engineer interview Q&A and hands-on practice.

Open Data Engineer Q&A →Machine Learning Q&A
Previous topic
Solving Data Skew (Salting Keys, Adaptive Query Execution / AQE)
Next topic
Reading Spark UI (DAG Visualization, Spill to Disk, GC Time, Shuffle Read/Write)