Micro-Batch vs Continuous
Concepts covered: Structured Streaming
Every micro-batch pays a fixed toll before it touches a row. The driver first writes the batch's offsets to the write-ahead log, then plans and schedules a job. Stateful tasks load and commit their stores, and after the sink finishes the driver writes a marker to the commit log. Spark's documentation puts the micro-batch floor at about 100 milliseconds end to end, and a query with a stateful shuffle and object-store checkpoints usually needs a second or more per batch.
Continuous processing, added in Spark 2.3 and still marked experimental, skips the toll by never ending the job. One long-running task per input partition reads and writes rows as they arrive, and the trigger interval only sets how often Spark marks an epoch and checkpoints positions. The documentation quotes end-to-end latency as low as 1 millisecond.
surge_alerts = (
request_log
.filter("surge_multiplier > 2.0")
.select("request_id", "city", "surge_multiplier")
)
query = (
surge_alerts.writeStream
.format("console")
.option("checkpointLocation", "/chk/surge_alerts")
.trigger(continuous="1 second")
.start()
)What continuous mode gives up
A stateless filter feeds an alerting service that tolerates duplicate alerts and must react within 20 milliseconds. The source log has 12 partitions and the query gets 8 executor cores. What happens in continuous mode?
- It runs near 20 ms, because continuous tasks take turns on the 8 cores between epochs.
- It runs 8 partitions continuously and processes the other 4 as micro-batches.
- It runs, but some alerts are lost, because continuous mode delivers at most once.
- It stops making progress, because it needs 1 core for each of the 12 partitions.
Matching the mode to a latency budget
| Latency budget | Query shape | Choose | What you pay |
|---|---|---|---|
| Minutes | Any | Micro-batch, trigger every 1 to 5 minutes | Little: fewer batches, fewer small files |
| 1 to 10 seconds | Aggregations, joins, dedup | Micro-batch, default trigger | The toll on every batch |
| 100 ms to 1 second | Stateless | Micro-batch, tuned: few shuffle partitions, fast checkpoint storage | Tuning effort, more frequent commits |
| Under 100 ms | Stateless, map-only | Continuous | At-least-once, manual restarts, pinned cores |
| Under 100 ms | Stateful | Neither: Spark's floor is above it | A different serving design |
Choose the slowest trigger the consumer tolerates. A file sink on a 1-second trigger writes a fresh set of files every second, one per output task, and compaction becomes a permanent chore; a 1-minute trigger writes 60 times fewer sets for the same rows. A dashboard that refreshes every minute gains nothing from second-level batches.
Measure the toll before arguing about it. Each entry in query.recentProgress carries durationMs, where addBatch is the time spent running the batch's job and triggerExecution covers the whole batch, log writes included. If a stream on a 1-second trigger shows 400 milliseconds of triggerExecution outside addBatch, 40 percent of every second goes to bookkeeping.
- 4%
A fraud check must flag a card within 50 milliseconds when its spend over the last 10 minutes passes a limit. Which plan fits?
- Continuous mode, because it reaches 1-millisecond latency for any query shape.
- Neither: continuous mode rejects the aggregation, and micro-batch cannot reach 50 ms.
- Micro-batch with a 50-millisecond trigger, since the trigger sets end-to-end latency.
- Continuous mode with RocksDB state, since native memory removes the per-batch toll.
About this lesson
This section is part of the Structured Streaming: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.