INT315Semester 7 • 3 CreditsElective Subjects

Cluster Computing

Cluster Computing covers Apache Spark and Scala for distributed data processing, including RDDs, Spark SQL, GraphX, Spark Streaming with Apache Kafka, and machine learning using Spark MLlib and PySpark. The course emphasizes applying these technologies to big-data processing, analytics, graph processing, and practical machine-learning workflows.

6 Units • Notes • Model Answers • MCQs
Select Course Unit (6 Units)
Active: Unit 1
Unit 1:Unit 1: Introduction to Spark
7 min read•High-Yield Unit Guide

Unit 1: Introduction to Spark

Unit 1 Exam Focus & Core Takeaways

Review the core definitions, standard architectures, and syntax patterns below. Pay special attention to sections highlighted with bullet points for your mid-term and end-term subjective questions.

Unit 1: Introduction to Apache Spark


1. Foundations of Apache Spark and Cluster Computing

Conceptual Origins and Evolution

Apache Spark is an open-source, general-purpose distributed computing engine designed for large-scale data processing. Conceived in 2009 at the University of California, Berkeley’s AMPLab by Matei Zaharia and colleagues, Spark was developed to overcome the profound architectural and performance bottlenecks of the Hadoop MapReduce computing model. Open-sourced under a BSD license in 2010 and donated to the Apache Software Foundation in 2013 (becoming a top-level Apache project in 2014), Spark revolutionized big-data processing by introducing in-memory distributed storage abstractions and dynamic acyclic computation graphs.

CODE
+-----------------------------------------------------------------------------------------+
|                                  APACHE SPARK ECOSYSTEM                                 |
+-----------------------------------------------------------------------------------------+
|   Spark SQL        |   Spark Streaming    |       MLlib         |        GraphX         |
| (DataFrames/SQL)   | (Structured Stream)  | (Machine Learning)  | (Graph Processing)    |
+--------------------+----------------------+---------------------+-----------------------+
|                                    SPARK CORE                                           |
|       (RDD Abstraction, In-Memory Computing, Task Scheduling, Lineage Fault Recovery)   |
+-----------------------------------------------------------------------------------------+
|                                 CLUSTER MANAGERS                                        |
|         Standalone         |         Hadoop YARN         |         Kubernetes           |
+-----------------------------------------------------------------------------------------+
|                                 STORAGE BACKENDS                                        |
|     HDFS     |   Amazon S3   |   Apache Kafka   |   Apache Cassandra   |   Local Disk   |
+-----------------------------------------------------------------------------------------+

Core Architecture: Driver, Cluster Manager, and Executors

Spark operates on a master-worker distributed architecture coordinated by a centralized Driver Program:

CODE
+-----------------------------------------------------------------------------------------+
|                                      DRIVER PROGRAM                                     |
|  - SparkSession / SparkContext (Entry point)                                            |
|  - High-Level Code Translation -> Logical Plan -> Physical Plan                         |
|  - Directed Acyclic Graph (DAG) Engine -> Breaks Job into Stages & Tasks                |
|  - TaskScheduler & BackendScheduler (Dispatches tasks across cluster)                   |
+-----------------------------------------------------------------------------------------+
                                             |
                                  Acquires Worker Resources
                                             v
+-----------------------------------------------------------------------------------------+
|                                     CLUSTER MANAGER                                     |
|                 (Spark Standalone / Hadoop YARN / Kubernetes)                           |
+-----------------------------------------------------------------------------------------+
                                 /                         \
                   Spawns & Monitors               Spawns & Monitors
                               /                             \
                              v                               v
+------------------------------------------+    +------------------------------------------+
|          WORKER NODE 1                   |    |          WORKER NODE 2                   |
|  +------------------------------------+  |    |  +------------------------------------+  |
|  |             EXECUTOR               |  |    |  |             EXECUTOR               |  |
|  |  - Task Thread Pool (Cores)        |  |    |  |  - Task Thread Pool (Cores)        |  |
|  |  - Block Manager (Storage Memory)  |  |    |  |  - Block Manager (Storage Memory)  |  |
|  |  - Execution Memory (Joins/Shuff)  |  |    |  |  - Execution Memory (Joins/Shuff)  |  |
|  +------------------------------------+  |    |  +------------------------------------+  |
+------------------------------------------+    +------------------------------------------+
  1. The Driver Program:
    • The master orchestrator process hosting the application entry point (SparkSession in modern Spark, wrapping the historical SparkContext, SQLContext, and HiveContext).
    • Translates user code into a Directed Acyclic Graph (DAG) of logical transformations.
    • Decomposes the DAG into physical execution Stages (separated by shuffle boundaries) and discrete unit-of-work Tasks.
    • Schedules tasks across available worker executors and tracks task completion, retries, and data locality.
  2. The Cluster Manager:
    • An external or built-in service responsible for cluster resource negotiation and allocation.
    • Supported managers: Spark Standalone Manager, Hadoop YARN, Apache Mesos (deprecated), and Cloud-native Kubernetes.
  3. Executors:
    • Dedicated JVM processes launched on worker nodes for the explicit lifecycle of the Spark application.
    • Execute task threads concurrently across assigned CPU cores.
    • House the BlockManager, providing local in-memory caching and persistent storage for cached RDDs and DataFrames.
    • Isolate execution resources (RAM and CPU) per application.

2. Limitations of Hadoop MapReduce

The MapReduce Processing Bottleneck

Hadoop MapReduce (introduced by Doug Cutting and Mike Cafarella based on Google's 2004 whitepaper) provided the first planetary-scale distributed processing paradigm on commodity hardware. However, its architecture exhibits fundamental inefficiencies that severely hinder modern analytical workloads:

CODE
MapReduce Pipeline (Disk-Bound):
[ HDFS Input ] ---> (Map Phase) ---> [ Local Disk Spill / Shuffle ] ---> (Reduce Phase) ---> [ HDFS Output ]
                                                                                                   |
                                        Next Iterative Job reads from HDFS again <-----------------+

Detailed Breakdown of MapReduce Bottlenecks

Inefficiency / BottleneckTechnical Mechanism in MapReduceSpark Architectural Resolution
Pervasive Disk I/OIntermediate map outputs are sorted and materialized to local disk; job outputs are replicated 3x to HDFS.Intermediate datasets and working state are retained directly in distributed RAM (Tungsten memory engine).
High Startup LatencyStarting JVM containers for mappers and reducers introduces 15–60 seconds of cold-start latency.Executors persist as long-running JVM daemons; tasks are spawned instantly as lightweight threads.
Rigid Two-Stage ModelAlgorithms must be shoehorned strictly into Map followed by Reduce phases.Arbitrary computation graphs (DAGs) supporting complex multistage pipelines (map, filter, join, union).
Inefficient Iterative WorkloadsMachine learning (k-means, PageRank) requires iterating over data 20–100 times; each iteration re-reads HDFS blocks from disk.Iterative datasets are cached in memory across loop iterations via cache() or persist(), achieving 10x–100x speedups.
Lack of Interactive QueryingEvery query requires full MapReduce job submission, scheduling, and HDFS writes, making ad-hoc exploration impossible.Interactive shells (spark-shell, pyspark, Spark SQL) return query results within milliseconds.
Absence of Native StreamingProcesses bounded static files only; stream ingestion requires separate bolt-on engines (e.g., Apache Storm).Unified engine supporting batch, streaming (Structured Streaming), SQL, and ML in one unified framework.

Code Comparison: Word Count

Hadoop MapReduce Word Count (Verbose Java Abstraction):

Requires separate TokenizerMapper, IntSumReducer, job configuration, output key/value specifications, and file system setup spanning over 50 lines of boilerplate code.

Apache Spark Word Count (Declarative, Functional):

scala
// Scala Spark API
val textFile = spark.read.textFile("hdfs://cluster/data/sample.txt")
val counts = textFile.flatMap(line => line.split(" "))
                     .filter(word => word.nonEmpty)
                     .groupBy("value")
                     .count()
counts.write.parquet("hdfs://cluster/output/word_counts")

3. Data-Processing Models: Batch vs. Real-Time Stream Analytics

Bounded vs. Unbounded Data

Enterprise analytical systems operate across two fundamental temporal domains:

CODE
Batch Processing (Bounded / Historical):
[ Event 1 ] [ Event 2 ] [ Event 3 ] ... [ Event N ]  ---> [ Complete Dataset ] ---> Processing Engine ---> Results
(Data is static, finite, and fully accumulated prior to job start)

Stream Processing (Unbounded / Continuous):
... ---> [ Event 4 ] ---> [ Event 3 ] ---> [ Event 2 ] ---> [ Event 1 ]  ---> Processing Engine ---> Continuous Output
(Data is dynamic, infinite, and arrives asynchronously with variable velocity)

Comparative Analysis: Batch vs. Real-Time Stream Analytics

Architectural MetricBatch AnalyticsReal-Time Stream Analytics
Data NatureBounded, finite dataset with a defined start and end.Unbounded, infinite continuous stream of asynchronous events.
Latency ExpectationHigh latency: minutes, hours, or overnight batches.Low latency: sub-second, milliseconds, or small micro-batches.
Data IngestionBulk file loads from HDFS, Data Lakes, or Cloud Storage.Real-time messaging brokers (Apache Kafka, AWS Kinesis, RabbitMQ).
Processing ParadigmJob reads snapshot, processes data, writes output, terminates.Long-running daemon listening continuously for incoming tuples.
Temporal ConceptIngestion timestamp or processing date.Event-Time (when event occurred) vs Processing-Time.
Fault RecoverySimple re-execution of the failed job on the bounded input.Checkpointing, write-ahead logs (WAL), and stateful replaying.
Primary Use CasesMonthly payroll, historical reporting, end-of-day billing.Real-time fraud detection, server telemetry, algorithmic trading.

4. Modern Processing Techniques: In-Memory Computing and Stream Ingestion

In-Memory Distributed Computing

Spark’s defining performance advantage stems from its ability to retain distributed partitions in RAM across cluster nodes. In-memory computing does not mean data must fit entirely in RAM; Spark utilizes an adaptive Storage and Execution Memory architecture:

CODE
+-----------------------------------------------------------------------------------------+
|                               JVM EXECUTOR HEAP MEMORY                                  |
+-----------------------------------------------------------------------------------------+
|  Reserved Memory (300 MB)                                                               |
+-----------------------------------------------------------------------------------------+
|  User Memory (40%) - Custom data structures, internal metadata, user UDFs              |
+-----------------------------------------------------------------------------------------+
|  Spark Unified Memory (60% of (Heap - 300MB)):                                          |
|                                                                                         |
|  [ Storage Memory ] <=========== Dynamic Borrowing Boundary ===========> [ Execution ]  |
|  - Cached RDDs, DataFrames                                               - Shuffles,    |
|  - Broadcast variables                                                   - Joins, Sorts |
+-----------------------------------------------------------------------------------------+

Memory Eviction and Persistence Levels (persist()):

When memory pressure occurs, Spark automatically spills excess partitions to disk or evicts cached data according to the configured StorageLevel:

  • StorageLevel.MEMORY_ONLY: Partitions stored as deserialized Java objects in memory. If memory is insufficient, non-fitting partitions are recomputed on-demand via lineage.
  • StorageLevel.MEMORY_AND_DISK: Partitions that exceed RAM capacity are serialized and spilled directly to local disk rather than recomputed.
  • StorageLevel.MEMORY_ONLY_SER: Stores partitions as serialized byte buffers (one byte array per partition), reducing memory footprint by 2x–5x at the expense of slight CPU deserialization overhead.
  • StorageLevel.OFF_HEAP: Allocates memory outside the JVM garbage collector using Project Tungsten, eliminating GC pause times.

Stream Processing Architecture

Spark Structured Streaming unifies stream and batch semantics by treating a live data stream as an Unbounded, Incrementally Appended Table:

CODE
                              [ Incoming Stream of Kafka Records ]
                                                |
                                                v
+-----------------------------------------------------------------------------------------+
|                       UNBOUNDED TABLE (New rows continuously added)                     |
+-----------------------------------------------------------------------------------------+
| Time = 1 | {"user": "alice", "action": "login",  "timestamp": "10:00:01"}               |
| Time = 2 | {"user": "bob",   "action": "click",  "timestamp": "10:00:02"}               |
| Time = 3 | {"user": "alice", "action": "pay",    "timestamp": "10:00:05"}               |
+-----------------------------------------------------------------------------------------+
                                                |
                                     Continuous Incremental Query
                                                |
                                                v
+-----------------------------------------------------------------------------------------+
|                                    RESULT TABLE                                         |
|                 (Updated via Complete, Append, or Update Mode)                          |
+-----------------------------------------------------------------------------------------+

5. The Apache Spark Unified Platform Components

Architectural Integration of Spark Libraries

Unlike legacy big-data ecosystems that required integrating distinct, incompatible systems (Hadoop for batch, Storm for streaming, Mahout for ML), Spark provides a single, unified computing engine:

CODE
+-----------------------------------------------------------------------------------------+
|                                 APPLICATION LAYER                                       |
|  +------------------+  +------------------+  +------------------+  +------------------+ |
|  |    Spark SQL     |  |    Structured    |  |      MLlib       |  |      GraphX      | |
|  |  (DataFrames /   |  |    Streaming     |  |    (Pipelines,   |  |  (Graph-Parallel | |
|  |   Catalyst/Tung) |  | (Unbounded Tabl) |  |   Classification)|  |    Pregel API)   | |
|  +------------------+  +------------------+  +------------------+  +------------------+ |
+-----------------------------------------------------------------------------------------+
|                                    SPARK CORE                                           |
|       - Memory Manager     - Task Scheduler      - Shuffle Service     - Lineage DAG    |
+-----------------------------------------------------------------------------------------+
  1. Spark Core:
    • Manages fundamental distributed computing routines: task dispatching, inter-node shuffle data transfers, memory management, and I/O abstraction.
    • Houses the low-level Resilient Distributed Dataset (RDD) abstraction.
  2. Spark SQL & The Catalyst Optimizer:
    • The primary interface for working with structured data via DataFrames and Datasets.
    • Features the Catalyst Query Optimizer, which performs rule-based and cost-based optimizations: predicate pushdown, projection pruning, constant folding, and hash join optimization.
    • Features Project Tungsten, generating compact off-heap binary memory layouts and compiling physical plans into whole-stage Java bytecode.
  3. Structured Streaming:
    • Enables writing streaming queries using identical DataFrame APIs and SQL syntax as static batch queries.
    • Ensures end-to-end exactly-once fault-tolerant processing using checkpointing and write-ahead logs.
  4. MLlib (Machine Learning Library):
    • Distributed algorithms for classification, regression, clustering, collaborative filtering, and dimensionality reduction.
    • Provides scikit-learn-style Pipeline APIs for chaining feature extraction, transformations, and model evaluation.
  5. GraphX:
    • Distributed graph processing API extending RDDs to represent directed multigraphs with vertices and edges, supporting Pregel message-passing algorithms.

6. Standalone Spark Setup and Cluster Deployment

Deployment Topology

A Spark cluster can be provisioned in several operational modes:

  • Local Mode (local[*]): Driver and executor run within a single JVM process on a workstation, allocating thread pools matching available logical CPU cores. Ideal for development and automated unit testing.
  • Spark Standalone Mode: Spark’s native built-in cluster manager consisting of a Master daemon and multiple Worker daemons.
  • Hadoop YARN Mode: Deploys Spark as an application master on top of enterprise Hadoop clusters.
  • Kubernetes Mode: Dynamically schedules Spark driver and executor pods within Kubernetes namespaces.

Standalone Installation and Service Lifecycle

  1. Prerequisites Verification:
    Ensure Java Development Kit (JDK 8, 11, or 17) is installed and available:
    bash
    java -version
    
  2. Environment Variable Configuration:
    Configure $SPARK_HOME and update the system $PATH in .bashrc or .zshrc:
    bash
    export SPARK_HOME="/opt/spark"
    export PATH="$SPARK_HOME/bin:$SPARK_HOME/sbin:$PATH"
    
  3. Starting the Native Standalone Master:
    bash
    # Starts the Master daemon process
    $SPARK_HOME/sbin/start-master.sh
    
    • Master Web UI: Accessible at http://localhost:8080.
    • Master RPC URL: Displayed in the UI (e.g., spark://master-node:7077).
  4. Attaching Worker Daemons:
    bash
    # Connects worker daemon allocating local CPU cores and RAM to the master
    $SPARK_HOME/sbin/start-worker.sh spark://master-node:7077
    
  5. Submitting Applications via spark-submit:
    The standard production submission CLI:
    bash
    spark-submit \
      --class org.example.LogAnalytics \
      --master spark://master-node:7077 \
      --deploy-mode cluster \
      --executor-memory 4G \
      --total-executor-cores 8 \
      /opt/apps/log-analytics.jar
    
  6. Web Interfaces for Operational Monitoring:
    • Standalone Master UI: Port 8080 (Cluster overview, worker health, active/completed applications).
    • Active Application Spark UI: Port 4040 (DAG visualization, stage durations, task breakdown, executor storage, shuffle read/write).
    • History Server: Port 18080 (Post-mortem inspection of completed application logs).

7. Ecosystem Perspective: Apache Spark vs. Hadoop Ecosystem

Compute Engine vs. Full Distributed Stack

A frequent misconception in big-data engineering is viewing Apache Spark and Apache Hadoop as mutually exclusive competitors. In reality, they serve complementary layers of the distributed stack:

CODE
+-----------------------------------------------------------------------------------------+
|                                    BIG DATA ECOSYSTEM                                   |
+-----------------------------------------------------------------------------------------+
| COMPUTE LAYER:       [ Apache Spark ] (Fast, in-memory)  OR  [ MapReduce ] (Disk-batch) |
+-----------------------------------------------------------------------------------------+
| RESOURCE SCHEDULER:  [ YARN (Hadoop) ]  OR  [ Kubernetes ]  OR  [ Standalone ]          |
+-----------------------------------------------------------------------------------------+
| STORAGE LAYER:       [ HDFS (Hadoop) ]  OR  [ Amazon S3 ]   OR  [ Apache Cassandra ]    |
+-----------------------------------------------------------------------------------------+

Comprehensive Comparison: Spark vs. Hadoop Ecosystem

Architectural LayerApache SparkApache Hadoop
Primary ScopeHigh-performance distributed computing and processing engine.Comprehensive distributed ecosystem encompassing storage, resource scheduling, and batch compute.
Storage LayerHas no native distributed storage. Relies on external file systems (HDFS, S3, ADLS, GCS).Includes Hadoop Distributed File System (HDFS) with 3x block replication.
Resource SchedulingCan run on Standalone, YARN, Kubernetes, or Mesos.Includes YARN (Yet Another Resource Negotiator).
Execution ArchitectureIn-memory Directed Acyclic Graph (DAG) pipelining; threads within JVM executors.Disk-backed two-stage MapReduce; launches isolated JVM processes per task.
Fault Tolerance StrategyLineage Graphs: Recomputes lost partitions on-demand without data replication in memory.Data Replication: Persists three physical copies of data blocks across distinct cluster racks.
Primary Sweet SpotIterative algorithms, machine learning, interactive SQL, real-time streaming.Massive petabyte-scale batch processing where RAM is constrained and execution time is non-critical.