Sub topic 6
Controlling when batches run with triggers
A trigger tells a streaming query when to start the next micro-batch. This chapter assumes you know the unbounded table model, can write a basic writeStream query, and know what a checkpoint is. The trigger you pick sets the balance between latency and cost.
What a trigger decides
Without a trigger, Spark starts a new micro-batch as soon as the previous one finishes. That gives low latency, but the cluster stays busy and you pay for it all the time. A trigger lets you change that rhythm.
Two triggers cover most Databricks work:
| Trigger | Behavior | Typical use |
|---|---|---|
| `processingTime="1 minute"` | Starts a batch at a fixed interval. The query keeps running between batches. | Continuous pipelines with a latency target of seconds to minutes |
| `availableNow=True` | Processes all data available at start, in one or more batches, then stops. | Scheduled incremental loads, such as hourly or nightly jobs |
Processing time trigger
With processingTime, Spark checks for new data at each interval. If a batch takes longer than the interval, the next batch starts as soon as the current one ends. If there is no new data, Spark skips the batch.
query = (
spark.readStream.table("main.demo.orders_bronze")
.writeStream
.trigger(processingTime="1 minute")
.option("checkpointLocation", "/Volumes/main/demo/chk/orders_silver")
.toTable("main.demo.orders_silver")
)The cluster must stay up for the query to keep running. Choose this when the latency target justifies an always-on cluster.
availableNow trigger
availableNow suits queries that run on a schedule. Each run reads everything that arrived since the last checkpoint, splits it into batches that respect rate limits, and then stops. The cluster can shut down afterward.
query = (
spark.readStream.table("main.demo.orders_bronze")
.writeStream
.trigger(availableNow=True)
.option("checkpointLocation", "/Volumes/main/demo/chk/orders_daily")
.toTable("main.demo.orders_daily")
)
query.awaitTermination()Call awaitTermination() so the notebook or job waits until the run finishes. Without it, code after the query can run before the data is written.
How the two triggers compare in time
Common errors
- Choosing a short interval for a cheap job. A one-second interval on a source that gets data hourly wastes compute. Match the interval to how fast data arrives and how fresh the result must be.
- Forgetting that the cluster must be running. A
processingTimequery stops producing results if its cluster terminates. - Expecting
availableNowto wait for new data. It stops once it catches up. Data that arrives afterward is picked up by the next run. - Changing the trigger and assuming a reset. The checkpoint stores progress, not the trigger. You can switch between trigger types on the same checkpoint, and the query continues from where it stopped.
Exercise
A source receives files all day, but the business only needs the report refreshed once each night. Which trigger should you use, and why?
Show answer
Use availableNow=True and schedule it as a nightly job. Each run processes everything that arrived since the last checkpoint and then stops, so the cluster is only billed during the run. A processingTime trigger would keep the cluster running all day for no benefit.
Exercise
What happens if a processingTime="1 minute" batch takes three minutes to finish?
Show answer
Spark does not run overlapping batches. The next batch starts as soon as the slow one ends, so the query runs behind its schedule until a batch finishes within the interval.