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?

Matching the mode to a latency budget

Latency budgetQuery shapeChooseWhat you pay
MinutesAnyMicro-batch, trigger every 1 to 5 minutesLittle: fewer batches, fewer small files
1 to 10 secondsAggregations, joins, dedupMicro-batch, default triggerThe toll on every batch
100 ms to 1 secondStatelessMicro-batch, tuned: few shuffle partitions, fast checkpoint storageTuning effort, more frequent commits
Under 100 msStateless, map-onlyContinuousAt-least-once, manual restarts, pinned cores
Under 100 msStatefulNeither: Spark's floor is above itA 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.

A fraud check must flag a card within 50 milliseconds when its spend over the last 10 minutes passes a limit. Which plan fits?

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.