Triggers

Concepts covered: Structured Streaming

The micro-batch loop leaves one setting to you: when the next batch starts. That setting is the trigger, and you declare it on the writer with .trigger(...) before start(). It decides the trade between how fresh the output is and what the stream costs to run. There are 3 choices to know at this stage.

Default: back to back

Declare no trigger and the driver starts the next batch the moment the previous one finishes. If no new rows have arrived, it waits before checking again. This is the lowest latency micro-batching offers, near 100 milliseconds at best according to Spark's documentation, and it keeps executors busy whenever data is flowing.

Fixed interval: processingTime

query = (
    requested.writeStream
    .format("console")
    .trigger(processingTime="10 seconds")
    .start()
)

With processingTime, the driver starts a batch on a clock: one every 10 seconds here. Rows that arrive between ticks wait for the next one. A tick that finds no new rows runs no batch. Compared with the default, each batch holds more rows, so the fixed per-batch overhead is spread over more work and the file sink writes fewer, larger files.

Latency under a fixed interval has a worst case you can compute. A ride that arrives right after a batch begins waits one full interval for the next batch, then for that batch's job. With a 10-second interval and 3-second jobs, the worst case is 13 seconds. Under the default trigger with the same jobs it is about 6: up to 3 seconds for the running batch to finish, then 3 for its own.

The interval is a request, and a slow job can break it. If a batch takes 14 seconds against a 10-second interval, the driver starts the next batch as soon as the slow one finishes. The 2 never overlap. A stream in that state runs back to back whatever the trigger says.

Available-now: catch up and stop

Swap the trigger line for .trigger(availableNow=True), available from Spark 3.3, and the query changes character. It reads everything that has piled up at the source since its last run, processes it in one or more batches, and stops by itself. Launched by a scheduler every hour, it behaves like an hourly batch job that remembers where it left off, so each run reads only the new rows. Between runs the application has ended and its executors are released.

Latency against cost

TriggerA batch startsWorst-case wait for a rowCluster
DefaultWhen the last one endsAbout 2 batch durationsAlways on, busy
processingTime, 1 minuteEvery 60 seconds1 minute plus one batch durationAlways on, mostly idle
availableNow, hourlyWhen the scheduler launches it1 hour plus the runOn only during the run

The cluster column is where the money is. With a fixed number of executors, the usual setup for a stream, the application holds them from start to stop, so a stream on a 1-minute trigger holds its executors for all 60 seconds even when a batch is busy for only 5 of them. A longer interval saves files and overhead, and the idle executors still cost the same. Only available-now releases them, and it pays with latency measured in the gap between runs.

File counts move with the trigger too. Suppose every batch writes 4 files. Back-to-back 3-second batches make 28,800 batches a day, a 1-minute interval makes 1,440, and an hourly available-now run, at one batch per run, makes 24.

Choosing, and checking the choice

Start from the question the output answers. If someone acts on it within seconds, as a pricing system does, the default trigger fits, and so does an interval of a few seconds. If a dashboard refreshes every few minutes, a 1-minute processingTime loses nothing a viewer could see. If the output feeds a daily or hourly report, schedule available-now runs and stop paying for a cluster that waits.

Then verify in the Spark UI. A running stream adds a Structured Streaming tab that charts input rate against processing rate, along with each batch's duration. Durations well under your interval mean the trigger is doing what you asked. Durations at or above it mean the stream is running back to back, and the fix lies in the batch job: open the slow batch in the Jobs tab and find its slow stage.

A stream uses .trigger(processingTime="10 seconds"). One batch starts at 09:00:00 and its job takes 14 seconds. When does the next batch start?

Whatever the trigger, the query computes the same answer: the result table at any instant is the batch answer over the rows so far. The trigger decides how often Spark catches the result up, and what you pay for that.

About this lesson

This section is part of the Structured Streaming: Beginner 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.