Say you have a log file too big to open in Excel, or a sensor table with billions of rows. One laptop would take hours to scan it. Apache Spark solves this by splitting the data into small pieces and handing them to many computers at once, then putting the answers back together for you.
In this guide we will follow one Spark job from your code to the cluster. You will meet the driver, the cluster manager and the executors, see how a job breaks into stages and tasks, and then count the tasks and waves for a real 10 GB file yourself.

Apache Spark is an open source engine that processes large datasets in parallel across a cluster, keeping working data in memory where it can.
- A driver plans the work
- Executors run tasks
- One task per partition
- Shuffles split jobs into stages
- RDD, DataFrame and SQL APIs
What Apache Spark is
Spark is a distributed processing engine. It does not store your data; it reads from storage such as HDFS, Amazon S3, local files or databases, works on the data across many machines and writes results back.
It started as research at UC Berkeley. The 2012 RDD paper by Zaharia and colleagues showed the key idea: keep datasets in memory across steps and rebuild any lost piece from its recorded history. The paper reported speed-ups of up to 20 times over Hadoop for iterative applications such as machine learning.
One engine covers several jobs. Spark SQL handles tables and queries, Structured Streaming handles live data, MLlib provides machine learning and GraphX handles graphs. You can write Spark code in Scala, Java, Python or SQL.
How Apache Spark runs a job on a cluster
Every Spark application has the same three parts, as the official cluster mode overview describes.
- Driver: the process that runs your main program. It holds the SparkContext (or SparkSession), turns your code into a plan and schedules tasks.
- Cluster manager: the service that hands out machines. Spark works with its own standalone manager, Hadoop YARN and Kubernetes.
- Executors: worker processes launched on the cluster nodes. Each one runs tasks in its cores and can cache data in memory for later steps.
Jobs, stages and tasks
Spark code has two kinds of operations. Transformations such as filter, select or groupBy only describe a new dataset; they are lazy and run nothing. An action such as count, collect or write asks for a result, and that is what starts a job.
- Action. You call an action, for example writing a result table. The driver creates one job for it.
- DAG of stages. The driver walks back through your transformations and builds a directed acyclic graph. It cuts the graph into stages wherever data must be shuffled between machines.
- Tasks. Each stage becomes a set of tasks, one task for each partition of the data in that stage.
- Scheduling. The task scheduler sends tasks to free executor cores. With the default setting of one core per task, an executor with 4 cores runs 4 tasks at the same time.
- Shuffle and next stage. When a stage ends, its output is written out and regrouped by key, then the next stage starts reading it.
A narrow transformation, such as filter, needs only the data already in one partition, so many of them fit inside one stage. A wide transformation, such as groupBy or a join, needs rows with the same key from every partition. That move across the network is the shuffle, and it is the most costly part of most jobs.
RDD, DataFrame and Spark SQL
Spark gives you three ways to describe the same work. All of them end up as tasks on executors.
The Resilient Distributed Dataset is the original low level API from the 2012 paper. An RDD is a read-only collection of objects split into partitions across the cluster.
It is fault tolerant through lineage: Spark remembers the steps that built each partition, so a lost partition is recomputed instead of copied. RDDs give full control but no automatic optimisation.
A DataFrame is a table of rows with named, typed columns, much like a table in pandas or a database. You call methods such as select, filter and groupBy.
Because Spark knows the schema, the Catalyst optimizer can rewrite your plan, for example by pushing filters down to the data source. This is the API most people use today.
Spark SQL lets you register a DataFrame as a view and query it with ordinary SQL. The query goes through the same Catalyst optimizer as DataFrame code.
So a SQL query and the matching DataFrame code produce the same physical plan. Pick whichever your team reads more easily.
Why partitions matter so much
A partition is the unit of parallel work. Too few partitions leave cores idle; too many create lots of tiny tasks whose scheduling overhead adds up.
When Spark reads files such as Parquet, JSON or ORC, the setting spark.sql.files.maxPartitionBytes caps each partition at 128 MB by default. After a shuffle, spark.sql.shuffle.partitions sets the number of partitions, 200 by default, unless adaptive query execution merges small ones.
Key formulas for Apache Spark parallelism
Three simple relations tell you how much parallel work a stage has and how many rounds it takes.
| Quantity | Formula | Symbols and units |
|---|---|---|
| Partitions when reading a file | N = ceil(S ÷ P) | S = file size in MB, P = maximum partition size in MB (128 MB default) |
| Parallel task slots | Slots = E × C | E = executors, C = cores per executor (one core per task by default) |
| Waves in a stage | W = ceil(N ÷ Slots) | N = tasks in the stage, W = rounds of tasks |
Worked example: counting tasks and waves
- Given
- A 10 GB Parquet file (10,240 MB), a cluster of 5 executors with 4 cores each, and a job that filters the rows, then groups them by key and writes the totals
- Find
- The number of tasks and waves in each stage
- Assumptions
- Default settings: 128 MB maximum file partition, 200 shuffle partitions, 1 core per task. Adaptive query execution (on by default since Spark 3.2) is turned off, so the shuffle keeps exactly 200 partitions.
- Step 1Stage 1 reads and filters. N = 10,240 ÷ 128 = 80 partitions, so 80 tasks
- Step 2Task slots = 5 executors × 4 cores = 20 tasks at a time
- Step 3Stage 1 waves = 80 ÷ 20 = 4 waves
- Step 4groupBy needs a shuffle, so Stage 2 starts with 200 partitions, so 200 tasks
- Step 5Stage 2 waves = 200 ÷ 20 = 10 waves. Total tasks = 80 + 200 = 280
Limitations: Spark can choose smaller read partitions when the data is small compared with the total cores, and adaptive query execution usually merges small shuffle partitions. Check the real task counts on the Stages tab of the Spark UI.
If the totals after grouping are small, each of the 200 shuffle tasks may do very little work. Adaptive query execution handles this case by merging small shuffle partitions after it sees their real sizes.
Caching and fault tolerance
Spark recomputes a dataset each time an action needs it. If several actions reuse the same filtered data, call cache or persist on it, and the executors keep its partitions in memory after the first action computes them. For an RDD, cache uses the MEMORY_ONLY storage level.
Failures are handled at the task level. If a task fails, the driver runs it again, by default up to 4 failures of the same task (spark.task.maxFailures) before the job is stopped.
If an executor is lost along with its cached partitions, lineage covers the gap. Spark reruns only the steps needed to rebuild the missing partitions, not the whole job.
Where Apache Spark is used
- Batch ETL into data lakes and warehouses
- Large SQL analytics on lakehouse tables
- Feature preparation for machine learning
- Near real-time streams with Structured Streaming
- Log and sensor data processing
- Graph analysis with GraphX
Spark is often the transform engine inside an ETL or ELT pipeline and the compute layer on top of a data lake.
Common beginner mistakes
- Calling collect on a big dataset: it pulls every row back into the driver’s memory, which can crash the driver.
- Expecting transformations to run at once: nothing runs until an action, so timing a filter on its own shows almost nothing.
- Ignoring the shuffle: extra groupBy or join steps add shuffles, and each one moves data across the network.
- Leaving partition counts unchecked: the Spark UI shows tasks per stage, and it is the fastest way to spot too few or too many partitions.
Apache Spark interview and viva questions
Q1What does the driver do in Apache Spark?
It runs your main program, builds the DAG of stages from your code and schedules tasks on the executors.
Q2What is the difference between a transformation and an action?
A transformation such as filter only describes a new dataset and is lazy. An action such as count or write asks for a result and starts a job.
Q3What creates a new stage in a Spark job?
A shuffle. Wide transformations such as groupBy or join need data from every partition, so Spark ends the stage there and starts a new one.
Q4How does an RDD recover from a lost partition?
Through lineage. Spark records the steps that built each partition and recomputes the lost one instead of keeping copies.
Q5How many tasks can 5 executors with 4 cores each run at once?
20, because each core runs one task at a time by default. An 80 task stage therefore takes 4 waves.
Related reading
Frequently asked questions
Is Apache Spark a database?
No. Spark is a processing engine. It reads data from storage such as HDFS, Amazon S3, files or databases, processes it across a cluster and writes the results back out. It does not keep your data permanently the way a database does.
How is Spark different from Hadoop MapReduce?
MapReduce writes intermediate results to disk between steps. Spark plans a whole job as a graph of stages and can keep working data in memory between them. The original RDD paper reported Spark running iterative applications up to 20 times faster than Hadoop. Spark can still run on a Hadoop YARN cluster and read HDFS.
Do I need to learn Scala to use Spark?
No. Spark has APIs for Scala, Java and Python (PySpark), plus Spark SQL. DataFrame code and SQL queries are both planned by the same Catalyst optimizer, so Python DataFrame code usually runs about as fast as the same logic written in Scala.
References
- M. Zaharia et al., Resilient Distributed Datasets: A Fault-Tolerant Abstraction for In-Memory Cluster Computing, USENIX NSDI, 2012
- M. Armbrust et al., Spark SQL: Relational Data Processing in Spark, ACM SIGMOD, 2015
- B. Chambers and M. Zaharia, Spark: The Definitive Guide, O'Reilly, 2018
- J. S. Damji, B. Wenig, T. Das and D. Lee, Learning Spark, 2nd ed., O'Reilly, 2020
- Apache Spark official documentation (Cluster Mode Overview, RDD Programming Guide, Spark SQL Performance Tuning, Configuration), spark.apache.org
Get one engineering email a week
New articles, worked examples and study notes every week. Free, no spam, unsubscribe any time.