Caching in Apache Spark means storing a dataset in memory to speed up repeated access. It helps avoid recomputing the dataset every time it is used.
Click to reveal answer
beginner
What does persistence mean in Spark?
Persistence means saving a dataset to memory and/or disk with different storage levels. It allows Spark to reuse the dataset efficiently across multiple operations.
Click to reveal answer
intermediate
Name two common storage levels used in Spark persistence.
Two common storage levels are MEMORY_ONLY (store data only in memory) and MEMORY_AND_DISK (store data in memory, spill to disk if not enough memory).
Click to reveal answer
intermediate
How does caching improve performance in iterative algorithms?
Caching keeps the data in memory so iterative algorithms can reuse it without recomputing or reading from disk each time, making the process faster.
Click to reveal answer
intermediate
What is the difference between cache() and persist() in Spark?
cache() is shorthand for persist() with the default storage level MEMORY_AND_DISK. persist() lets you choose other storage levels like DISK_ONLY or MEMORY_ONLY_SER.
Click to reveal answer
What happens when you call cache() on a DataFrame in Spark?
AThe DataFrame is stored in memory for faster access.
BThe DataFrame is deleted from memory.
CThe DataFrame is saved permanently to disk.
DThe DataFrame is converted to a different format.
✗ Incorrect
cache() stores the DataFrame in memory to speed up future operations.
Which storage level stores data only on disk in Spark persistence?
AMEMORY_ONLY
BOFF_HEAP
CMEMORY_AND_DISK
DDISK_ONLY
✗ Incorrect
DISK_ONLY stores the dataset only on disk, not in memory.
Why would you use persist() instead of cache()?
ATo convert the dataset to JSON format.
BTo delete the dataset from memory.
CTo choose a specific storage level other than the default.
DTo speed up the first computation only.
✗ Incorrect
persist() allows selecting different storage levels like MEMORY_ONLY or DISK_ONLY.
What is a benefit of caching in iterative machine learning algorithms?
AIt increases disk usage.
BIt reduces repeated data loading and computation.
CIt slows down the algorithm.
DIt deletes intermediate results.
✗ Incorrect
Caching avoids recomputing data in each iteration, speeding up the process.
If memory is limited, which storage level helps avoid out-of-memory errors?
AMEMORY_AND_DISK
BMEMORY_ONLY
CDISK_ONLY
DNONE
✗ Incorrect
MEMORY_AND_DISK stores data in memory and spills to disk if memory is full, preventing errors.
Explain caching and persistence in Apache Spark and how they help improve performance.
Think about how storing data in memory or disk helps avoid recomputation.
You got /4 concepts.
Describe the difference between cache() and persist() methods in Spark with examples of when to use each.
Consider how you might want to store data differently depending on memory availability.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using cache() in Apache Spark?
easy
A. To delete data from memory
B. To permanently save data to disk
C. To store data in memory for faster repeated access
D. To convert data into a different format
Solution
Step 1: Understand what caching means in Spark
Caching stores data in memory to speed up repeated operations on the same data.
Step 2: Identify the role of cache()
The cache() method specifically stores the DataFrame or RDD in memory for quick access.
Final Answer:
To store data in memory for faster repeated access -> Option C
Quick Check:
Caching = store in memory [OK]
Hint: Cache means keep data in memory for speed [OK]
Common Mistakes:
Thinking cache saves data permanently on disk
Confusing cache with deleting data
Assuming cache changes data format
2. Which of the following is the correct syntax to persist a DataFrame df with disk storage level in Spark?
easy
A. df.persist(StorageLevel.DISK_ONLY)
B. df.cache(StorageLevel.DISK_ONLY)
C. df.persist('disk_only')
D. df.cache('disk_only')
Solution
Step 1: Recall the correct method to specify storage level
To persist with a specific storage level, use persist() with a StorageLevel object.
Step 2: Check the syntax for disk-only persistence
The correct syntax is df.persist(StorageLevel.DISK_ONLY). The cache() method does not accept parameters.
Hint: Use persist() with StorageLevel, cache() has no params [OK]
Common Mistakes:
Using cache() with parameters
Passing storage level as string instead of StorageLevel object
Using persist() without StorageLevel
3. Consider the following Spark code snippet:
val df = spark.read.csv("data.csv")
df.cache()
df.count()
df.count()
What is the effect of calling df.count() twice after caching?
medium
A. Both counts reload data from disk without caching
B. The first count triggers data loading and caching; the second count uses cached data for faster result
C. The second count triggers caching; the first count uses cached data
D. Both counts use cached data without loading from disk
Solution
Step 1: Understand caching behavior on first action
The first action (count()) triggers loading data and caches it in memory.
Step 2: Effect on second action
The second count() uses the cached data, so it runs faster without reloading from disk.
Final Answer:
The first count triggers data loading and caching; the second count uses cached data for faster result -> Option B
Quick Check:
First action caches, second uses cache [OK]
Hint: First action caches, second action reads cache [OK]
Common Mistakes:
Assuming cache happens before first action
Thinking both counts reload from disk
Believing second count triggers caching
4. Given this Spark code:
val df = spark.read.json("data.json")
df.persist(StorageLevel.MEMORY_AND_DISK)
df.unpersist()
What is the main issue with this code?
medium
A. Calling unpersist() immediately removes cached data, so persistence is ineffective
B. The persist() method requires a string, not StorageLevel object
C. You must call cache() before persist()
D. The code will cause a syntax error due to missing parentheses
Solution
Step 1: Understand the effect of persist()
The persist() call stores the DataFrame in memory and disk as specified.
Step 2: Effect of immediate unpersist()
Calling unpersist() right after removes the cached data, negating the persistence benefit.
Final Answer:
Calling unpersist() immediately removes cached data, so persistence is ineffective -> Option A
Quick Check:
Immediate unpersist clears cache [OK]
Hint: Unpersist right after persist clears cache [OK]
Common Mistakes:
Thinking persist needs string argument
Believing cache must be called before persist
Assuming syntax error due to parentheses
5. You have a large DataFrame df used multiple times in your Spark job. You want to cache it but ensure it uses memory first and spills to disk if memory is full. Which persistence level should you use and why?
hard
A. Use df.persist(StorageLevel.DISK_ONLY) to avoid memory usage
B. Use df.cache() because it automatically spills to disk
C. Use df.unpersist() to keep data only in memory
D. Use df.persist(StorageLevel.MEMORY_AND_DISK) to store in memory and spill to disk if needed
Solution
Step 1: Identify persistence levels for memory and disk
StorageLevel.MEMORY_AND_DISK stores data in memory and spills to disk if memory is insufficient.
Step 2: Compare with other options
cache() only stores in memory and may fail if memory is full; DISK_ONLY avoids memory; unpersist() removes cached data.
Final Answer:
Use df.persist(StorageLevel.MEMORY_AND_DISK) to store in memory and spill to disk if needed -> Option D
Quick Check:
MEMORY_AND_DISK = memory first, then disk spill [OK]
Hint: MEMORY_AND_DISK spills to disk if memory full [OK]
Common Mistakes:
Assuming cache spills to disk automatically
Choosing DISK_ONLY to save memory but slower
Using unpersist to keep data cached
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.