The big-data problem, MapReduce's limits, in-memory computing, and the unified engine for batch, SQL, streaming, and ML.
⏱ 7 min readTopics chapter readerLevel · Foundations
01 · Orientation
Start here
⏱ 5 min · Topic 1 of 7
Apache Spark is a tool that runs one data job on many computers at the same time. You write the job once. Spark splits the work.
You already know SQL, Python, batch and streaming, storage, and pipeline design. Spark is the engine that can run those steps when the data is too big for one computer.
This chapter shows the main idea. You will do one job, revenue by customer, in plain Python, in SQL, and in Spark. Then you will see why Spark splits data, and when you should not use it.
Later chapters this week explain each part.
The architecture chapter shows who does the work.
The lazy evaluation chapter shows why Spark waits before it runs.
The DataFrames chapter shows DataFrames in more depth.
The shuffle chapter shows the costly step called the shuffle.
02 · The problem
When one machine is not enough
⏱ 6 min · Topic 2 of 7
A machine is one computer. It has a fixed amount of memory (RAM), disk space, and processor time. Every tool you used so far runs on one machine.
This is fine for the shop. The real shop gets about 200 orders a day. That fits easily in Postgres or in pandas (a Python table library) on a laptop.
Now make an assumption. Imagine the shop had hundreds of millions of orders a day, kept for years.
This is not true for our shop. It is only a way to see the problem.
With that much data, three things go wrong on one machine.
The data does not fit in memory, so the job stops with an error or runs very slowly.
The data does not fit on one disk.
One machine must read every row by itself, so the job takes too long.
A bigger machine helps for a while. At some size it becomes very costly, and then it stops being possible. The other way is to use many normal machines together.
A group of machines that work together is called a cluster. The hard part is sharing the work between them. Spark does that part for you.
03 · The idea
Same job, three tools
⏱ 8 min · Topic 3 of 7
The job is: total paid revenue for each customer. Here is the input. We use it in all three tools.
Plain Python: one loop on one machineworked example
The database decides how to run it, on one machine.
PySpark lets you write Spark code in Python. A SparkSession is your starting point in Spark. A DataFrame is a table that Spark can spread over many machines.
PySpark: the same job, written as a DataFrameworked example
Spark prints 65.0, not 65.00. Spark reads amount as a double (a number with a decimal point). It does not add extra zeros. It is the same value.
F is the functions module, imported as F. It gives you col and sum.
header=True: the first line has the column names.
inferSchema=True: Spark guesses each column's type.
filter = WHERE. groupBy = GROUP BY. agg(F.sum(...)) = SUM. alias = AS. orderBy = ORDER BY.
show() prints the result.
The answer is the same in all three tools. The difference is where the work can run.
Python and SQL ran on one machine. Spark can run the same steps on a whole cluster.
Spark does not filter or add up the rows when it reaches those lines. It waits until show(). The lazy evaluation chapter explains why.
Watch out
Spark is not a database. It does not store your data. It reads data from files or tables, works on it, and writes the result back.
04 · How it splits work
Split the orders
⏱ 8 min · Topic 4 of 7
Spark splits data into pieces called partitions.
This is not the same as the date folders from the storage chapter. Those folders are on disk. A Spark partition is a group of rows that one machine works on during a job.
Each machine works on some partitions at the same time. Then Spark combines the partial answers. A partial answer is the total from one partition only.
Our shop is tiny, so we use two partitions of three rows to see the idea. Machine A gets orders 101, 103, 105. Machine B gets orders 102, 104, 106.
Each machine runs the same steps on its own rows only.
Look at customer 1. Machine A sees order 101. Machine B sees order 102.
Two machines, then combine (plain Python model)worked example
Customer 1 appears on both machines: 40.00 from A and 25.00 from B. Combining gives 65.00. On machine A, order 103 is refunded and order 105 is pending, so the filter drops both. Customer 3's 15.50 comes only from order 104 on B.
This is a model, not Spark code. Real Spark does the splitting, the running, and the combining for you. You never write the two machines yourself.
The combine step is the costly part.
The partial totals for the same customer must meet on one machine. Spark sends them over the network. The shuffle chapter teaches this step.
Remember
More partitions let more machines work at once. The same steps run on every partition.
05 · What is in Spark
One engine, many jobs
⏱ 5 min · Topic 5 of 7
Spark is one engine with several parts on top. You use the same engine for different kinds of work.
Part
What it does for the shop
Spark SQL and DataFrames
Work with tables in SQL or in Python code. Both run on the same engine.
Structured Streaming
Process new orders as they arrive, in small batches by default.
MLlib
Train machine learning models, for example to guess which orders will be refunded.
Spark also keeps data in memory between steps when it can.
An older tool, Hadoop MapReduce, also ran jobs on many machines. It wrote results to disk after each step. That made jobs with many steps slower.
Spark reads Parquet files and the date folders you saw in the storage chapter.
06 · Choose the tool
Do you need Spark?
⏱ 6 min · Topic 6 of 7
Often the answer is no. Spark adds work: you set up a cluster, wait for it to start, and learn how it runs.
Our shop has about 200 orders a day. That fits in Postgres or pandas on one machine. Spark would be slower to start and harder to run, with no gain.
Situation
Better choice
Data fits on one machine, such as the shop's 200 orders a day
Postgres, SQL, or pandas
Data is too big for one machine, or one machine is too slow
Spark
You need an answer in a few milliseconds for one row
A database or a service, not Spark
You can run Spark on one machine too.
This is called local mode. Spark still splits the work, but one machine does all of it. It is good for learning and testing.
Interview question
The question is "When would you use Spark?" A good answer: "When the data is too big for one machine. For small data I use a simpler tool."
Check yourself with these questions.
In the two-machine example, why did customer 1 need a combine step?
Is Spark a database? Say why or why not.
Name one job you would run on Spark and one you would not.
07 · Next Chapter
Next Chapter
You now know why Spark exists: it splits big data into partitions and works on them on many machines.