Overview
Curated: · Written: · Reviewed:
Batch vs. Streaming Processing
In one line
Batch computes a result once over a complete, bounded input. Streaming keeps results updated as an unbounded stream arrives. Choose streaming when a decision must change within seconds or minutes, and accept watermarks, state and corrections as the cost.
Key definitions
- Event time: when the business event happened. Windows built on it give the same result on replay.
- Processing time: when the operator handled the record. Only correct for actions tied to arrival.
- Watermark: the engine's estimate of event-time progress. A heuristic, not a guarantee that no earlier event is still in flight.
- Allowed lateness: how long a fired window keeps state so late records can update it. Flink's default is 0.
- Trigger and accumulation mode: when a pane is emitted, and whether later panes replace, retract or add to earlier ones.
- Checkpoint: a consistent snapshot of source offsets and operator state that a restart resumes from.
Formulas and complexity
Hypothetical figures: the maximum event time seen is 12:00:40 and the bound is 90 s, so the watermark is 11:59:10. The window [11:58:00, 11:59:00) has passed its end and fires. With allowed lateness of 120 s, its state survives until the watermark reaches 12:01:00. A record stamped 11:58:30 that arrives while the watermark is below 12:01:00 updates that window and emits a corrected pane.
How it works
- Each record is assigned to windows by its event timestamp, not its arrival time.
- The watermark advances with the maximum event time seen, minus the out-of-orderness bound.
- When the watermark passes a window end, the window fires and emits its result.
- A record for a window already past its allowed lateness goes to a side output or is dropped. It is never silently merged.
- Checkpoints store offsets and state together, so a restart replays only what the sink has not committed.
When to use and when not to
| Factor | Batch | Streaming |
|---|---|---|
| Freshness needed | Hours, complete period | Seconds to minutes |
| Late data | Reprocess the full period | Corrections through lateness and triggers |
| Final numbers | Simple, complete input | Needs a watermark policy and reconciliation |
| Failure recovery | Rerun the job | Checkpoints, offsets and sink commits |
| Operating cost | Scheduled, cheap to rerun | Always on; state and backpressure to watch |
| Typical use | Settlement reports, training, restatement | Fraud response, monitoring, personalization |
| Only a small subset needs seconds-level latency | Batch everything and accept the lag | Stream the subset, batch the full history |
Traps interviewers probe
- Running a batch group-by over an out-of-order stream. Fix: use event-time windows, set the watermark bound from disorder you measured on real data (90 s in the example above), and set allowed lateness or a side output for records beyond it.
- Treating the watermark as proof. Fix: call it a heuristic. Decide what happens to late records and who accepts the correction.
- Assuming exactly-once engine state means exactly-once effects. Fix: checkpoints restore engine state, but a non-idempotent sink still receives replayed records. Use a stable event identity as the idempotent key, or a two-phase-commit sink.
- Building keys from arrival time or System.currentTimeMillis(). Fix: derive keys from the event identity, so a replay writes the same row.
- Fixing a hot key by adding partitions. Fix: one key stays on one subtask. Split the key with salting or pre-aggregation, and check per-subtask CPU.
Interview tips
Open with the decision: "The freshness and correctness requirements decide it. I would keep one durable log, stream the subset that needs seconds, and batch-restate the full day."
Follow-ups to expect:
- What watermark bound did you choose, and how did you measure the disorder?
- What happens to a record that arrives after its window fired?
- Where does exactly-once stop, and what does the sink do on replay?
- Kappa or Lambda for historical reprocessing? Kappa replays the log; Lambda keeps a separate batch path.
- Which lag and watermark metrics page on-call?
30-second answer outline:
- Name the decision and the freshness it needs (5 s).
- Choose event time with a watermark bound set from measured disorder (8 s).
- Say what happens to late data and who accepts corrections (7 s).
- State the end-to-end guarantee: checkpoint plus idempotent or transactional sink (6 s).
- Name two SLIs: event-time lag and consumer lag (4 s).
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.