Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 22/ 31
Distributed Systems & Architecture

Stream & Batch Processing

Aggregate unbounded real-time streams with windows and watermarks, or run massive bounded batch jobs.

AdvancedWindows · Watermarks · Lambda/Kappa3 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
Windowing, Watermarks & Backpressure

Aggregate endless event streams accurately even when events arrive out of order.

Advanced
Lambda vs. Kappa Architecture

Compare dual batch+speed pipelines (Lambda) against unified stream-only replay (Kappa).

Advanced

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

Loading interactive diagram
Loading interactive exercise

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.

Key takeaway: Group streaming windows by Event Time (not server arrival time) and use Watermarks to handle late mobile events.

How it works, step by step

Step 1 of 4
Time Semantics

1. 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.

Offline mobile devices upload events minutes or hours late
Key numbers at this step
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

Batch Processing (Spark / MapReduce)
Best for: Monthly billing, ML training, full historical backfills

Strengths: Complete data available upfront; highest throughput per dollar; simple retries.

Watch out: Minutes-to-hours data freshness latency.

Stream Processing (Flink / Kafka Streams)
Best for: Fraud detection, live leaderboards, real-time anomaly alerts, ad rollups

Strengths: Sub-second to few-second freshness; continuous stateful aggregations.

Watch out: Requires handling late arrivals, watermarks, and state checkpoints.

Kappa vs. Lambda
Best for: One replayable stream pipeline (Kappa) vs. complex batch backfill (Lambda)

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.
Interview tip: Mention HyperLogLog and Count-Min Sketch whenever a streaming problem asks for distinct counts or top-K trends at massive scale.

Knowledge check

Loading quiz

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.

Open AI HLD Practice →SD Interview Q&A
Previous topic
Message Queues & Event Streaming
Next topic
Real-Time Communication