Resilient Distributed Datasets: partitions, lineage, fault tolerance, and when low-level RDDs still matter under the modern APIs.
⏱ 5 min readTopics chapter readerLevel · Foundations
01 · Overview
Start here
⏱ 4 min · Topic 1 of 6
Under every DataFrame there is an RDD. RDD means Resilient Distributed Dataset. It is the oldest and lowest layer of Spark.
You will rarely write RDD code today. But the RDD explains three ideas that every later chapter uses: partitions, lineage and lazy work.
You will learn to:
Make an RDD from the shop orders and see its partitions.
Use lineage to explain how Spark recovers lost data.
Run transformations and actions on an RDD.
Choose between an RDD and a DataFrame.
02 · The idea
What an RDD is
⏱ 5 min · Topic 2 of 6
An RDD is one list of items, cut into partitions. A partition is one piece of the list. Each task works on one partition, so many partitions can be worked on at the same time.
The SparkContext, sc, makes RDDs. parallelize() turns a Python list into an RDD, here with 3 partitions:
Read it from the bottom up. (3) is the number of partitions, and file paths are removed. PySpark joins filter and map into one PythonRDD.
The +- line marks a shuffle. reduceByKey must bring all amounts of one customer together, so the work splits into two stages there (chapter 5).
Resilient means Spark can rebuild lost data. Suppose the machine that holds partition 1 stops.
Spark does not need a backup copy. It runs the same steps again on orders 103 and 104, on another machine.
This costs little before a shuffle, because one partition needs only its own input. After a shuffle, one lost partition can need input from every partition before it, so the rebuild costs more.
Lineage has a limit. If the source itself is gone, such as a temp file that was deleted, Spark cannot run the steps again.
Checkpointing helps here. It writes a copy of an RDD to safe storage, so recovery can start from that copy. Chapter 19, Caching, Persistence & Checkpointing, shows how.
Interview question
“How does Spark recover when a machine fails?” It reruns the lineage for the lost partitions on another machine. It does not keep backup copies of the data.
05 · Choosing
RDDs vs DataFrames
⏱ 6 min · Topic 5 of 6
A DataFrame is also an RDD underneath. You can see it with .rdd:
Each item of df.rdd is a Row. orders.csv is the file from chapter 3.
So why use DataFrames? A DataFrame has a schema, so Spark knows the columns and their types. Its optimizer, Catalyst, can then rewrite your query into a faster plan.
In an RDD, Spark sees only your lambda functions. It cannot look inside them, so it cannot improve them.
RDD
DataFrame
Spark sees
Functions it cannot read
Columns and types
Optimizer
None
Catalyst (chapter 13)
Use it for
Rare low-level work
Almost all data work
Use DataFrames and Spark SQL by default. Use an RDD only when the DataFrame API cannot do the job, for example to control partitions by hand.
Check yourself. Why can Spark rebuild partition 1 without a backup? (Answer: it keeps the lineage and runs the same steps again.)
06 · Next Chapter
Next Chapter
You have seen partitions, lineage and lazy work on a small RDD.
⏱ 3 min · Topic 6 of 6
Next chapter
Transformations, Actions & Lazy Evaluation
You have seen partitions, lineage and lazy work on a small RDD.
Next, the same ideas on DataFrames: why nothing runs until an action, and how a shuffle splits a job into stages.