1. A data engineer runs `df.persist(StorageLevel.DISK_ONLY)` on a 50 GB DataFrame before two downstream transformations. A colleague suggests switching to `df.cache()` instead. What is the PRIMARY difference between these two calls in a default Databricks cluster configuration?
- A. `cache()` stores data in memory only (MEMORY_AND_DISK is not available on Databricks), while `persist(DISK_ONLY)` stores data only on disk.
- B. `cache()` is equivalent to `persist(MEMORY_AND_DISK)` with deserialized storage, and will spill to disk only when memory is insufficient, whereas `persist(DISK_ONLY)` never uses memory — data is always read from disk for each downstream action.✓ Correct
- C. `persist(DISK_ONLY)` automatically replicates the data across two executor nodes for fault tolerance, while `cache()` does not.
- D. `cache()` persists the DataFrame across Spark sessions, while `persist(DISK_ONLY)` is scoped to the current session only.
Explanation
`df.cache()` in Spark is shorthand for `persist(StorageLevel.MEMORY_AND_DISK)`, which stores deserialized partitions in JVM heap memory and spills to executor local disk only when memory pressure occurs. `persist(DISK_ONLY)` always writes serialized partitions to disk and reads them back from disk for every downstream use, which avoids memory pressure but incurs higher I/O cost. For a 50 GB DataFrame on a cluster with sufficient memory, `cache()` would be faster; if memory is insufficient, both ultimately use disk but `cache()` still tries memory first. Option A is incorrect: MEMORY_AND_DISK is the default for `cache()` on Databricks. Option C is incorrect: DISK_ONLY_2 provides replication, not plain DISK_ONLY. Option D is incorrect: neither call persists data across sessions; both are scoped to the current SparkContext.