Why one computer isn't enough
ShopKart grows. Now there are 2 billion order rows. One laptop would take hours just to read them. Spark's idea is simple: cut the data into pieces and give each piece to a different worker. Like counting votes in an election: one person counting everything takes days; 1,000 people each counting one box takes minutes, and someone adds up their totals.
Driver and executors
π§ Driver (1 per application)
Runs your notebook code, builds the plan, splits it into tasks, collects results
π§°Worker nodes run executors
Each executor has CPU cores (task slots) and memory
Executor 1
Tasks on partitions 1, 2
Executor 2
Tasks on partitions 3, 4
Executor 3
Tasks on partitions 5, 6
βοΈData in cloud storage
Each partition is read straight from storage by an executor
- Driver: the brain. It turns your code into a plan and hands out tasks. If the driver dies, the job dies.
- Executor: a worker process with some CPU cores and memory. Each core runs one task at a time.
- Partition: one chunk of the data. One task processes one partition.
- Cluster manager: gets machines for Spark (on Databricks, Databricks does this for you).
Transformations vs actions (lazy evaluation)
Transformations describe what you want (filter, select, groupBy, join). Spark doesn't run them; it writes them into a plan. Actions ask for a result (count, show, collect, write). Only then does Spark optimise the whole plan and run it. This is lazy evaluation, and it lets Spark skip work: for example, read only the columns you use.
from pyspark.sql import functions as F
numbers = spark.range(1_000_000) # 1 million rows: 0, 1, 2, ...
print(numbers.rdd.getNumPartitions()) # how many pieces Spark split it into
evens = numbers.filter("id % 2 = 0") # transformation: nothing runs yet
print(evens.count()) # action: NOW Spark runs a job β 500000| Transformations (lazy) | Actions (run a job) |
|---|---|
| select, filter / where, withColumn, drop | show, display, count |
| groupBy + agg, join, orderBy, distinct | collect, take, first |
| union, repartition, dropDuplicates | write / saveAsTable |
Narrow vs wide transformations
Narrow: each output partition needs only one input partition, so every executor works alone (filter, select, withColumn). Wide: rows must move between machines so that matching keys meet (groupBy, join, distinct, orderBy). That movement is the shuffle: it writes to disk and goes over the network, so it's slow.
Partition 1
- Kolkata
- Delhi
- Kolkata
Partition 2
- Delhi
- Mumbai
- Kolkata
Shuffle
Rows travel over the network so each city lands on one executor
After
- Kolkata Γ3
- Delhi Γ2
- Mumbai Γ1
by_last_digit = (evens
.groupBy((F.col("id") % 10).alias("last_digit")) # wide: rows must move between machines
.count())
by_last_digit.explain() # look for "Exchange": that's the shuffle
display(by_last_digit)In the printed plan, Exchange means shuffle. We checked: this plan contains one, and the result has 5 rows (last digits 0, 2, 4, 6, 8).
Jobs, stages and tasks
βΆοΈJob
Created by each action (count, write...)
π§±Stages
A job is cut into stages at every shuffle
Stage 1
Read + filter (narrow)
Stage 2
After shuffle: count per group
πΉTasks
One per partition in a stage, running in parallel on executor cores
So: 1 action β 1 job β stages (split at shuffles) β tasks (one per partition). You see all of this in the Spark UI, which is how you'll debug slow jobs in lesson 11.
The Catalyst optimizer
Before running, Spark's Catalyst optimizer rewrites your plan: it pushes filters down to the file read (predicate pushdown), reads only the columns you use (column pruning), and picks join strategies. That's why DataFrames and SQL beat hand-written RDD code. df.explain() prints the final plan.
Why Parquet files?
Delta tables store data as Parquet: a columnar file format. All values of one column sit together, compressed. A query that needs 3 of 50 columns reads only those 3, and files keep min/max statistics so Spark can skip whole files. This is a big reason analytics on Databricks is fast.
π― Interview questions on this topic
Try to answer out loud first, then tap to check.
Q1Explain Spark architecture.Fresher+
Q2What is lazy evaluation and why is it useful?Fresher+
Q3Narrow vs wide transformations, with examples?1β3 yrs+
Q4What happens when you call df.count()?1β3 yrs+
Q5repartition vs coalesce?1β3 yrs+
Q6Why is collect() dangerous?3+ yrs+
Q7RDD vs DataFrame vs Dataset?3+ yrs+
Key takeaways
- βSpark is fast because many executors process many partitions in parallel.
- βLazy evaluation lets Spark optimize the whole plan before running anything.
- βShuffles (from wide transformations) are the expensive part of most jobs.
- βJobs split into stages at shuffles, and stages into one task per partition.
Frequently asked questions
What is Apache Spark in simple words?
Spark is an engine that splits a big data job into small pieces and runs them on many computers at the same time, then combines the results. It's why Databricks can process terabytes in minutes.
What is lazy evaluation in Spark?
Transformations like filter or select don't run straight away. Spark just writes them down as a plan. Only when you call an action (count, show, write) does it optimise the whole plan and run it.
What is a shuffle?
A shuffle moves rows between machines so that rows with the same key end up together, for example before a groupBy or join. It uses the network and disk, so it's the slowest part of most Spark jobs.
What is the difference between an RDD and a DataFrame?
An RDD is Spark's original low-level collection of objects. A DataFrame is a table with named columns and a schema. DataFrames are optimised by the Catalyst optimizer, so they are faster and easier. Use DataFrames.
How many partitions should I have?
A common rule: partitions of roughly 100β200 MB each, and at least 2β3 times as many partitions as CPU cores so every core stays busy. On Databricks, Adaptive Query Execution tunes shuffle partitions for you.
π§ Quick check: how Spark works
1. Which is an ACTION?
2. Which transformation causes a shuffle?
3. What runs the tasks?
4. One task works on...
5. A stage boundary appears where...
Checking your accessβ¦