Menu

Apache Spark course · Lesson 2 of 11

Hadoop vs Spark: Differences, How They Work and When Each Is Used

Hadoop vs Spark explained simply: HDFS, YARN and MapReduce, how Spark keeps work in memory, a side-by-side code example, and where each fits in 2026.

  • Beginner
  • 6 min read
  • Updated Nov 2026
On this page
  1. What Hadoop is made of
  2. MapReduce in a few lines
  3. The same work in Spark
  4. Hadoop vs Spark side by side
  5. Where each fits in 2026
  6. Common mistakes
  7. What interviewers look for
  8. Key takeaway

Hadoop is a platform for storing and processing very large data on a cluster of ordinary machines. It bundles a distributed file system (HDFS), a resource manager (YARN) and a processing engine (MapReduce). Spark is a processing engine only. It does the same kind of work as MapReduce, but keeps intermediate results in memory and offers much easier APIs, so it is usually faster and simpler to use.

So the honest comparison is Spark vs MapReduce. Spark often runs on Hadoop, reading files from HDFS and getting machines from YARN.

What Hadoop is made of

  • HDFS (Hadoop Distributed File System) splits each file into large blocks (128 MB by default) and stores copies of every block on several machines (three by default). If a machine dies, the data is still there.
  • YARN (Yet Another Resource Negotiator) shares the cluster’s CPU and memory between jobs. Since Hadoop 2 it can run engines other than MapReduce, including Spark.
  • MapReduce processes data in three phases: map (turn each input record into key-value pairs), shuffle (move all pairs with the same key to the same machine), and reduce (combine the values for each key).
  • The wider ecosystem grew around these: Hive for SQL on Hadoop, HBase for key-value storage, and tools for ingestion and scheduling.

Hadoop’s big idea was to move the computation to the data: run the map step on the machines that already hold the blocks, instead of copying terabytes over the network.

MapReduce in a few lines

This plain-Python model shows the three phases on six page-view log lines. On a real cluster, each phase runs on many machines at once:

from itertools import groupby

# Page-view log lines: date, page, country
lines = [
    "2026-10-01 /pricing IN", "2026-10-01 /docs US", "2026-10-01 /pricing US",
    "2026-10-02 /docs IN", "2026-10-02 /pricing IN", "2026-10-02 /blog GB",
]

# 1. Map: each line becomes (key, 1). In Hadoop, many mappers do this in parallel, one per file block.
mapped = [(line.split()[1], 1) for line in lines]

# 2. Shuffle and sort: bring all pairs with the same key together. Hadoop writes these to disk between steps.
shuffled = sorted(mapped)

# 3. Reduce: one reducer per key adds up the values.
for page, pairs in groupby(shuffled, key=lambda kv: kv[0]):
    print(page, sum(v for _, v in pairs))
/blog 1
/docs 2
/pricing 3

The weak point is between jobs. A real analysis is rarely one map and one reduce. Each MapReduce job reads its input from HDFS and writes its full output back to HDFS, with copies on several machines, before the next job can start. A ten-step pipeline pays for that ten times.

The same work in Spark

Spark plans the whole pipeline as one graph of steps (a DAG) and passes data between steps in memory where it can. Here the parsed data is cached and reused by two different questions:

from pyspark.sql import SparkSession, functions as F

spark = SparkSession.builder.master("local[2]").appName("page-views").getOrCreate()
spark.sparkContext.setLogLevel("ERROR")

lines = [
    "2026-10-01 /pricing IN", "2026-10-01 /docs US", "2026-10-01 /pricing US",
    "2026-10-02 /docs IN", "2026-10-02 /pricing IN", "2026-10-02 /blog GB",
]
views = (spark.createDataFrame([(l,) for l in lines], ["line"])
         .select(F.split("line", " ").alias("p"))
         .select(F.col("p")[0].alias("day"), F.col("p")[1].alias("page"), F.col("p")[2].alias("country"))
         .cache())                                   # keep in memory: the next two queries reuse it

views.groupBy("page").count().orderBy("page").show()
views.groupBy("country").agg(F.countDistinct("page").alias("pages")).orderBy("country").show()
spark.stop()
+--------+-----+
|    page|count|
+--------+-----+
|   /blog|    1|
|   /docs|    2|
|/pricing|    3|
+--------+-----+

+-------+-----+
|country|pages|
+-------+-----+
|     GB|    1|
|     IN|    2|
|     US|    2|
+-------+-----+

Spark still shuffles data between machines for groupBy, as MapReduce does. The differences are that it does not write every intermediate result to the distributed file system, it optimises the whole query plan before running, and you write a few lines of DataFrame code or SQL instead of separate mapper and reducer classes. See the Spark execution model for how jobs, stages and shuffles fit together.

Hadoop vs Spark side by side

Hadoop (HDFS + YARN + MapReduce) Spark
What it is Storage, resource management and processing Processing engine only
Storage HDFS None of its own: reads HDFS, S3, ADLS, GCS, databases, Kafka
Intermediate data Written to disk between jobs Kept in memory where possible, spills to disk when needed
Programming Java mappers and reducers (or Hive SQL on top) Python, SQL, Scala, Java and R; DataFrames and SQL
Workloads Batch Batch, SQL, streaming, machine learning, graph processing
Speed Slow for multi-step and repeated work Much faster for multi-step, iterative and interactive work
Runs on Its own cluster YARN, Kubernetes, standalone, or managed services
Memory needs Modest Higher, since it benefits from caching data

Where each fits in 2026

  • Spark is the default engine for large-scale batch processing and a common choice for streaming, available as Databricks, Amazon EMR, Google Dataproc, Azure services and plain open source.
  • HDFS has mostly given way to cloud object storage (S3, ADLS, GCS) in new platforms, because storage and compute can then scale separately and nobody has to run the file system.
  • MapReduce is rarely chosen for new work. You meet it in older on-premises clusters, where Hive jobs may still run on it, and in interview questions about the history of big data.
  • YARN is still widely used in on-premises clusters and in services such as EMR, often to run Spark.
  • The ideas live on. Splitting data into blocks, moving computation to data, map and shuffle phases, and replication for fault tolerance all appear in Spark and in cloud warehouses.

If you join a company with an on-premises Hadoop cluster, expect migration work: moving data from HDFS to object storage and rewriting Hive or MapReduce jobs as Spark or warehouse SQL.

Common mistakes

  1. Saying “Spark replaced Hadoop”. Spark replaced MapReduce as the engine; HDFS and YARN can still be underneath.
  2. Saying Spark does everything in memory. It spills to disk when data does not fit, and shuffles still write files.
  3. Assuming Spark is always faster. For a single pass over data that is far bigger than memory, the gap is much smaller.
  4. Forgetting Spark needs storage. It needs HDFS, object storage or a database to read from and write to.

What interviewers look for

For “Hadoop vs Spark” or “why is Spark faster than MapReduce”:

  • That Hadoop has three parts and Spark only replaces the processing part.
  • In-memory processing between steps, DAG planning and query optimisation as the reasons for speed.
  • That Spark can run on YARN and read HDFS.
  • Where each is used today, including the move to cloud object storage.

Practise with the data engineering interview questions, then continue with Apache Spark architecture.

Key takeaway

Hadoop combines distributed storage (HDFS), resource management (YARN) and a disk-based processing engine (MapReduce). Spark is a faster, easier processing engine that keeps intermediate data in memory, plans whole pipelines and supports SQL, streaming and machine learning. Today Spark usually runs on cloud object storage or Kubernetes, but it can still run on Hadoop clusters, and Hadoop’s core ideas are built into it.

By , Data Engineer and Founder of DataDank · Last reviewed Nov 2026 · Describes Apache Hadoop 3.x and Apache Spark 4.x. The MapReduce example is a plain-Python model of the three phases; the Spark example runs on PySpark 4.2.0 in local mode. Both outputs are real.

Progress is saved in this browser only. No account needed.

Search
Filter by type