Skip to content
mentoor

Topic 2 of 5 · 5 min read

Choosing batch or streaming processing

Goal: Choose batch, micro-batch or streaming for a stated freshness need and name the late-data rule it requires.

Pick the slowest processing style that still meets the freshness the business actually needs. Streaming is not better than batch; it is more expensive to build and to run, and it adds a problem batch mostly avoids: events that arrive late or out of order.

Check yourself · 1 of 3

A report is read once each morning and may be up to 12 hours old. Which processing style fits?

Choose one answer

Why it matters

Teams ask for "real time" when they mean "before the morning meeting". A nightly batch job is simple to write, test, re-run and explain. A streaming job runs all day, needs state, monitoring and a plan for restarts. An assistant will happily generate a Kafka consumer for a report that is read once a day, so you must set the requirement first.

How to choose

  1. Ask how stale the data may be. Hours or a day: batch. A few minutes: micro-batch, which runs a small batch every minute or five. Seconds, because someone or something reacts to it (fraud checks, live inventory): streaming.
  2. Ask what happens when it is wrong or late. A dashboard can be corrected tomorrow. A fraud block cannot.
  3. Pick the source. An event log such as Kafka holds ordered events in topics and partitions that consumers read at their own pace. Change data capture (CDC), for example with Debezium, reads a database's change log and publishes each insert, update and delete, so you do not poll tables.
  4. Write the late-data rule. Events carry two times: when the thing happened (event time) and when you received it. A phone offline in a tunnel sends a purchase ten minutes late. Decide the allowed lateness, for instance "accept events up to 15 minutes late, then send them to a correction job".

Before and after

Request: "Show orders per minute on the ops screen." An assistant proposes a full streaming stack with a stateful aggregation. The needed freshness is two minutes, and the volume is a few hundred orders a minute.

The fixed plan is micro-batch: every minute, run a query over the last five minutes of the orders table and overwrite those minutes in a summary table. Late orders inside five minutes are counted, and a nightly batch recomputes the day for exact totals.

What goes wrong

  • Streaming for a daily report. You pay for always-on infrastructure nobody uses.
  • Windows by arrival time. Late events land in the wrong minute, and counts differ from the batch totals.
  • No replay plan. A bug in the consumer means yesterday's events are wrong, and there is no stored log to reprocess.
  • Polling a table with an updated_at column for deletes. Deleted rows are never seen; CDC sees them.

Examples

What the requirement actually says

A product manager asks for a real-time sales dashboard. A short question reveals that the sales team looks at it twice a day. Freshness needed: one hour. A batch job every 30 minutes is enough and one engineer can maintain it.

A late-data rule written down

Window: event time, 1 minute
Allowed lateness: 15 minutes (update the window)
Later than that: write to late_events, fix in the nightly batch

Three lines remove the question "why do live and nightly numbers differ" from every future incident.

Key points

  • Choose the slowest style that meets the stated freshness: batch for hours, micro-batch for minutes, streaming for seconds.
  • Streaming adds always-on cost, state and restart handling that batch mostly avoids.
  • CDC reads a database's change log, so it captures updates and deletes that polling an updated_at column misses.
  • Event time (when it happened) and arrival time differ; window by event time and decide how late is accepted.
  • Keep the event log or raw data so a bug in the job can be fixed by reprocessing.

Common mistakes

  • Accepting "real time" without asking how stale the data may be.
  • Windowing by arrival time instead of event time.
  • Polling a table for changes and expecting to see deleted rows.
  • Building a streaming job without a way to replay past events.

Check yourself · 2 of 3

A phone app sends a purchase event ten minutes after it happened because the phone was offline. Which time should a per-minute sales window use?

Choose one answer

Check yourself · 3 of 3

What does change data capture add over polling a table with an updated_at column?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic