Apache Spark is a distributed engine for processing data at scale across a cluster of machines. One API covers batch ETL, SQL analytics, streaming, and machine learning, and the same DataFrame code runs on a laptop or a thousand-node cluster. Spark replaced MapReduce as the default heavy-lifting engine because it keeps working data in memory between steps and optimizes whole queries rather than running rigid map-then-reduce stages.
How work is distributed
A Spark application has a driver that plans the job and executors that do the work. Data is split into partitions; each executor processes partitions in parallel as tasks. The unit underneath the DataFrame API is the RDD - a partitioned, immutable collection with a lineage of how it was derived. Lineage is how Spark recovers from a lost executor: it recomputes only the missing partitions from their parents instead of restarting the job.
Transformations, actions, and lazy evaluation
Spark operations are either transformations (filter, join, groupBy) or actions (count, write, collect). Transformations are lazy - they build a plan but run nothing. Only an action triggers execution. This lets the optimizer see the entire chain at once and rearrange it: push filters down to the scan, combine projections, and prune columns before reading. The practical implication is that nothing happens until you call an action, and calling collect() on a large result pulls every row to the driver and will run it out of memory - write to storage instead.
orders = spark.read.parquet("s3://lake/orders/")
daily = (orders
.filter(orders.status == "paid")
.groupBy("order_date", "region")
.agg(sum("amount").alias("revenue")))
daily.write.mode("overwrite").parquet("s3://lake/daily_revenue/") # action runs the plan
Catalyst and Tungsten
DataFrame and SQL queries go through Catalyst, Spark's query optimizer, which applies rule-based and cost-based rewrites and picks join strategies. Execution runs on Tungsten, which manages memory off-heap and generates compact bytecode to avoid per-row interpretation overhead. The headline consequence: typed RDD code bypasses Catalyst, so prefer the DataFrame/SQL API - it is both higher level and usually faster because the optimizer can see it.
The shuffle: the thing that decides performance
Operations that regroup data across partitions - joins, groupBy, distinct, wide aggregations - trigger a shuffle: executors write intermediate files and exchange them over the network. Shuffles are the dominant cost in most Spark jobs and the usual source of slowness and out-of-memory failures. Two failure modes recur:
- Skew - one key holds far more rows than the rest, so one task runs long after the others finish. Adaptive Query Execution can split skewed partitions automatically; salting the key is the manual fix.
- Small/large join mismatch - joining a big table to a small one with a full shuffle is wasteful. A broadcast join ships the small side to every executor and skips the shuffle entirely.
from pyspark.sql.functions import broadcast
result = big_fact.join(broadcast(small_dim), "dim_id") # avoids a shuffle of the fact table
Adaptive Query Execution
AQE (on by default in Spark 3+) re-optimizes the plan at runtime using actual statistics: it coalesces too-many small shuffle partitions, switches a sort-merge join to a broadcast join when a side turns out small, and splits skewed partitions. It removes a lot of the manual tuning (like hand-setting spark.sql.shuffle.partitions) that older Spark required.
Structured Streaming
The same DataFrame API runs on unbounded data through Structured Streaming, which models a stream as a table that grows over time. It supports event-time windows, watermarks for late data, and exactly-once sinks to systems like Delta Lake or Iceberg via checkpointing. Code written for a batch job is largely reusable for the streaming version.
When to use it - and when not
- Good fit: datasets too big for one machine, multi-source joins, heavy transformations, and pipelines that mix SQL, Python, and ML on the same data.
- Overkill: data that fits comfortably in memory on one box - DuckDB, Polars, or plain pandas will be faster and far simpler, with none of the cluster overhead.
- Wrong tool: low-latency record lookups or sub-second serving - Spark is a throughput engine, not a key-value store.
Where you run it
Spark runs on YARN, Kubernetes, or standalone, and is the engine behind managed platforms like Databricks, EMR, and Dataproc. Managed offerings handle cluster provisioning, autoscaling, and the storage connectors, which is why most teams consume Spark through one rather than operating a cluster themselves. Whatever the platform, the performance levers are the same: minimize shuffles, broadcast small joins, watch for skew, and avoid pulling results to the driver.