Skip to main content
PYSPARK โ€ข LESSON 169

In-Memory Ingestion Caching with cache()

How and when do we cache a DataFrame in executor memory when it is accessed multiple times across queries?

Expertโฑ 2 Minutesโญ 900 XP
๐Ÿค” THE QUESTION

How and when do we cache a DataFrame in executor memory when it is accessed multiple times across queries?

๐Ÿ’ก WHAT IS IT?

cache() sets StorageLevel.MEMORY_AND_DISK, storing deserialized partitions in memory after the first action.

๐ŸŽฏ WHAT IS IT USED FOR?

Iterative machine learning, multi-query dashboard backends, and avoiding re-reading large datasets from disk.

๐Ÿ’ป EXAMPLE
df = spark.read.parquet("data/large_dataset.parquet").cache()
df.count()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for In-Memory Ingestion Caching with cache().

  • โœ“ Call cache() on DataFrame
  • โœ“ Trigger materialization with count() action
  • โœ“ Accelerate subsequent multi-pass queries