Apache Spark
Apache Spark is an open-source engine for processing large datasets across a cluster of machines. You describe a computation ("read last week's click logs, keep the US traffic, count clicks per ad, join with the ad catalog, write the result as Parquet") and Spark splits it into thousands of small tasks, runs them in parallel on many machines, moves data between them where it has to, and retries the pieces that fail.
A few properties make Spark what it is:
- General-purpose batch engine: Spark runs arbitrary multi-step computations (filters, joins, aggregations, machine learning) as a graph of steps, not just the single map-then-reduce shape that Hadoop MapReduce allowed.
- In-memory where it helps: intermediate results stay in executor memory between steps instead of being written to a distributed file system after every step. That made Spark 10-100x faster than MapReduce on iterative workloads, and it's why Spark replaced MapReduce for most new work.
- Lazy and optimized: nothing runs until you ask for a result. Spark first builds a plan for the whole computation, and its SQL optimizer rewrites that plan (pushing filters down, choosing join strategies, pruning columns) before running it.
- Fault tolerant through lineage: Spark remembers how each piece of data was computed. If a machine dies, it recomputes only the lost pieces from their inputs, rather than keeping replicas of everything.
- One engine, several workloads: the same engine serves SQL queries, batch ETL, streaming (Structured Streaming), machine learning (MLlib) and graph processing.
Spark comes up in interviews whenever a design has a batch or analytics component: nightly aggregation for an ad click aggregator, rebuilding a search index, computing recommendations, reconciling payments, backfilling a new metric over a year of data. You rarely need to design Spark itself, but you should be able to say what it does well, how a job is split up and why shuffles are expensive, and when you'd pick it over a streaming engine like Flink.
The Core Abstractions
Spark has gone through three programming interfaces, and you'll hear all three names.
RDD (Resilient Distributed Dataset): The original, low-level abstraction. An RDD is an immutable collection of records split into partitions, spread across the cluster. You transform it with functions like map, filter and reduceByKey. RDDs know nothing about the structure of your records, so Spark can't optimize much; it runs your functions exactly as written.
DataFrame: A distributed table with named, typed columns, like a table in a database. You manipulate it with SQL or with a query-builder API (select, where, groupBy, join). Because Spark understands the schema and the operations, it can optimize the whole query and store data in a compact binary format. This is what almost all modern Spark code uses.
Dataset: The typed version of a DataFrame in Scala and Java. In Scala a DataFrame is literally Dataset[Row]. Python has only DataFrames.
Under the hood, DataFrames are still compiled down to RDDs of binary rows, so the RDD concepts (partitions, lineage, narrow and wide dependencies) are what actually explain how Spark behaves.
A typical batch job in PySpark looks like this:
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.appName("daily-ad-clicks").getOrCreate()
clicks = spark.read.parquet("s3://logs/clicks/date=2026-03-01/")
ads = spark.read.parquet("s3://warehouse/ads/")
daily = (clicks
.where(F.col("country") == "US")
.groupBy("ad_id")
.agg(F.count("*").alias("clicks"),
F.countDistinct("user_id").alias("unique_users"))
.join(ads.select("ad_id", "advertiser_id"), "ad_id"))
daily.write.mode("overwrite").parquet("s3://warehouse/ad_clicks_daily/date=2026-03-01/")
Transformations and Actions
Every operation is one of two kinds:
- Transformations (
where,select,groupBy,join,map) describe a new dataset in terms of an old one. They return immediately and do no work. - Actions (
count,collect,show,write) ask for a result. Only an action makes Spark actually run anything.
This lazy evaluation is what lets Spark optimize. By the time you call write, Spark has the whole plan in front of it. It can see that only three columns of clicks are used and read just those columns from Parquet, and that the country filter can be pushed into the file scan so most row groups are skipped entirely.
A common slip in interviews (and in real code) is assuming each line runs as it's written. If you call
count()on a DataFrame, thenwriteit, Spark runs the entire pipeline twice, reading the input twice. If a dataset is reused by several actions,cache()it.
Narrow and Wide Dependencies
The most useful idea for reasoning about Spark performance is how each transformation depends on its input partitions.
- A narrow transformation (
filter,select,map) computes each output partition from exactly one input partition. Partition 7 of the output only needs partition 7 of the input, so the work can run on the same machine with no network traffic. - A wide transformation (
groupBy,join,distinct,orderBy,repartition) needs records from many input partitions to build one output partition. All the clicks forad_id = 42are scattered across every input partition, and they must be brought together. That data movement is called a shuffle.
Shuffles are where most of the cost of a Spark job lives, and they're also where Spark draws the boundaries between stages.