Batch and stream processing
MapReduce and Spark for large batch jobs, and streaming concepts: event time, windows, watermarks and exactly-once results.
On the roadmap:Software Architect · step 4: Data at scale
Lesson 2 of 4 · 12 min read · quiz · exercise
Two ways to process data
- Batch: process a bounded dataset (yesterday's logs) as a job that starts, runs and finishes. High throughput, results later.
- Stream: process an unbounded flow of events continuously as they arrive. Low latency, harder to get right.
Most platforms need both: stream for fresh insight, batch for heavy recomputation and history.
Before: batch: a big job on a schedule
After: stream: process each event
What changed
- Results arrive in seconds instead of hours
- Late and duplicate events must be handled
- Batch stays useful for reprocessing history
Batch: the MapReduce idea
Word count is the classic example.
Figure SDA-BDP-02-1 · MapReduce word count flow · © forcsguy.com
- Map: each worker reads a slice and emits key-value pairs (
("system", 1)). - Shuffle: the framework groups all pairs with the same key and sends them to the same reducer. This is the expensive step (network and sorting).
- Reduce: each reducer combines the values for its keys (
("system", 4812)).
Failures are handled by re-running tasks, which is possible because inputs are immutable and tasks have no side effects.
Spark improved this model: it keeps intermediate data in memory, expresses jobs as chains of transformations on DataFrames (with a query optimiser), and offers SQL, machine learning and streaming APIs on one engine. Today you usually write Spark or SQL, not raw MapReduce.
Image: Unknown authorUnknown author · Public domain · Source · Wikimedia Commons: Ford assembly line - 1913
Stream processing
A stream processor reads events from a log (such as Kafka), transforms them, and writes results to another log, a database or a dashboard.
Common operations: filter and enrich, aggregate, join streams, and detect patterns.
Time is the hard part
- Processing time: when the system sees the event.
- Event time: when it actually happened.
Phones go offline, networks lag, and events arrive late and out of order. Computing "clicks per minute" by arrival time gives wrong answers. Use event time.
Windows
Aggregations need boundaries:
| Window | Meaning |
|---|---|
| Tumbling | Fixed, non-overlapping (every minute) |
| Sliding | Overlapping (last 5 minutes, updated each minute) |
| Session | Grouped by activity, closed by a gap of inactivity |
Watermarks and late data
A watermark is the system's estimate of "we have seen everything up to event time T". When it passes the end of a window, the window can close and emit a result. Choosing the delay trades latency against completeness. Events arriving after the watermark are late: drop them, send them to a side output, or update the earlier result.
State and failures
Windows and joins need state. Engines such as Flink keep state locally and checkpoint it periodically to durable storage, together with the position in the input log. After a failure they restore the checkpoint and replay from that position.
Delivery guarantees
- At-most-once: may lose events.
- At-least-once: may duplicate on replay.
- Exactly-once effect: achieved by replayable sources, checkpointed state and idempotent or transactional sinks, so a replay does not change results.
Architectures that combine both
- Lambda architecture: a batch layer computes accurate views from all data, a speed layer gives fast approximations; the two are merged. Powerful but means writing logic twice.
- Kappa architecture: treat everything as a stream. Keep the log long enough to replay history through the same code when logic changes. Simpler, if your log and engine can handle it.
- Modern engines and the dataflow model increasingly unify batch and streaming under one API.
Figure SDA-BDP-02-2 · Lambda and Kappa architectures · © forcsguy.com
Choosing
| Need | Choice |
|---|---|
| Nightly reports, big joins, model training on history | Batch (Spark, SQL warehouse) |
| Fraud detection, live dashboards, alerting, real-time features | Streaming (Flink, Kafka Streams, Spark Structured Streaming) |
| Both with minimal duplication | Streaming core with replayable log, or a unified engine |
Real-world reading
Related reading
- MapReduce: Simplified Data Processing on Large Clusters (paper)Dean and Ghemawat · GooglePapers
- Cape: Event Stream Processing Framework at DropboxDropbox engineers · DropboxCase Studies
- Pagelets Parallelize Server-side Processing at YelpYelp engineers · YelpCase Studies
- Optimizing CAL Report Hadoop MapReduce Jobs at eBayeBay engineers · eBayCase Studies
Show 6 moreShow fewer
- Bullet: Forward-Looking Query Engine for Streaming Data at YahooYahoo engineers · YahooCase Studies
- Samza: Stream Processing System for Latency Insighs at LinkedInLinkedIn engineers · LinkedInCase Studies
- Netflix: What Happens When You Press Play?High Scalability engineers · High ScalabilityBlogs
- CMU Database GroupCarnegie Mellon UniversityYouTube
- Omid: Transaction Processing Platform at YahooYahoo engineers · YahooCase Studies
- Big Data Processing at UberUberPapers
Check your understanding
Question 1 of 3
0 correct so far
In MapReduce word counting, what does the shuffle step do?
Go deeper
- Streaming Systems by Tyler Akidau, Slava Chernyak, Reuven Lax: a book on the dataflow model; the earlier articles 'Streaming 101' and 'Streaming 102' are free online
Practise it: A stream you cannot drop
Everything below is already set up for this lesson: change the design, run the traffic, and see whether you meet the goal.
- 1The task
- 2Build and test
Events arrive at 1,500 per second, continuously. A stream processor must keep up, and whatever it writes needs capacity too.
Goal: Under 1% errors, backlog clears within 30 s.
A stream is a queue that never stops. What drains it, and what does it write to?
Try it yourself
You count clicks per minute from mobile apps. Some events arrive up to 5 minutes late. Describe how you would window the data, handle late events and make sure a retry does not double count.
Click what a strong design here would need:
More in Real systems
© 2026 forcsguy.com. Original lesson text and diagrams. Books and sites linked above belong to their authors.