Kokil Thapa - Professional Web Developer in Nepal
Freelancer Web Developer in Nepal with 15+ Years of Experience

Kokil Thapa is an experienced full-stack web developer focused on building fast, secure, and scalable web applications. He helps businesses and individuals create SEO-friendly, user-focused digital platforms designed for long-term growth.

Apache Spark Fundamentals

By Kokil Thapa | Last reviewed: September 2026

Apache Spark Fundamentals matter when your dataset outgrows a single machine and a cron job on MySQL starts timing out. Spark is an open-source, distributed analytics engine built for large-scale batch processing, SQL queries, streaming, and machine learning. It keeps working sets in memory across a cluster, which cuts disk I/O compared with classic MapReduce. This guide walks through architecture, core APIs, and a first job you can run today—whether you feed a enterprise application dashboard or an ETL pipeline beside Apache Kafka.

What is Apache Spark and why does it matter in 2026?

Apache Spark is a unified engine for data engineering and analytics. It runs on a cluster of machines and splits work into tasks that execute in parallel. You write logic once; Spark handles partitioning, scheduling, and fault recovery.

Spark 4.x (the current stable line as of 2026) builds on a decade of production use at banks, ad networks, and SaaS platforms. It supports Python (PySpark), Scala, Java, R, and SQL through Spark SQL. You can deploy it on YARN, Kubernetes, or standalone clusters.

On real client projects, Spark rarely sits inside a Laravel request cycle. It sits behind one. Nightly jobs aggregate clickstream or order data. Results land in PostgreSQL or Redis. Your web app reads precomputed summaries. That separation keeps page loads fast while analytics stay honest at scale.

Apache Spark Cluster ArchitectureDriver ProgramSparkContext / SparkSessionCluster ManagerK8s, YARN, or StandaloneExecutor 1Tasks + CacheExecutor 2Tasks + CacheExecutor 3Tasks + CacheHDFS, S3, JDBC, Kafka — distributed data sources
Apache Spark Fundamentals: the driver schedules work; executors run tasks on partitioned data from external storage.

Spark matters because it replaces slow, multi-stage disk pipelines with in-memory DAG execution. A single Spark job can chain map, filter, join, and aggregate steps without writing intermediate files to HDFS after every stage. For teams already running distributed storage, Spark is the compute layer that makes that storage useful at query speed.

How does Apache Spark architecture distribute work?

Every Spark application has one driver process and one or more executor processes. The driver holds your SparkSession, builds the logical plan, and asks the cluster manager for resources. Executors run on worker nodes and perform the actual computation.

Driver responsibilities

The driver converts your code into a directed acyclic graph (DAG) of stages. It ships tasks to executors, tracks completion, and handles failures by retrying lost tasks on other nodes. If the driver dies, the entire application stops.

Executor responsibilities

Each executor runs multiple tasks in parallel threads. Executors cache partitions in memory or spill to disk when RAM is tight. They report heartbeats to the driver so the scheduler knows which nodes are healthy.

Cluster managers

Spark does not manage its own machines by default. It delegates to Kubernetes, Apache YARN, or its built-in standalone scheduler. On a small team budget, a three-node standalone cluster on Ubuntu 24 with 16 GB RAM per node handles many batch jobs. Larger teams typically move to Kubernetes for elasticity.

I've seen production analytics sit on the same shared EC2 infrastructure as web apps. Spark workers get dedicated instances. The web tier never shares CPU with a shuffle-heavy join. That isolation prevents a nightly ETL run from slowing checkout on a Laravel eCommerce platform.

What are RDDs, DataFrames, and Datasets in Spark?

Spark exposes three main programming models. Most new code uses DataFrames, but understanding RDDs explains how Spark thinks about data.

Resilient Distributed Datasets (RDDs)

An RDD is an immutable, partitioned collection of records spread across the cluster. Spark tracks lineage: if a partition is lost, Spark rebuilds it from the parent RDD and the transformation that created it. RDDs offer fine-grained control but verbose syntax.

DataFrames

DataFrames are distributed tables with named columns and a schema. Spark SQL's Catalyst optimizer rewrites queries for efficiency. PySpark and Spark SQL are the default choice for ETL, reporting, and ad hoc analytics in 2026.

Datasets (Scala and Java)

Datasets combine RDD type safety with DataFrame optimization. They shine in strongly typed Scala services. Python users typically stay with DataFrames because PySpark lacks full Dataset support.

Lazy Evaluation DAG PipelineSourceCSV / Parquetfilter()TransformationgroupBy()Transformationagg()Transformationcount() ActionTriggers executionCatalyst Optimizer merges stages before shuffleNo work runs until an action is called
Spark builds a logical plan from transformations; only actions trigger distributed execution across the cluster.

Choose DataFrames unless you need low-level RDD operations. The optimizer understands column pruning and predicate pushdown. Your JSON payloads from an API export become a readable schema with spark.read.json() in one line.

How do Spark transformations and actions differ?

This distinction is the heart of Apache Spark Fundamentals. Transformations define what to compute. Actions ask Spark to compute it now.

Transformations (lazy)

Common transformations include map, filter, flatMap, join, groupByKey, and select. They return a new RDD or DataFrame. Spark records them in the lineage graph but does not run them yet.

Actions (eager)

Actions include count, collect, take, save, and show. They send the DAG to the scheduler, launch stages, shuffle data across the network when needed, and return a result or write output.

A common mistake is chaining ten transformations inside a loop and calling collect() each iteration. Every action re-reads and re-shuffles data. Batch your logic and write once to Parquet instead.

CriteriaApache SparkHadoop MapReduce
Execution modelIn-memory DAG with optional disk spillDisk-heavy map-shuffle-reduce stages
API surfaceSQL, DataFrame, RDD, MLlib, StreamingMap and reduce functions only
Iterative algorithmsFast — cached partitions reusedSlow — rewrite to HDFS each round
Latency for small jobsSeconds (JVM and scheduler overhead)Minutes (job startup cost)
Best fitETL, interactive SQL, streaming, MLSimple one-pass batch on cold storage
Ops complexityCluster tuning, shuffle, memory managementSimpler per job, slower overall
Spark vs MapReduce WorkloadsApache SparkIn-memory iterationsUnified SQL + streaming10–100x faster on loopsMapReduceDisk between stagesSingle-pass batch onlyMature, simple opsVerdict: Spark for analytics; MapReduce for legacy one-off batchBoth read from HDFS and S3 — Spark adds speed, not storagePair with Kafka and Airflow for full pipelines
Apache Spark Fundamentals favor iterative and multi-stage analytics; MapReduce remains viable for simple cold-storage batch jobs.

Shuffle operations—groupBy, join, repartition—move data across the network. They dominate runtime on large joins. Use broadcast joins for small lookup tables. Filter early to shrink partitions before a shuffle. Monitor shuffle read and write bytes in the Spark UI on port 4040 during development.

How do you run your first Apache Spark job?

Start local before you rent a cluster. Install Java 17 or 21, then download Spark from the official Apache site. PySpark is the fastest path for most teams.

  1. Install PySpark: pip install pyspark
  2. Start a local session with SparkSession.builder.master("local[*]").getOrCreate()
  3. Load a CSV or Parquet file with spark.read
  4. Transform with DataFrame API methods
  5. Trigger an action such as df.write.parquet("output/")
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, sum as spark_sum

spark = (
    SparkSession.builder
    .appName("daily-orders-summary")
    .master("local[*]")
    .config("spark.sql.shuffle.partitions", "8")
    .getOrCreate()
)

orders = spark.read.parquet("s3a://analytics/orders/")

summary = (
    orders
    .filter(col("status") == "completed")
    .groupBy("region")
    .agg(spark_sum("amount_npr").alias("total_npr"))
)

summary.write.mode("overwrite").jdbc(
    url="jdbc:postgresql://db.internal:5432/analytics",
    table="daily_region_totals",
    properties={"user": "etl", "password": "secret", "driver": "org.postgresql.Driver"}
)

spark.stop()

Run the same job on a cluster by changing master to yarn or k8s://https://... and submitting with spark-submit. Package dependencies with --packages for JDBC drivers or Kafka connectors.

spark-submit on a cluster

spark-submit \
  --master yarn \
  --deploy-mode cluster \
  --num-executors 4 \
  --executor-memory 4G \
  --executor-cores 2 \
  daily_orders_summary.py

Schedule recurring jobs with Apache Airflow. Airflow triggers spark-submit, checks exit codes, and retries on failure. That pattern mirrors how I wire GitLab CI for web deploys: one orchestrator, idempotent tasks, alerts on red builds.

Structured Streaming basics

Spark Structured Streaming treats a live Kafka topic like an unbounded table. You write the same DataFrame logic as batch. The engine handles micro-batches and checkpointing to durable storage.

stream = (
    spark.readStream
    .format("kafka")
    .option("kafka.bootstrap.servers", "broker:9092")
    .option("subscribe", "pageviews")
    .load()
)

counts = stream.groupBy("page").count()

query = (
    counts.writeStream
    .outputMode("complete")
    .format("parquet")
    .option("path", "s3a://analytics/pageview_counts/")
    .option("checkpointLocation", "s3a://analytics/checkpoints/pageviews/")
    .start()
)

query.awaitTermination()

Checkpoint paths must survive restarts. Losing a checkpoint forces a full replay from the Kafka offset or earliest available message.

Where does Apache Spark fit in a modern data stack?

Spark sits between raw ingestion and consumption layers. Kafka or Fluent Bit ingest events. Spark transforms and aggregates. PostgreSQL, Elasticsearch, or a REST API serves results to dashboards and mobile clients.

For a directory platform like Gulfbizlist, batch Spark jobs could compute trending listings from search logs. The Laravel app reads a materialized view refreshed hourly. Users see fresh rankings without hammering the transactional database.

Spark also pairs with monitoring. Export JVM metrics to Prometheus. Alert on executor memory pressure or task duration p99 spikes. Treat Spark like any production service: logs, metrics, runbooks.

Spark in a Web-App Data StackKafka / LogsEvent ingestSpark ETLBatch + streamPostgreSQLSummary tablesLaravel APIFast readsCommon Gotchacollect() on big DataFramesBest PracticeWrite to JDBC or ParquetLazy transforms until one actionAirflow schedules spark-submit nightly
Apache Spark Fundamentals in practice: aggregate offline, serve precomputed data through your web tier for low latency.

Security belongs in the design. Run Spark on private subnets. Use IAM roles for S3 access instead of hard-coded keys. Encrypt data in transit between nodes when compliance requires it. For teams exploring ML features, Spark MLlib handles feature pipelines before models export to a dedicated serving layer via AI integration services.

Tuning checklist for production:

  • Set spark.sql.shuffle.partitions to roughly three times total executor cores.
  • Enable dynamic allocation on YARN or K8s so idle executors release memory.
  • Prefer Parquet over CSV for column pruning and smaller I/O.
  • Cache only datasets reused across multiple actions; unpersist when done.
  • Size executors with headroom for shuffle buffers—4 GB heap plus overhead is a common starting point.

Official docs at spark.apache.org stay the authoritative reference for configuration keys and API changes. The Apache Software Foundation also publishes migration notes between major Spark versions. Read those before upgrading a long-running cluster.

If your team lacks dedicated data engineers, start with managed offerings. Databricks, AWS EMR, and Google Dataproc handle cluster provisioning. You pay a premium but skip weeks of Linux cluster administration. For a Kathmandu startup processing under 100 GB daily, a single local[*] job on a fat VM plus Airflow may suffice for months.

Connect Spark output to your app through stable contracts. Version your summary tables. Document column meanings. Apply the same discipline you would to a public GraphQL API schema. Downstream Laravel models should never depend on ad hoc column renames.

Rate-limit and cache API responses that read aggregated data. Spark computed the hard numbers overnight. Your web tier should not re-aggregate them per request. That separation is how API rate limiting stays effective under traffic spikes.

Before production cutover, run load and correctness tests on sample partitions. Compare Spark output row counts and checksums against a trusted SQL baseline. One off-by-one join key can silently double revenue figures in a dashboard.

Key Takeaways

  • Apache Spark Fundamentals center on a driver, cluster manager, and executors running lazy transformations until an action triggers execution.
  • Use DataFrames and Spark SQL for new projects; reserve RDDs for legacy code or custom partitioning needs.
  • Minimize shuffles, avoid collect() on large datasets, and write results to JDBC or Parquet instead of pulling data to the driver.
  • Pair Spark with Kafka for ingest and Airflow for scheduling; keep transactional web databases on pre-aggregated summaries.
  • Monitor the Spark UI, tune shuffle partitions, and read official Apache docs before upgrading cluster versions.
  • Managed EMR or Databricks reduces ops burden for small teams that need analytics without a dedicated platform group.

People Also Ask

Is Apache Spark hard to learn if I know SQL?

If you know SQL, Spark SQL is the fastest entry point. You can spark.read.parquet(...).createOrReplaceTempView("orders") and run familiar SELECT queries. PySpark adds programmatic control for complex ETL. Most data analysts become productive within a few days of hands-on practice.

Do I still need Hadoop to run Spark?

No. Spark reads from S3, Azure Blob, GCS, JDBC, and local files without Hadoop. YARN on a Hadoop cluster is one deployment option, not a requirement. Kubernetes and standalone mode are equally valid in 2026.

What is the difference between Spark Streaming and Structured Streaming?

Spark Streaming (DStreams) is the legacy micro-batch API based on RDDs. Structured Streaming is the modern engine built on DataFrames with exactly-once semantics and unified batch/stream code. New projects should use Structured Streaming exclusively.

How much memory does a Spark cluster need?

Rule of thumb: hold one-third to one-half of your active working set in executor memory across the cluster. A 50 GB daily batch with heavy joins might need 64–128 GB total executor RAM split across four nodes. Start smaller on local mode, profile spill metrics, then scale.

Build analytics pipelines that feed your applications

Apache Spark Fundamentals give you the vocabulary to design batch and streaming jobs that scale beyond a single database server. Master lazy evaluation, respect shuffle costs, and keep heavy compute off your request path. When you need help connecting analytics output to a Laravel portal, eCommerce dashboard, or custom software platform, the same engineering discipline applies: stable schemas, tested pipelines, and production monitoring from day one. Review the about page for background on full-stack delivery, or contact us to discuss your data and application architecture.

Frequently Asked Questions

Apache Spark is a unified, open-source distributed analytics engine for large-scale batch processing, SQL queries, streaming, and machine learning. Spark 4.x runs work in parallel across a cluster, keeps working sets in memory, and chains multi-stage jobs without writing intermediate files after every step like classic MapReduce. It matters when datasets outgrow one machine and cron jobs on MySQL start timing out. Banks, ad networks, and SaaS platforms rely on it for honest analytics at scale.

Every Spark application has one driver and one or more executors. The driver holds the SparkSession, builds a DAG of stages, and asks a cluster manager for resources. Executors on worker nodes run tasks in parallel threads, cache partitions in memory, and spill to disk when RAM is tight. Spark delegates cluster management to Kubernetes, Apache YARN, or standalone mode. If the driver dies, the entire application stops; lost partitions rebuild from lineage.

RDDs are immutable, partitioned record collections with lineage-based fault recovery, but verbose syntax. DataFrames are distributed tables with named columns and a schema; Spark SQL's Catalyst optimizer rewrites them efficiently. Datasets combine RDD type safety with DataFrame optimization in Scala and Java. PySpark users typically stay with DataFrames because Dataset support is limited in Python. For new ETL and reporting in 2026, DataFrames and Spark SQL are the default unless you need low-level RDD control.

Transformations such as map, filter, join, and select define what to compute. They return a new RDD or DataFrame and stay lazy—Spark records them in the lineage graph without running them. Actions such as count, collect, take, save, and show trigger execution: the scheduler launches stages, shuffles data when needed, and returns results or writes output. A common mistake is calling collect() inside a loop after each transformation, which re-reads and re-shuffles data every time. Batch logic and write once to Parquet instead.

Spark uses an in-memory DAG with optional disk spill, while MapReduce relies on disk-heavy map-shuffle-reduce stages. Spark exposes SQL, DataFrames, RDDs, MLlib, and streaming; MapReduce offers only map and reduce functions. Iterative algorithms run fast on Spark because cached partitions reuse across rounds; MapReduce rewrites to HDFS each iteration. Small jobs finish in seconds on Spark versus minutes on MapReduce due to JVM startup overhead. MapReduce remains viable for simple one-pass batch on cold storage; Spark fits ETL, interactive SQL, streaming, and ML.

Install Java 17 or 21, then pip install pyspark. Start locally with SparkSession.builder.master("local[]").getOrCreate(). Load data via spark.read from CSV or Parquet, transform with DataFrame methods, and trigger an action such as df.write.parquet("output/") or a JDBC write to PostgreSQL. Set spark.sql.shuffle.partitions for local testing. On a cluster, change master to yarn or k8s:// and submit with spark-submit, passing --num-executors, --executor-memory, and --packages for JDBC drivers. Schedule recurring runs with Apache Airflow triggering spark-submit and checking exit codes.

Structured Streaming treats a live Kafka topic like an unbounded table. You write the same DataFrame logic as batch; the engine handles micro-batches and checkpointing to durable storage. A typical flow reads from kafka.bootstrap.servers, applies groupBy and count, then writesStream to Parquet with a checkpointLocation. Checkpoint paths must survive restarts—losing one forces a full replay from the Kafka offset or earliest available message. New projects should use Structured Streaming exclusively; legacy Spark Streaming (DStreams) based on RDDs is deprecated in favor of this unified batch-and-stream model.

Spark sits between raw ingestion and consumption. Kafka or Fluent Bit ingest events; Spark transforms and aggregates overnight; PostgreSQL, Elasticsearch, Redis, or a REST API serves precomputed results to dashboards and Laravel apps. Your web tier reads materialized summaries, not raw clickstream tables. That separation keeps page loads fast while analytics stay accurate at scale. I've wired this pattern on production systems: Spark workers on dedicated EC2 instances so shuffle-heavy ETL never shares CPU with checkout on an eCommerce platform.

Spark SQL is the fastest entry point for SQL users. Load Parquet with spark.read, call createOrReplaceTempView("orders"), and run familiar SELECT queries. PySpark adds programmatic control for complex ETL pipelines. Most analysts become productive within a few days of hands-on practice.

No. Spark reads from S3, Azure Blob, GCS, JDBC, and local files without Hadoop. YARN on a Hadoop cluster is one deployment option, not a requirement.

Spark Streaming (DStreams) is the legacy micro-batch API built on RDDs. Structured Streaming is the modern engine on DataFrames with exactly-once semantics and unified batch-and-stream code. You write the same DataFrame transformations for both bounded batch files and unbounded Kafka topics. New projects in 2026 should use Structured Streaming exclusively. DStreams lack the Catalyst optimizer, schema enforcement, and checkpoint model that make production streaming pipelines maintainable alongside your batch ETL jobs.

Hold one-third to one-half of your active working set in executor memory across the cluster. A 50 GB daily batch with heavy joins often needs 64–128 GB total executor RAM split across four nodes.

Set spark.sql.shuffle.partitions to roughly three times total executor cores. Enable dynamic allocation on YARN or Kubernetes so idle executors release memory. Prefer Parquet over CSV for column pruning and smaller I/O. Cache only datasets reused across multiple actions, then unpersist when done. Size executors with headroom for shuffle buffers—4 GB heap plus overhead is a common starting point. Use broadcast joins for small lookup tables and filter early before shuffles. Monitor shuffle read and write bytes in the Spark UI on port 4040 during development.

Run Spark on private subnets, not exposed to the public internet. Use IAM roles for S3 access instead of hard-coded keys in job configs or environment files. Encrypt data in transit between nodes when compliance requires it. Treat Spark like any production service: export JVM metrics to Prometheus, alert on executor memory pressure, and maintain runbooks. JDBC credentials passed to spark-submit should come from secrets management, not committed scripts. The same discipline you apply to Laravel .env files applies to ETL job configuration.

Managed offerings like Databricks, AWS EMR, and Google Dataproc handle cluster provisioning and reduce Linux administration, but you pay a premium. A three-node standalone cluster on Ubuntu 24 with 16 GB RAM per node handles many batch jobs on a small budget. For a Kathmandu startup processing under 100 GB daily, a single local[] job on a fat VM plus Airflow may suffice for months before cluster complexity pays off. If your team lacks dedicated data engineers, managed Spark skips weeks of ops work. Self-hosted makes sense once you need isolation, predictable costs, or custom tuning at scale.

Share this article

0 Comments

Leave a comment

Your email is not published. Comments appear once they have been read. Sign in to have your details filled in.

Quick Contact Options
Choose how you want to connect me: