Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Data Engineer Professional

Streaming state, watermarks, and output modes

Databricks Data Engineer Professional · Lesson 6 of 21 · 46:16

YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗

Learning with these videos? Subscribe for new courses and exam-style questions.

In this lesson

Prepare for the Databricks Certified Data Engineer Professional exam. In this lesson: stateful streaming and state that never stops growing, watermarks, output modes (append, update, and complete), foreachBatch, and checkpoints. Includes three original practice questions.

Study notes

The text of this lesson, to read at your own pace. Information as of November 2026.

State that never stops growing

Quayside Eats counts orders per restaurant in five minute windows, from a Kafka stream, to spot kitchens that fall behind. After two weeks, the job ran out of memory, because it kept the state of every window it had ever seen, and a restart with a new checkpoint folder counted some windows twice. This lesson covers stateful streaming: watermarks, output modes, foreachBatch, and checkpoints.

State and watermarks

Stateful operations keep data between micro batches: aggregations, distinct and drop duplicates, stream to stream joins, and custom stateful operators. Without a limit, that state grows forever. A watermark, set with withWatermark on the event time column and a delay like ten minutes, tells Spark how late an event can be. Spark keeps the state of a window until the end of the window is older than the latest event time seen, minus the delay, and then drops it, and an event later than that can be ignored. A short delay keeps less state and emits results sooner, but drops more late events, while a long delay tolerates more lateness, at the cost of more state and more latency.

Output modes

The output mode decides which rows a stateful query emits at each trigger, and it only matters for aggregations. Append, the default, emits only rows that will not change anymore: for a windowed aggregation, a window is written once the watermark passes its end. Update emits every row that changed during the trigger, so results are fresher, but the same window can be written several times. Complete rewrites the whole result table at every trigger, only works for aggregations, and performs poorly as data grows, so Databricks recommends materialized views instead. Joins only support append mode. And a Delta table sink accepts append and complete, but not update: to keep a Delta table current with changed rows, merge them in foreachBatch.

foreachBatch

foreachBatch calls your function with each micro batch, as a DataFrame, and its batch ID. Inside, you can use any batch API: merge the batch into a Delta table, write to a sink that has no streaming connector, or write the same batch to several places. foreachBatch only guarantees at least once delivery: after a failure, the same batch can run again. So make the writes idempotent: a merge on a key is safe to repeat, and for other Delta writes, the txnAppId and txnVersion options, with the version bound to the batch ID, let Delta skip a batch it already wrote. And handle empty batches, which can happen, for example after an OPTIMIZE command on the source table.

Checkpoints

The checkpoint location stores the progress of a query: the offsets it read, the batches it committed, its state, and its metadata. After a failure or a restart, the query resumes exactly where it stopped, and with an idempotent sink, like Delta, it produces exactly once results, with no double counting. Each streaming query needs its own checkpoint, in a volume or in cloud storage, and two queries must never share one. Deleting or changing the checkpoint restarts the query from scratch, as a new query. Some changes also need a new checkpoint: the number or the type of the sources, the subscribed Kafka topics, the stateful operation, or the schema of the state. Filters and trigger intervals can change safely, and on serverless notebooks and jobs, the trigger is available now, which processes what is new and then stops.

Recap

Let's recap. Stateful operations keep data between micro batches, and a watermark on the event time limits how long their state is kept. Append mode, the default, emits rows that will not change, update emits changed rows, and complete rewrites everything, so prefer materialized views. A Delta sink takes append or complete, so changed rows are merged in foreachBatch. foreachBatch delivers at least once, so make writes idempotent, with a merge, or with txnAppId and txnVersion. And each query has its own checkpoint, which gives exactly once recovery, while new sources or a new kind of state need a new one.

Quick checks for this lesson

The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.

  1. A streaming count of orders per restaurant and per 5-minute window has run for weeks, and its state keeps growing until the job runs out of memory. Events are never more than ten minutes late. What should the data engineer add?

    • A ten-minute watermark on the event time
    • The complete output mode
    • A larger serverless memory setting
    • A new checkpoint location each day
    Show the answer

    A ten-minute watermark on the event time

    A ten minute watermark lets Spark drop the state of windows that can no longer change, so the state stops growing. “The complete output mode” would emit every window at every trigger, and keep all the state. “A larger serverless memory setting” would only delay the failure, since the state would still grow without limit. And “A new checkpoint location each day” would restart the query from scratch each day, and lose its exactly once guarantee.

  2. A notification service must send exactly one alert per closed 5-minute window of late deliveries, and never send an update for a window it already reported. Which output mode fits?

    • Update
    • Complete
    • Append, with a watermark
    • Any mode: the query is stateless
    Show the answer

    Append, with a watermark

    Append mode emits each window once, when the watermark shows that it can no longer change. “Update” would emit a window again every time its count changes. “Complete” would emit every window at every trigger. And “Any mode: the query is stateless” is wrong, since a windowed count is a stateful aggregation.

  3. A team subscribes a running stateful query to a second Kafka topic, and keeps the same checkpoint location. The query fails at restart. What should the data engineer do?

    • Delete the state folder of the checkpoint
    • Restart with a longer watermark
    • Share one checkpoint between two queries
    • Restart the changed query on a new checkpoint
    Show the answer

    Restart the changed query on a new checkpoint

    A change to the subscribed topics changes the query, so it needs a new checkpoint, and it then starts as a new query. “Delete the state folder of the checkpoint” would corrupt the checkpoint, and lose the state that the query depends on. “Restart with a longer watermark” would not make the new topic compatible with the old checkpoint. And “Share one checkpoint between two queries” is never allowed, since each query needs its own checkpoint.