MESSAGING & EVENT-DRIVEN ARCHITECTURE / SYSTEM CONCEPT BRIEF

Batch vs streaming

Batch processing collects data over a period and processes it all at once, for example a nightly job computing yesterday's sales.

IntermediatePhase 06 / Topic 11 of 18RequirementsTrade-offsFailure modes
01

Overview

Batch processing collects data over a period and processes it all at once, for example a nightly job computing yesterday's sales. Stream processing handles each event (or small window) continuously as it arrives, producing results within seconds. Batch is simpler, cheaper, and easier to reprocess; streaming gives low latency but must handle late, duplicate, and out-of-order events.

Modern data platforms often combine both. The Lambda architecture runs a batch layer for accuracy and a speed layer for freshness; the Kappa architecture uses one streaming pipeline and replays the log to reprocess. Engines like Apache Flink and Spark Structured Streaming unify the two.

Laundry

Batch is doing all the laundry on Sunday: efficient, but a shirt worn Monday is not clean until next week. Streaming is washing each item right after use: always fresh, but you are running the machine constantly.

02

When to use it

  • Batch: reports, billing, ML training, backfills, data warehouse loads.
  • Streaming: fraud detection, live dashboards, alerts, recommendations updates.
  • Deciding freshness requirements for a data pipeline.
  • Interview designs for analytics and metrics.
03

Where it shows up in interviews

Real-time analytics

Recognize it when: results needed within seconds.

  • Design real-time fraud detection
  • Design a live leaderboard of trending topics
Periodic aggregation

Recognize it when: daily or hourly results are enough.

  • Design a billing system
  • Design ad click aggregation reports
04

Where it is used in real software

Apache Spark and Hadoop

Batch processing of massive datasets in data lakes.

Apache Flink

Stateful stream processing with event-time windows and exactly-once state, used by Alibaba, Uber, and Netflix.

Ad click aggregation

Ad platforms stream clicks for near-real-time budgets and reconcile with batch jobs for billing accuracy.

05

Key terms

Event time vs processing time
When it happened vs when it was processed.
Window
Group events by time: tumbling, sliding, session.
Watermark
Estimate that events up to time T have arrived.
Late data
Events arriving after their window closed.
Lambda / Kappa architecture
Batch plus stream / stream-only with replay.
06

How it works, step by step

  1. 1
    Define freshness needs

    Seconds, minutes, hours, or daily.

  2. 2
    Pick the model

    Batch for large periodic jobs, streaming for continuous low-latency results.

  3. 3
    Handle time correctly

    Use event time, windows, and watermarks for streams.

  4. 4
    Ensure correctness

    Idempotent sinks, exactly-once state, deduplication.

  5. 5
    Plan reprocessing

    Replay the log or rerun batch jobs after bugs.

07

Batch vs streaming

Key differences

Step 1 / 5
AspectBatchStreaming
LatencyMinutes to hoursMilliseconds to seconds
DataBounded (a day's files)Unbounded (continuous)
ComplexityLowerHigher (state, late data, ordering)
CostEfficient bulk processingAlways-on resources
ToolsSpark, dbt, AirflowFlink, Kafka Streams, Spark Streaming

NOWAspect: Latency | Batch: Minutes to hours | Streaming: Milliseconds to seconds

Start with batch unless the business needs low latency; add streaming for the specific use cases that do.

08

Implementation

-- Count clicks per ad in 1-minute tumbling windows by event timeCREATE TABLE clicks (  ad_id STRING,  user_id STRING,  ts TIMESTAMP(3),  WATERMARK FOR ts AS ts - INTERVAL '10' SECOND   -- tolerate 10 s of lateness) WITH ('connector' = 'kafka', 'topic' = 'ad-clicks', 'format' = 'json'); SELECT ad_id,       TUMBLE_START(ts, INTERVAL '1' MINUTE) AS window_start,       COUNT(*) AS clicksFROM clicksGROUP BY ad_id, TUMBLE(ts, INTERVAL '1' MINUTE);
09

Complexity and performance

Batch latencyJob interval + runtime

Hours typical.

Streaming latencyWindow + watermark delay

Seconds typical.

10

Trade-offs

Freshness vs complexity and cost

Streaming delivers fresh results but needs always-on infrastructure and careful handling of time and state.

Accuracy

Streams must decide how long to wait for late data; batch sees complete data.

11

Variants and related techniques

Micro-batching

Spark Structured Streaming processes small batches every few seconds.

Kappa architecture

One streaming codebase; reprocess by replaying Kafka.

12

Common mistakes

  • Using processing time for business metrics.

    Fix: Delays and replays shift events into the wrong windows; use event time.

  • Choosing streaming for daily reports.

    Fix: Batch is cheaper and simpler when freshness is not needed.

13

Interview questions

How would you aggregate ad clicks for billing and a live dashboard?

Stream clicks through Kafka into Flink with event-time windows for the dashboard (seconds of latency), and run a batch job over the raw, deduplicated click log for billing, reconciling differences.

What is a watermark?

A marker in a stream asserting that no more events older than a given time are expected, letting windows close and emit results while tolerating a bounded amount of lateness.

14

Practice problems

ProblemDifficultyWhat it trains
Choose batch or streaming for 6 use casesEasyFreshness needs.
Design an ad click aggregation systemHardStreaming + batch reconciliation.