Stream & Batch Processing
Aggregate unbounded real-time streams with windows and watermarks, or run massive bounded batch jobs.
In plain words
Batch processing handles a big pile of data all at once, for example every night. Stream processing handles each event the moment it arrives.
Think of it like…
Batch processing is reconciling all paper receipts at midnight; stream processing is a live conveyor-belt counter updating totals every second while handling late arrivals.
Where you’ve seen it
A bank runs a nightly batch job for monthly statements, but uses stream processing to flag a suspicious card payment within seconds.
Words you’ll hear
- Batch
- Processing a large, finished dataset in one job (e.g. Spark).
- Stream
- Processing an endless flow of events continuously (e.g. Flink).
- Window
- A time bucket such as "last 5 minutes" used to group events.
- Event time
- When something actually happened, as opposed to when it was processed.
Visual diagram
Theory & deep dive
Event Time, Watermarks, and Late Data
Suppose a subway rider taps their phone at 10:04 AM inside a tunnel, and the phone regains signal and uploads the event at 10:12 AM. Event time is 10:04 AM; processing time is 10:12 AM.
Stream engines like Apache Flink track event time using watermarks ("we estimate no events older than 10:08 will arrive anymore") so 5-minute dashboards stay accurate even when mobile devices go temporarily offline.
How it works, step by step
Step 1 of 41. Distinguish Event Time vs. Processing Time
Always bucket metrics by event time (when the user clicked) rather than processing time (when the server received it) so network delays don’t skew results.
- Event Time
- True occurrence
- Processing Time
- Server clock
When to use it & trade-offs
What it is
Batch processing crunches bounded historical datasets (Spark, MapReduce) for high-throughput accuracy, while stream processing computes continuous, low-latency aggregations over unbounded event streams (Flink, Kafka Streams).
Why it matters: Trending hashtags, real-time fraud detection, and live ad-click billing cannot wait for an overnight batch job - yet late-arriving mobile events require careful windowing.
When to reach for it
- Real-time ad-impression deduplication and billing rollups
- Sliding-window trending topics, surge pricing, and fraud velocity checks
- Approximate sketches (HyperLogLog for unique counts, Count-Min Sketch for top-K)
When to avoid or be careful
- Aggregating by server processing time when mobile clients buffer events offline
- Maintaining huge exact
HashSets of 1B user IDs in stream RAM when HyperLogLog (~12 KB) gives 99% accuracy
Stream vs. Batch & Architecture Patterns
Strengths: Complete data available upfront; highest throughput per dollar; simple retries.
Watch out: Minutes-to-hours data freshness latency.
Strengths: Sub-second to few-second freshness; continuous stateful aggregations.
Watch out: Requires handling late arrivals, watermarks, and state checkpoints.
Strengths: Kappa keeps one codebase; Lambda isolates real-time approximations from batch truth.
Watch out: Lambda doubles maintenance; Kappa requires long log retention or tiered storage.
Interview signals & numbers to know
When this topic comes up
- real-time analytics
- last 5 minutes
- late events
- ad counting
- surge pricing
Benchmarks & rules of thumb
- HyperLogLog memory (1B uniques)
- ~1.5–12 KB (~0.8% err)
- Stream end-to-end latency
- 50 ms – 5s
- Checkpointing interval
- 10–60s
- Top-K sketch
- Count-Min Sketch
Common mistakes & follow-ups
Common interview mistakes
- Bucketing events by arrival time instead of event time, distorting metrics during network outages
- Forgetting memory bounds on unbounded streams (must use windows + TTL or probabilistic sketches)
- Building two separate pipelines (Lambda) when a single replayable Kappa stream suffices
Likely interviewer follow-ups
- How do watermarks decide when a window is ready to emit while still handling late mobile events?
- How can you count unique daily active users in real time across 500M users using tiny memory (HyperLogLog)?
- Compare Tumbling, Hopping/Sliding, and Session windows with examples.
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Stream & Batch Processing inside real High-Level Design prompts or review Q&A flashcards.