System Design · 06. Architecture · 10 min · Intermediate
Batch & Stream Processing
Crunch a huge pile of data once a day, or count events as they arrive: when to choose each.
In short
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.
Key ideas
- Two ways to answer: process the pile in big runs (batch), or keep counting as events arrive (stream). (step 1)
- Windows turn an endless stream into a series of small, finished answers. (step 6)
- Checkpoint the position and the state together, and write results in a way a replay cannot double. (step 8)
Good to know
- Move the code to the data, split by key, and the work spreads over as many machines as you can add. (step 3)
- Use a stream when a delay costs money: fraud checks, live dashboards, alerts. (step 5)
- Many teams now start with a stream and replay, and keep a batch job only where they truly need it. (step 9)
Watch out
- Batch gives complete, correct answers, but the result is only as new as the last run: up to a day old. (step 2)
- Alert when a job has not finished by a deadline. A silent failed job means yesterday's numbers on today's dashboard. (step 4)
- A longer wait is more accurate and slower. Choose the grace period from how late your data really is. (step 7)
Key numbers
- Result age
- ≈ 2 s
- Events counted
- 99.5%
Terms
- Batch processing
- Reading a large, fixed set of data in one job, computing the answer and writing it out.
- Data lake
- Cheap, large storage that keeps raw data, such as every event, for later processing.
- MapReduce
- Split data over machines, map each piece into key-value pairs, shuffle by key, and reduce each key.
- Job scheduler
- A tool that starts the stages of a data pipeline at set times and in the right order.
- Stream processing
- Handling each event as it arrives, so results are fresh within seconds.
- Tumbling window
- A fixed, non-overlapping slice of time, such as one minute, used to group events in a stream.
- Watermark
- How long a stream waits for late events before it closes a window.
- Checkpoint
- A saved position and state, from which a stream job restarts after a crash.
- Exactly-once (effectively)
- At-least-once delivery plus idempotent writes, so each event affects the result once.
- Lambda architecture
- A fast stream layer and an accurate batch layer over the same events, merged at query time.
- Kappa architecture
- Only a stream, with the event log replayed to recompute results.