Cert EspressoLearn. Practise. Find your flow.

A LITTLE FOCUS.
A BIG NEXT STEP.

Your pace. Your space.
Databricks · Databricks Spark Developer Associate

Windows, Watermarks, and Streaming Deduplication

Databricks Spark Developer Associate · Lesson 16 of 18 · 2:06:36

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 Associate Developer for Apache Spark exam. In this lesson: basic operations on streams, tumbling, sliding, and session windows, watermarks, and streaming deduplication. Includes three original practice questions.

Study notes

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

Late phones, double events

The leaderboard now needs points for every five minute window, and phones that were offline send their events up to an hour late. Some events also arrive twice, and the team must decide how long to wait for late data, and how much state to keep. This lesson covers basic operations on streams, event time windows, watermarks, and streaming deduplication.

Basic operations on streams

Most DataFrame operations work on a streaming DataFrame exactly as in batch: select, where, withColumn, and stateless UDFs apply to each micro-batch. groupBy, followed by an aggregation such as a count or a sum, keeps a running result that Spark updates as new rows arrive, which makes the query stateful. A few operations are not supported on streams, such as sorting, which only works after an aggregation in complete mode, and Spark rejects them with an error when the query starts. A stream can also be joined with a static DataFrame, such as the table of players, which adds columns to each event without keeping any state.

Event time windows

Event time is the time inside the data, when the purchase happened on the phone, rather than the time it reached Spark. window groups rows by event time into intervals: with only a duration, such as five minutes, the windows are tumbling, fixed, and never overlap. With a slide interval as well, such as ten minute windows every five minutes, the windows are sliding, and overlap, so each row can count in several windows. session_window groups the events of each key that arrive close together, and closes the session after a gap with no activity. The window becomes a column of the result, with a start and an end, and you group by it, usually together with other columns.

Watermarks

Late data is normal: a phone that was offline sends its events when it reconnects, long after their window ended. withWatermark sets how late data can be, such as one hour, on the event time column, and it must come before the aggregation, on the same column. Spark tracks the latest event time it has seen, and once that time minus the delay passes the end of a window, the window is final, and its state is dropped. Data less late than the delay is always aggregated, while data later than that may be dropped, so the guarantee only works in one direction. Without a watermark, the state of every window would grow forever, and append mode could never output an aggregation.

Streaming deduplication

dropDuplicates works on streams too, and keeps the keys it has already seen in the state store. Without a watermark, that state grows forever, since Spark can never forget a key that might come back. With a watermark, and the event time column included in the deduplication columns, Spark drops the keys older than the watermark, which bounds the state. dropDuplicatesWithinWatermark, for streams only, removes the copies that arrive within the watermark delay of each other, even when their event times differ, such as an event resent with a new timestamp. It requires a watermark, and suits copies with different timestamps, while dropDuplicates with the event time suits copies that share the same timestamp.

Recap

Let's recap. select, where, and withColumn work on streams as in batch, an aggregation keeps a running result, and sorting needs an aggregation in complete mode. window with a duration gives tumbling windows, a slide interval gives sliding windows, and session_window groups bursts of activity. withWatermark, before the aggregation and on the same column, bounds the state, and decides when a window is final. Data less late than the delay always counts, while later data may be dropped. And streaming deduplication needs a watermark to bound its state: dropDuplicates with the event time, or dropDuplicatesWithinWatermark for copies with different timestamps.

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 developer sorts a streaming DataFrame of purchases by amount, with no aggregation, and starts the query in append mode. What happens?

    • The query fails to start, with an error
    • Each micro-batch is sorted on its own
    • Spark sorts all the purchases seen so far
    • The sort is ignored: rows come in arrival order
    Show the answer

    The query fails to start, with an error

    Sorting a stream is only supported after an aggregation, in complete mode, so Spark rejects the query when it starts. “Each micro-batch is sorted on its own” is wrong, since Spark does not quietly sort each micro-batch instead. “Spark sorts all the purchases seen so far” would need the complete mode, after an aggregation. And “The sort is ignored: rows come in arrival order” is wrong, since the sort raises an error, rather than being ignored.

  2. A query sets a watermark of 1 hour on event time, then sums points per 5-minute window. The latest event time seen is 15:20. What happens to an event from 14:05 that arrives now?

    • It is always added to its window
    • It opens a new window that starts at 15:20
    • It may be dropped: its window is already final
    • The query fails: the event is too late
    Show the answer

    It may be dropped: its window is already final

    The watermark is twenty past two, one hour before the latest event time, so the window that starts at five past two is final, and the event may be dropped. “It is always added to its window” is only guaranteed for data less late than the delay. “It opens a new window that starts at 15:20” is wrong, since an event always belongs to the window of its own event time. And “The query fails: the event is too late” is wrong, since late data never stops the query.

  3. The game resends some events with the same event_id but a new timestamp, up to 10 minutes later, and the state must stay bounded. Which approach fits?

    • dropDuplicates(["event_id"]), no watermark
    • dropDuplicatesWithinWatermark(["event_id"]), 30-minute watermark
    • dropDuplicates(["event_id", "event_time"]), 30-minute watermark
    • dropDuplicatesWithinWatermark(["event_id"]), no watermark
    Show the answer

    dropDuplicatesWithinWatermark(["event_id"]), 30-minute watermark

    DropDuplicatesWithinWatermark removes the copies within the delay of each other, even with different timestamps, and the watermark bounds the state. “dropDuplicates(["event_id"]), no watermark” would keep every event ID in state forever. “dropDuplicates(["event_id", "event_time"]), 30-minute watermark” would keep both copies, since their timestamps differ, so their keys differ. And “dropDuplicatesWithinWatermark(["event_id"]), no watermark” is wrong, since this method requires a watermark.