06. Architecture / Lesson 4 of 4
Batch & Stream Processing
Crunch a huge pile of data once a day, or count events as they arrive: when to choose each.
- 10 min
- Intermediate
- 10 steps
- Result age—
- Job time—
- Events counted—
- Health check
- Write
- Request
- Failed request
Step 1 of 10: A billion events a day
Read every step as text
Step 1: A billion events a day
A busy site records about 1,000,000,000 events a day (page views, clicks, orders), each around 200 bytes. That is 11,574 events every second, and 200 GB a day, 6,000 GB (6 TB) in a month.
We want answers from it: views per page, revenue per country, which feature people use. A normal database query cannot scan that much quickly, and one server cannot even hold the month.
Two ways to answer: process the pile in big runs (batch), or keep counting as events arrive (stream).
Step 2: Batch: process yesterday's data in one big job
A batch job reads a large, fixed set of data, such as all of yesterday's events, computes the answer and writes it out. The events are first stored in cheap, large storage: a data lake.
On one machine that reads 100 MB/s, 200 GB takes 200,000 MB ÷ 100 MB/s = 2,000 s, about 33.3 min. A report for the whole month, 6,000 GB, would take 16.7 hours.
Batch gives complete, correct answers, but the result is only as new as the last run: up to a day old.
Step 3: Split the work: map, shuffle, reduce
Cut the data into pieces and give each machine a piece. Map: each machine reads its part and emits pairs such as
(page, 1). Shuffle: pairs with the same page go to the same machine. Reduce: each machine adds up the numbers for its pages.With 20 machines each reads 10 GB: 10,000 MB ÷ 100 MB/s = 100 s instead of 2,000 s. Hadoop and Spark are built on this idea, and they restart a failed piece instead of the whole job.
Move the code to the data, split by key, and the work spreads over as many machines as you can add.
Step 4: A scheduler runs the stages in order
Real pipelines have stages: extract, clean, aggregate, report. A scheduler (Airflow, cron) starts them at a set time, say 02:00, in the right order, and starts a stage only when the stage before it has finished. With stages of 5 min, 5 min, 100 s and 1 min, the pipeline takes about 12.7 min.
When a stage fails, only that stage is retried, not the whole night's work. Jobs should be idempotent: running one twice gives the same result, so a retry is safe.
Alert when a job has not finished by a deadline. A silent failed job means yesterday's numbers on today's dashboard.
Step 5: Streams: update the answer with every event
A stream processor (Flink, Kafka Streams) never waits for a pile. It reads each event as it arrives from the event stream, updates its counts and keeps running forever.
The result is fresh within a couple of seconds instead of a day. It needs to keep up: 11,574 events a second, all day, which is why the work is split by key over many workers.
Use a stream when a delay costs money: fraud checks, live dashboards, alerts.
Step 6: Windows: count events in slices of time
A stream has no end, so "how many views?" needs a time slice. A tumbling window is a fixed, non-overlapping slice, such as one minute: at 11,574 events a second about 694,444 fall into each. A sliding window moves forward in small steps, such as the last 5 minutes updated every minute.
When a window closes, its result is written and the window's memory is freed.
Windows turn an endless stream into a series of small, finished answers.
Step 7: Events arrive late, and out of order
A phone loses signal, then sends a click it made at 12:00:58 at 12:01:20. By its event time it belongs to the 12:00 window, but that window is already closed: the stream sees it by processing time in the next one.
The processor keeps a watermark: it waits 10 s after a window ends before closing it. A later event is dropped or sent to a correction path. Say 0.5% arrive too late: 5,000,000 of 1,000,000,000 events a day are missing from the stream's answer, which is 99.5% counted.
A longer wait is more accurate and slower. Choose the grace period from how late your data really is.
Step 8: Exactly-once, effectively
A stream worker can crash. It restarts from its last checkpoint, saved every 10 s, and re-reads events: up to 10 × 11,574 = 115,741 of them. A plain count would count those twice.
No network gives true exactly-once delivery. What works is at-least-once delivery (never lose an event) plus idempotent or transactional writes (a repeat has no extra effect). The effect on the result is exactly once.
Checkpoint the position and the state together, and write results in a way a replay cannot double.
Step 9: Run both, or only the stream
Lambda architecture runs two paths on the same events: a fast speed layer (stream, answer in about 2 s, 99.5% counted) and a batch layer (nightly, complete). A query merges them, and the nightly run fixes the 5,000,000 late events. The cost: two code bases that must give the same answer.
Kappa uses only the stream. The event log keeps data for weeks, so to fix a bug or rebuild results, you start a new stream job and replay the log from the start. One code base, but the log must be kept and replays take time.
Many teams now start with a stream and replay, and keep a batch job only where they truly need it.
Step 10: Batch and stream in one picture
Batch jobs process a big, fixed pile with many machines (100 s instead of 33.3 min) and are complete but old. Streams update with every event, fresh in about 2 s, but must handle windows, late events and restarts.
Choose by how fresh the answer must be, and by how much correctness you need on the first try.