Spark & PySpark
Apache Spark RDDs: A Deep Dive into the Core of Spark
In the world of big data, speed, scalability, and fault-tolerance are non-negotiable. At the heart of Apache Spark lies RDDs (Resilient…
In the world of big data, speed, scalability, and fault-tolerance are non-negotiable. At the heart of Apache Spark lies RDDs (Resilient Distributed Datasets) — the core data abstraction that powers Spark’s distributed computations.
Before Spark introduced higher-level APIs like DataFrames and Datasets, everything revolved around RDDs. And even today, they remain the backbone of Spark’s engine. If you want to truly understand Spark, you must understand RDDs.
What is an RDD?
An RDD (Resilient Distributed Dataset) is an immutable, distributed collection of objects that can be processed in parallel across a cluster.
Let’s break down the name:
- Resilient → Fault-tolerant. Lost data can be recomputed via lineage.
- Distributed → Stored across multiple nodes in a cluster.
- Dataset → A collection of records (like a Python list, but spread out across machines).
👉 Imagine an RDD as a large list split into multiple slices (partitions). Each slice is processed independently on different worker nodes — and Spark brings the results together at scale.
Key Features of RDDs
- Immutable → Once created, you cannot modify an RDD. Transformations always create a new RDD.
- Lazy Evaluation → Spark doesn’t compute results immediately; it builds a DAG (Directed Acyclic Graph) and executes only when an action is called.
- Partitioned Data → RDDs are divided into logical partitions for parallelism.
- In-Memory Computation → Data can be cached in memory for faster re-use.
- Fault-Tolerant → If a partition is lost, Spark recomputes it using lineage.
RDD Operations
RDDs support two main categories of operations:
1. Transformations
Operations that create a new RDD from an existing one.
Examples: map(), filter(), flatMap(), distinct(), union(), join().
2. Actions
Operations that trigger computation and return a result to the driver or write data to storage.
Examples: collect(), count(), first(), take(), reduce(), saveAsTextFile().
👉 Key Insight: Transformations are lazy; actions are eager.
Lazy Evaluation and DAGs in Spark
One of the biggest reasons Spark is so fast and efficient is because of its lazy evaluation model.
Instead of executing each operation step by step (like traditional systems), Spark builds a logical plan of all transformations first, and only runs the computation when an action is triggered (like collect() or count()).
How It Works:
- Transformations build a DAG
- Every transformation (
map,filter,flatMap, etc.) creates a new node in a Directed Acyclic Graph (DAG). - But nothing is executed immediately.
2. Actions trigger execution
- When you call an action, Spark looks at the DAG and decides the best way to execute it.
- The DAG is split into stages, and each stage is divided into tasks that run in parallel on cluster nodes.
3. Optimized execution
- Spark eliminates unnecessary steps, pipelines operations, and reduces disk I/O by keeping data in memory.
- This makes Spark much faster compared to Hadoop MapReduce (which writes intermediate data to disk at each step).
Example of Lazy Evaluation
rdd = sc.parallelize([1, 2, 3, 4, 5])
# Transformations (lazy)
rdd2 = rdd.map(lambda x: x * 2)
rdd3 = rdd2.filter(lambda x: x > 5)
# Action (triggers execution)
print(rdd3.collect())👉 Even though we defined map and filter, Spark doesn’t run them until we call .collect(). At that point, Spark looks at the DAG (parallelize → map → filter → collect) and executes it efficiently.
Why DAGs Matter
- DAGs give Spark fault tolerance: if one task fails, Spark knows exactly which transformations to recompute.
- DAGs make Spark optimizable: Spark can rearrange or combine steps for efficiency.
- DAGs provide parallelism: tasks from different partitions can run at the same time on different machines.
⚡ In short: Lazy evaluation + DAG execution is what makes Spark both fast and fault-tolerant.

Mini PySpark Example
Let’s see RDDs in action:
from pyspark import SparkContext
# Start SparkContext
sc = SparkContext("local", "RDDExample")
# Create an RDD from a Python list
data = [1, 2, 3, 4, 5, 6]
rdd = sc.parallelize(data)
# Transformation: filter even numbers
even_rdd = rdd.filter(lambda x: x % 2 == 0)
# Transformation: square the numbers
squared_rdd = even_rdd.map(lambda x: x * x)
# Action: collect results
result = squared_rdd.collect()
print("Squared Even Numbers:", result)Output:
Squared Even Numbers: [4, 16, 36]👉 Breakdown:
- Original RDD →
[1, 2, 3, 4, 5, 6] - Filtered RDD →
[2, 4, 6] - Mapped RDD →
[4, 16, 36] - Action (
collect) triggers execution and returns results.
This shows how RDD transformations are lazy until an action is called.
Fault Tolerance with Lineage
One of the most powerful aspects of RDDs is lineage.
If a node storing a partition fails, Spark doesn’t panic — it simply recomputes the lost partition using the sequence of transformations applied on the original dataset.
Example lineage graph:
Raw Data → filter() → map() → reduce()If data from map() is lost, Spark just re-runs filter() on the raw data, then applies map() again. This makes RDDs naturally fault-tolerant.
When to Use RDDs?
Even though DataFrames and Datasets are more popular now, RDDs are still useful when:
- You need low-level transformations and control over data.
- You’re working with unstructured data (text, logs, binary data).
- Performance tuning requires fine-grained control.
RDDs vs DataFrames vs Datasets

Conclusion
RDDs are the building blocks of Spark, offering fine-grained control, immutability, and fault-tolerance. While most modern workloads use DataFrames or Datasets for simplicity and optimization, understanding RDDs gives you the core foundation of how Spark works under the hood.
Whether you’re filtering log files, recomputing lost partitions, or chaining transformations, RDDs demonstrate the true power of distributed computing.
👉 Coming Next: Essential RDD Transformations and Actions in PySpark — a hands-on guide where we’ll explore the most common operations step by step with code examples.