<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Streaming_deduplication</id>
	<title>Streaming deduplication - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Streaming_deduplication"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Streaming_deduplication&amp;action=history"/>
	<updated>2026-12-06T00:13:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.11</generator>
	<entry>
		<id>https://openlakehouse.io/w/index.php?title=Streaming_deduplication&amp;diff=209&amp;oldid=prev</id>
		<title>Open Lakehouse Wiki sync: Sync from open-lakehouse/wiki@7c9abaa</title>
		<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Streaming_deduplication&amp;diff=209&amp;oldid=prev"/>
		<updated>2026-11-25T00:53:00Z</updated>

		<summary type="html">&lt;p&gt;Sync from open-lakehouse/wiki@7c9abaa&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Infobox concept&lt;br /&gt;
| name            = Streaming deduplication&lt;br /&gt;
| also_known_as   = Stream deduplication&lt;br /&gt;
| field           = [[Stream processing]]&lt;br /&gt;
| key_parts       = Unique identifier column(s), [[watermark]], deduplication state in the [[state store]]&lt;br /&gt;
| implementations = [[Structured Streaming]]: &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dropDuplicatesWithinWatermark&amp;lt;/code&amp;gt; (Spark 3.5.0 and later); [[Delta Lake]] insert-only &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; in [[foreachBatch]]&lt;br /&gt;
| related         = [[Watermark]], [[Exactly-once semantics]], [[State store]], [[Stateful stream processing]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the Spark 4.3.0 Structured Streaming programming guide (streaming deduplication, recovery semantics) and Real-time Mode guide, &amp;lt;code&amp;gt;python/pyspark/sql/dataframe.py&amp;lt;/code&amp;gt; at tag v4.3.0 of apache/spark, SPARK-42931, and the Delta Lake merge documentation}}&lt;br /&gt;
&lt;br /&gt;
In [[Apache Spark]] [[Structured Streaming]], &amp;#039;&amp;#039;&amp;#039;streaming deduplication&amp;#039;&amp;#039;&amp;#039; is removing repeated records from a stream by a unique identifier, so that each event is passed on once even if the source delivers it several times. Spark keeps the identifiers it has already seen as state; &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; does this with or without a [[watermark]], and &amp;lt;code&amp;gt;dropDuplicatesWithinWatermark&amp;lt;/code&amp;gt;, added in Spark 3.5.0, deduplicates within the watermark delay when duplicates carry different event times.&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html#streaming-deduplication |title=Structured Streaming: Streaming Deduplication (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;jira&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-42931 |title=SPARK-42931: dropDuplicates within watermark |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Duplicates are common in streaming because producers and connectors retry, and at-least-once delivery means a record may arrive more than once.&lt;br /&gt;
&lt;br /&gt;
== How does dropDuplicates work on a stream? ==&lt;br /&gt;
On a batch DataFrame, &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; (available since Spark 1.4.0) simply drops duplicate rows. On a streaming DataFrame, it keeps the data it needs from all earlier triggers as state in order to drop duplicates in later ones.&amp;lt;ref name=&amp;quot;df&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/python/pyspark/sql/dataframe.py |title=python/pyspark/sql/dataframe.py (Apache Spark v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The Spark guide describes two ways to use it:&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Without a watermark&amp;#039;&amp;#039;&amp;#039;, there is no bound on when a duplicate may arrive, so the query keeps state for every record it has ever seen. State grows for the life of the query.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;With a watermark&amp;#039;&amp;#039;&amp;#039;, if duplicates arrive no later than a known bound, the query sets a watermark on an event-time column and deduplicates on both the identifier and the event-time column. Spark then drops state for records older than the watermark, which bounds state.&lt;br /&gt;
&lt;br /&gt;
Records older than the watermark are dropped, to avoid any chance of emitting a duplicate.&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
events = spark.readStream.table(&amp;quot;main.iot.raw_events&amp;quot;)   # columns: event_id, event_time, ...&lt;br /&gt;
&lt;br /&gt;
# Unbounded state: every event_id is remembered&lt;br /&gt;
dedup_all = events.dropDuplicates([&amp;quot;event_id&amp;quot;])&lt;br /&gt;
&lt;br /&gt;
# Bounded state: event_id plus event_time, cleaned by the watermark&lt;br /&gt;
dedup_bounded = (events&lt;br /&gt;
    .withWatermark(&amp;quot;event_time&amp;quot;, &amp;quot;10 seconds&amp;quot;)&lt;br /&gt;
    .dropDuplicates([&amp;quot;event_id&amp;quot;, &amp;quot;event_time&amp;quot;]))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What is dropDuplicatesWithinWatermark? ==&lt;br /&gt;
The bounded form of &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; works only if the event time is part of the identity of a record, so that true duplicates share it. Sometimes they do not, for example when a non-idempotent writer sets the event time at write time and a retried write gets a new one. &amp;lt;code&amp;gt;dropDuplicatesWithinWatermark&amp;lt;/code&amp;gt; handles this case: it deduplicates on the identifier alone, within the time range of the watermark.&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
dedup = (events&lt;br /&gt;
    .withWatermark(&amp;quot;event_time&amp;quot;, &amp;quot;10 hours&amp;quot;)&lt;br /&gt;
    .dropDuplicatesWithinWatermark([&amp;quot;event_id&amp;quot;]))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The guarantee is that events are deduplicated as long as the distance between the earliest and the latest copy is smaller than the watermark delay, so the delay should be longer than the largest time difference expected between duplicates. A watermark is required, and the operator works only on streaming DataFrames. Data older than the watermark is dropped.&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== dropDuplicates vs dropDuplicatesWithinWatermark ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; without watermark !! &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; with watermark !! &amp;lt;code&amp;gt;dropDuplicatesWithinWatermark&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Available since&lt;br /&gt;
| Spark 1.4.0&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
| Spark 1.4.0&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
| Spark 3.5.0&amp;lt;ref name=&amp;quot;jira&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Key&lt;br /&gt;
| Identifier columns&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
| Identifier and event-time columns&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
| Identifier columns only&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! State&lt;br /&gt;
| Unbounded&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
| Bounded by the watermark&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
| Bounded by the watermark&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Duplicates with different event times&lt;br /&gt;
| Removed&amp;lt;ref name=&amp;quot;ss-dedup&amp;quot; /&amp;gt;&lt;br /&gt;
| Not removed, because event time is part of the key&lt;br /&gt;
| Removed if within the watermark delay&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Batch DataFrames&lt;br /&gt;
| Supported&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
| Supported&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
| Streaming only&amp;lt;ref name=&amp;quot;df&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Real-time Mode (Spark 4.3.0)&lt;br /&gt;
| colspan=&amp;quot;2&amp;quot; | Supported&amp;lt;ref name=&amp;quot;rtm&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/real-time-mode.html |title=Real-time Mode (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
| Not supported&amp;lt;ref name=&amp;quot;rtm&amp;quot; /&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The deduplication columns are part of the query&amp;#039;s state schema: changing their number or type is not allowed between restarts from the same [[streaming checkpoint]].&amp;lt;ref name=&amp;quot;ss-changes&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html#recovery-semantics-after-changes-in-a-streaming-query |title=Structured Streaming: Recovery Semantics after Changes in a Streaming Query (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; The plain &amp;lt;code&amp;gt;distinct&amp;lt;/code&amp;gt; operation is not supported on streaming DataFrames.&amp;lt;ref name=&amp;quot;ss-unsup&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html#unsupported-operations |title=Structured Streaming: Unsupported Operations (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Deduplicating against a table ==&lt;br /&gt;
&amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; only removes duplicates that the query itself sees within its state. To avoid inserting a record that is already in the target table, for example one written by an earlier query or a backfill, the [[Delta Lake]] documentation uses an insert-only &amp;lt;code&amp;gt;MERGE&amp;lt;/code&amp;gt; inside [[foreachBatch]], which inserts only rows whose key is not yet in the table.&amp;lt;ref name=&amp;quot;delta-merge&amp;quot;&amp;gt;{{Cite web |url=https://docs.delta.io/latest/delta-update.html#upsert-from-streaming-queries-using-foreachbatch |title=Table deletes, updates, and merges: Upsert from streaming queries using foreachBatch |publisher=Delta Lake (Linux Foundation) |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
from delta.tables import DeltaTable&lt;br /&gt;
&lt;br /&gt;
target = DeltaTable.forName(spark, &amp;quot;main.iot.events&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
def insert_new(batch_df, batch_id):&lt;br /&gt;
    (target.alias(&amp;quot;t&amp;quot;)&lt;br /&gt;
        .merge(batch_df.dropDuplicates([&amp;quot;event_id&amp;quot;]).alias(&amp;quot;s&amp;quot;),&lt;br /&gt;
               &amp;quot;t.event_id = s.event_id&amp;quot;)&lt;br /&gt;
        .whenNotMatchedInsertAll()&lt;br /&gt;
        .execute())&lt;br /&gt;
&lt;br /&gt;
(dedup_bounded.writeStream&lt;br /&gt;
    .foreachBatch(insert_new)&lt;br /&gt;
    .option(&amp;quot;checkpointLocation&amp;quot;, &amp;quot;/checkpoints/iot_events&amp;quot;)&lt;br /&gt;
    .start())&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Deduplication in the stream and an insert-only merge into the table can be combined: the first bounds the work per batch, the second protects the table across queries and restarts.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Watermark]]&lt;br /&gt;
* [[Exactly-once semantics]]&lt;br /&gt;
* [[foreachBatch]]&lt;br /&gt;
* [[State store]]&lt;br /&gt;
* [[Stateful stream processing]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{Navbox open lakehouse}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Concepts]]&lt;br /&gt;
[[Category:Glossary terms]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;br /&gt;
[[Category:Pipelines and orchestration]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>