Data Engineering · Module 08
Batch processing with Spark
- Lessons
- 14
- Free
- 0
- Reading
- ~4 h
Your progress
0 / 14 lessons done
Sign in to trackLessons
- Distributed batch processing: beyond one machineEvery growing data team reaches a night when one job no longer finishes in its window on the biggest server it has. Knowing what a cluster actually changes, and what it costs, decides whether you add machines or fix the job.
- MapReduce: the idea behind big dataEvery Spark job, warehouse GROUP BY and distributed join still splits work the way MapReduce did: work on pieces, move records with the same key together, then combine them. Knowing that shape tells you where a big job spends its time and why it fails.
- Spark architecture: driver, executors and tasksMost Spark failures and slow jobs make sense once you know which process holds what: a driver out of memory, a stage stuck on one task, an executor lost halfway. Every Spark setting you tune later is a setting for one of these processes.
- Spark DataFrames and Spark SQLAlmost every Spark job today is written with DataFrames or Spark SQL. Knowing what runs on the cluster and what drags data back into one Python process decides whether a job finishes in minutes or dies on the first full day of data.
- Lazy evaluation and Spark query plansA Spark job’s cost is decided by the plan Spark builds, not by the order of your lines. Reading that plan is how you catch a job that reads thirteen months of data to answer a question about one week.
- Shuffles: the most expensive step in SparkMost slow or failing Spark jobs are slow or failing in a shuffle. Knowing what a shuffle writes, sends and reads lets you look at a query and say how much data it will move before you run it on a billion rows.
- Case: LinkedIn’s push-based shuffle for SparkShuffle failures and slow shuffle reads are among the most common reasons a large Spark job misses its deadline. LinkedIn’s account shows where that pain comes from at petabyte scale, and which parts of the fix apply to a much smaller platform.
- Partitions and parallelism in SparkA Spark job is only as parallel as its number of partitions. Too few and paid-for cores sit idle while one task runs for hours; too many and the job drowns in tiny tasks and tiny files. Every slow or costly Spark job you debug starts with this number.
- Joins at scale: broadcast and sort-mergeAlmost every Spark job joins a large fact table to dimensions. Choosing how the rows meet decides whether the job moves 2 GB or 40 GB over the network, and whether it finishes or fails at 2 a.m.
- Data skew: when one task does all the workReal data is never even: a sale, a big seller or a pile of NULL keys sends most rows to one task. Adding machines doesn’t help, so a job that ran in minutes for months can take an hour on the one night the business needs it most.
- Caching and memory in SparkA Spark job that reads the same data three times costs three times the reads, and a cache that doesn’t fit costs more than no cache at all. Knowing how an executor splits its memory tells you which one you have before the job fails at 03:00.
- Writing efficient Spark jobsMost slow Spark jobs aren’t slow because the cluster is too small. They read data they don’t need, push rows through Python, run the same input several times or write thousands of tiny files. Each of these shows up in the plan before the job runs, and each fix is a few lines.
- Reading the Spark UI to debug jobsA Spark job that runs twice as long as yesterday rarely says why in its logs. The Spark UI shows where every second went, task by task, and reading it is how you avoid paying for executors that would not help.
- Design: a nightly job over a billion ordersMost companies end up with a few batch jobs that read all of their history every night. Whether those jobs finish on time, and whether their numbers can be trusted after a crash, decides whether the morning report and the models that depend on them are right.
Practise with
No tools or paper guides for this module yet.
Terms this module teaches 47
Open glossaryAction (Spark)
A call that needs a real result, such as count, collect or a write; it makes Spark run the steps recorded so far.
Adaptive Query Execution (AQE)
Spark SQL's re-optimization of a query plan during the run, using statistics from finished stages; on by default since Spark 3.2.0.
Apache Spark
An open-source engine that runs batch jobs across many machines: a driver splits the work into tasks and executors run them in parallel.
Block (Spark cache)
One stored partition of a cached DataFrame or RDD, kept on the executor that computed it.
Broadcast hash join
A distributed join that sends a full copy of the small table, as a hash table, to every executor, so the large table is joined where it already sits.
Catalyst optimizer
Spark SQL's query optimizer: it checks a DataFrame or SQL query's names and types, rewrites the logical plan with rules, and chooses a physical plan.
Cluster manager
The service that finds machines for a Spark application and starts its driver and executor processes.
Column expression
A description of a calculation on a column, such as a comparison or a sum, that a query engine runs later on every row.
Combiner
A function that merges a map task’s own output before the shuffle, so less data moves; it must give the same final answer as merging everything at the end.
Data locality
Running a task on the machine that already holds its input, so the data is read from local disk instead of over the network.
Data skew
An uneven spread of rows across partitions, usually because one key has far more rows than the others, so one task gets much more work.
Driver (Spark)
The process that runs a Spark program’s main code, turns it into a plan, splits the plan into tasks and schedules them on executors.
Dynamic partition overwrite
An overwrite mode that replaces only the partitions a write puts data into, leaving every other partition of the table as it was.
Event log (Spark)
A file a Spark application writes while it runs, recording the events its UI shows, so the UI can be rebuilt after the application ends.
Execution memory
Memory a Spark task uses for its working data in shuffles, joins, sorts and aggregations.
Executor (Spark)
A process started for one Spark application on a worker machine; it runs that application’s tasks in threads and keeps their data in memory or on disk.
External shuffle service
A long-running process on each cluster node that stores and serves shuffle files, so executors can be removed without losing the shuffle output they wrote.
Hadoop YARN
Hadoop’s cluster manager, which gives applications such as Spark containers on a shared cluster and runs a NodeManager process on every node.
Job (Spark)
The work Spark runs for one action, made of one or more stages.
Logical plan
A tree of what a query computes (scan, filter, aggregate) without saying how; Spark keeps a parsed, an analyzed and an optimized version.
Memory overhead
Memory a Spark executor’s container gets on top of the JVM heap, for native buffers, JVM internals and Python worker processes.
Narrow transformation
A Spark operation that needs only the rows already in its own partition, such as a filter or a new column, so it runs without moving data.
pandas UDF
A PySpark user-defined function that receives a batch of rows as pandas Series, moved with Apache Arrow, instead of one row at a time.
Partial aggregation
Aggregating each task’s rows before a shuffle, so only one partial result per key per task crosses, and combining the partial results after it.
Partitioning function
The rule each map task uses to choose which reduce task gets a record, usually a hash of the key modulo the number of reduce tasks.
Physical plan
The operators an engine will actually run for a query, such as which file scan, which aggregation and where data moves between machines.
Push-based shuffle
A Spark shuffle mode in which map tasks push their small blocks to shuffle services that merge them into one file per reduce partition, so reduce tasks make a few large reads instead of many small ones.
Shuffle
Spark redistributing rows between tasks so that all rows with the same key end up in the same partition, by writing them to local disk on one side and fetching them over the network on the other.
Shuffle fetch failure
A reduce task’s failure to read a shuffle block it needs, after which Spark reruns the map tasks that wrote the missing output and then the reduce stage.
Shuffle partition
One of the groups a shuffle sorts rows into, processed by one task after the shuffle; Spark SQL makes 200 by default.
Sort-merge join
A distributed join that shuffles both tables by the join key, sorts each partition, then walks the two sorted lists together to find matches.
Spark DataFrame
A table with named, typed columns whose rows are split into partitions across a Spark cluster; the program holds a plan for computing it, not the data.
Spark history server
A service that reads Spark event logs and shows the UI of finished applications, by default on port 18080.
Spark partition
One chunk of a DataFrame’s rows that a single task processes in one stage; the number of partitions sets how much of the stage can run in parallel.
Spark UI
The web pages a Spark driver serves while a job runs, showing its jobs, stages, tasks, executors and query plans with timings and data sizes.
Spark unified memory
The part of a Spark executor’s heap shared by execution and storage, 0.6 of the heap after a 300 MB reserve by default; execution can evict cached blocks only down to a protected half.
Stage (Spark)
A set of tasks in a job that can run without moving rows between executors; a shuffle starts the next stage.
Storage level
The setting that tells Spark where to keep a cached dataset’s blocks (memory, disk or both, with one or two copies) and what to do when memory runs out.
Straggler
A task that runs much longer than the others in its stage, so the whole stage waits for it.
Success marker
An empty file a job writes into its output folder only after all its output is complete, so readers know the folder is safe to read.
Task (Spark)
The unit of work sent to one executor: one stage’s steps applied to one partition of the data.
Task slot
A place on an executor where one task can run; an executor has its cores divided by spark.task.cpus slots.
Temporary view
A name given to a DataFrame for the length of a Spark session, so SQL queries can refer to it like a table.
Transformation (Spark)
A Spark operation that returns a new DataFrame or RDD and only adds a step to the plan, without reading or computing anything.
User-defined function (UDF)
A function you write and call inside a query; in PySpark it runs in a separate Python process, and the optimizer can’t see inside it.
Wave (of tasks)
One round of tasks that fills every task slot; a stage with more tasks than slots runs in several waves.
Wide transformation
A Spark operation that needs rows from other partitions, such as a group-by, join or window, so it needs a shuffle.