ML & Data Science

Spark vs Hadoop Graph Benchmark

Big data · Distributed processing

MSc Data Science · Big Data coursework (Task 1) · Coventry University

The same graph problem on two big data engines, measured second by second, to see where each one spends its time as the data grows 216×.

Role
Data Engineer
Year
2025
Focus
Big data
Stack
7 technologies
  • PySpark
  • Hadoop MapReduce
  • Hadoop Streaming
  • Python
  • psutil
  • Matplotlib
  • Docker
  • 69M

    Edges in soc-LiveJournal1

  • 216×

    Data growth, 5 MB → 1.08 GB

  • 100%

    Spark and Hadoop outputs match

  • 88 s

    Hadoop on LiveJournal (Spark 95 s)

  • −84%

    Spark runtime on email-EuAll after tuning

  • 2,365×

    Hadoop network growth (Spark 14×)

Spark is usually called the faster engine because it keeps data in memory, while Hadoop MapReduce writes to disk between every stage. That claim needed testing on a real workload: computing who-links-to-whom (in-degree) across graphs from 5 MB to over 1 GB, and checking not just speed but correctness, resource use, and how each engine scales.

Three graphs from the Stanford SNAP collection are used as plain edge lists: email-EuAll (5 MB, 420K edges), web-BerkStan (110 MB, 7.6M edges), and soc-LiveJournal1 (1.08 GB, 69M edges). Both engines skip # comment lines and count incoming edges for every node, then turn those counts into a degree histogram: how many nodes have in-degree 1, 2, 3, and so on.

The Spark version maps each edge to its destination node and uses reduceByKey to count. The Hadoop version is two chained MapReduce Streaming jobs written in Python: one to compute in-degree per node, one to build the histogram. A local fallback reproduces Hadoop's exact output format when a Hadoop install isn't available, and a Docker Compose file defines a Hadoop (NameNode, DataNode, YARN) plus Spark master/worker cluster.

Every run is wrapped by a psutil monitor that samples CPU, memory, disk I/O, and network once per second into a timeseries CSV and a summary JSON. A single main.py runs the whole pipeline: experiments, correctness validation, distribution plots, per-dataset metrics, scaling curves, and a second round of optimised runs with before/after comparisons.

Experiment pipeline

One command (python scripts/main.py) runs every step below, and each step can also be run on its own.

  1. 01

    Edge lists

    SNAP

    email-EuAll, web-BerkStan and soc-LiveJournal1 as source → destination text files; # comment lines skipped.

  2. 02

    Spark job

    PySpark

    Map every edge to (destination, 1), reduceByKey to get in-degree, then count nodes per in-degree value.

  3. 03

    Hadoop job 1

    MapReduce Streaming

    Python mapper emits destination\t1; reducer sums per node to get in-degree.

  4. 04

    Hadoop job 2

    MapReduce Streaming

    Mapper emits in-degree\t1; reducer sums into the degree histogram.

  5. 05

    Metrics

    psutil · 1 Hz

    CPU %, memory, disk read/write and network sent/received sampled every second for each run.

  6. 06

    Validate

    Spark and Hadoop distributions compared line by line for every dataset.

  7. 07

    Plot

    Matplotlib

    Log-log distributions, per-dataset CPU/memory and I/O timelines, scaling curves, and before/after optimisation charts.

Every chart comes from the results folder of the GitHub repo. Spark is blue, Hadoop is orange. Tap any figure to zoom.

  1. Fig. 01 · Correctness · log-log

    Same answer from both engines

    Left column Spark, right column Hadoop, one row per graph. The plots are identical, which visually confirms that both implementations compute exactly the same distribution.

    • All three graphs fall in a near-straight line on log-log axes: a power-law (scale-free) distribution.
    • Most nodes have very few incoming links (in email-EuAll nearly 10⁵ nodes have in-degree 1), while a handful of hubs have thousands.
  2. Fig. 02 · Scalability

    Runtime as data grows

    Hadoop finished first on every dataset: 2.0 s vs 13.2 s on email-EuAll, 9.1 s vs 19.3 s on web-BerkStan, and 88.0 s vs 95.1 s on LiveJournal. The gap narrows sharply as data grows, as Spark's fixed start-up cost matters less.

  3. Fig. 03 · Scalability

    Memory stays flat

    A 216× jump in data size raised peak memory by only about 1.01× for Spark and 1.05× for Hadoop. Both engines stream and partition the graph instead of loading it whole, so memory was not the bottleneck.

  4. Fig. 04 · Scalability

    Disk I/O: Hadoop's bottleneck

    Hadoop's disk writes grew 24.6× (4 MB → 99 MB) because MapReduce writes intermediate results after every phase. Spark's writes grew only 5.2× (15 MB → 78 MB), though its reads jumped 53× on LiveJournal as data spilled under memory pressure.

  5. Fig. 05 · Scalability

    Network shuffle

    Hadoop's network traffic exploded 2,365× (242 bytes → 572 KB) as the shuffle between map and reduce grew with the graph. Spark's grew only 14× thanks to its optimised shuffle, even though it moves more bytes in absolute terms.

  6. Fig. 06 · Resource timeline

    CPU and memory over time · LiveJournal

    Spark's CPU line swings hard (peaks around 70%) as it parallelises transformations in memory and shuffles. Hadoop's CPU is lower and steadier because its tasks are limited by disk throughput rather than computation.

  7. Fig. 07 · Resource timeline

    I/O bursts · LiveJournal

    The sharp orange spikes are Hadoop writing each phase's output to disk. Spark's disk activity is mostly low and smooth, with a burst near the end when results are written.

  8. Fig. 08 · Resource timeline

    CPU and memory over time · web-BerkStan

    On the mid-sized web graph Spark peaks near 90% CPU while Hadoop stays between 40% and 75%, matching the theory that in-memory DAG execution trades CPU for less disk.

  9. Fig. 09 · Totals

    Summary · LiveJournal

    On the largest graph Hadoop is slightly faster but does more disk I/O, while Spark uses more network (about 0.85 MB vs 0.57 MB).

  10. Fig. 10 · Optimisation

    Before vs optimised

    A second round of tuned runs (spark_opt, hadoop_opt) cut runtimes on every dataset. Hadoop's optimised run adds combiners so counts are pre-aggregated on the map side before the shuffle.

  11. Fig. 11 · Optimisation

    Optimisation gains per dataset

    The biggest win is Spark on email-EuAll: 13.2 s down to 2.1 s (−84%). On LiveJournal, Spark drops from 95.1 s to 81.9 s and Hadoop from 88.0 s to 82.9 s, so the two engines end up almost level.

  12. Fig. 12 · Optimisation

    Disk before vs optimised

    Combiners shrink Hadoop's intermediate data: on LiveJournal its disk writes fall from 99 MB to 74 MB, and on web-BerkStan from 21 MB to under 5 MB.

  • mapper_in_degree.py

    Hadoop

    Reads edges from stdin, skips comments and malformed lines, and emits the destination node with a count of 1.

  • reducer_in_degree.py

    Hadoop

    Streams sorted keys and sums counts per node. Also reused as a combiner in the optimised run to pre-aggregate on the map side.

  • mapper / reducer_histogram.py

    Hadoop

    Second job: turns node → in-degree into in-degree → number of nodes.

  • local_hadoop_fallback.py

    fallback

    Reproduces Hadoop's part-r-00000 output with a Counter and 4 MB read buffering, so the benchmark runs on machines without Hadoop.

  • metrics/runner.py

    monitor

    Launches any job as a subprocess and records system metrics at 1 Hz into timeseries.csv and summary.json.

  • main.py

    orchestrator

    Step registry (deps, experiments, validate, plots, sizes, scaling, optimised runs) with --datasets, --steps, --no-optimized and --dry-run flags.

Execution time (seconds)

Baseline runs, lower is better

  • email-EuAll · Spark13.2 s
  • email-EuAll · Hadoop2.0 s
  • web-BerkStan · Spark19.3 s
  • web-BerkStan · Hadoop9.1 s
  • LiveJournal · Spark95.2 s
  • LiveJournal · Hadoop88.0 s

Peak CPU utilisation

Spark is more CPU-hungry on every graph

  • email-EuAll · Spark63.9%
  • email-EuAll · Hadoop42.9%
  • web-BerkStan · Spark89.7%
  • web-BerkStan · Hadoop75.0%
  • LiveJournal · Spark70.2%
  • LiveJournal · Hadoop67.4%

LiveJournal: before vs optimised

Elapsed seconds on the 1.08 GB graph

  • Spark95.2 s
  • Spark (optimised)81.9 s
  • Hadoop88.0 s
  • Hadoop (optimised)82.9 s

Disk writes on LiveJournal (MB)

Baseline runs

  • Spark77.7
  • Hadoop99.4
  • Correctness: both produce the same, deterministic results. Spark recovers lost partitions from RDD lineage, while Hadoop replicates and commits each stage to disk, giving stronger durability when failures are common.
  • Execution time: Hadoop won in these single-pass tests, because MapReduce is built for large sequential reads and writes and Spark pays for job setup, stages, and shuffles.
  • CPU: Spark used more on every dataset (up to 89.7%), reflecting its CPU-intensive in-memory engine.
  • Disk: Spark read far less on the smaller graphs (0.47 MB vs 5.9 MB on email-EuAll) and wrote less on the larger ones.
  • Network: Hadoop moved almost nothing on the small graph (242 bytes total) but its shuffle grew fastest with size.
  • Hadoop outperformed Spark here, which suggests Spark lacked enough memory for optimal performance, or that a one-pass count favours sequential disk processing over iterative transformations.
  • For real graph workloads (PageRank, connected components, anything iterative) Spark is still the better choice: it keeps intermediate state in memory, while Hadoop re-reads and re-writes the graph from disk on every iteration.
  • The results match theory. MapReduce cost grows with disk I/O (O(n) per pass), and Spark's grows with shuffle complexity and CPU load, but it handles intermediate data far more efficiently.
  • Benchmarks need resource timelines, not just a stopwatch: the per-second CPU, disk and network traces explained why each engine won or lost.
  • Small inputs flatter Hadoop and penalise Spark's start-up cost; the ranking can flip as data and iteration counts grow.
  • Map-side combiners are the cheapest MapReduce optimisation: they shrink the shuffle and the intermediate files at the same time.