<?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=Stateful_stream_processing</id>
	<title>Stateful stream processing - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://openlakehouse.io/w/index.php?action=history&amp;feed=atom&amp;title=Stateful_stream_processing"/>
	<link rel="alternate" type="text/html" href="https://openlakehouse.io/w/index.php?title=Stateful_stream_processing&amp;action=history"/>
	<updated>2026-12-06T00:25:13Z</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=Stateful_stream_processing&amp;diff=206&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=Stateful_stream_processing&amp;diff=206&amp;oldid=prev"/>
		<updated>2026-11-25T00:52:59Z</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            = Stateful stream processing&lt;br /&gt;
| also_known_as   = Stateful streaming&lt;br /&gt;
| field           = [[Stream processing]]&lt;br /&gt;
| key_parts       = Keyed state, timers, time-to-live (TTL), [[watermark]], fault-tolerant state storage&lt;br /&gt;
| implementations = [[Structured Streaming]] (built-in stateful operators, [[transformWithState]]), [[Apache Flink]] (keyed state, &amp;lt;code&amp;gt;KeyedProcessFunction&amp;lt;/code&amp;gt;), Kafka Streams&lt;br /&gt;
| related         = [[State store]], [[RocksDB state store]], [[Streaming checkpoint]], [[mapGroupsWithState and flatMapGroupsWithState]]&lt;br /&gt;
}}&lt;br /&gt;
{{Verified|2026-11-23|the Spark 4.3.0 Structured Streaming programming guide (arbitrary stateful operations, unsupported operations) and transformWithState guide, &amp;lt;code&amp;gt;SQLConf.scala&amp;lt;/code&amp;gt; at tag v4.3.0 of apache/spark, SPARK-40925, the Databricks &amp;quot;What is stateful streaming?&amp;quot; page, and the Apache Flink 2.3 &amp;quot;Working with State&amp;quot;, &amp;quot;Process Function&amp;quot;, &amp;quot;State Backends&amp;quot; and &amp;quot;State Schema Evolution&amp;quot; pages}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Stateful stream processing&amp;#039;&amp;#039;&amp;#039; is stream processing in which the result for a record depends on information kept from earlier records, such as a running count, the last value seen for a key, or rows waiting for a join partner. The engine stores this &amp;#039;&amp;#039;&amp;#039;state&amp;#039;&amp;#039;&amp;#039;, usually partitioned by key, saves it to durable storage so it survives failures, and removes it when it is no longer needed. In [[Apache Spark]] [[Structured Streaming]], state is held in a [[state store]] and used by built-in operators (aggregations, deduplication, stream-stream joins) and by the [[transformWithState]] operator for custom logic.&amp;lt;ref name=&amp;quot;ss-store&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html#state-store |title=Structured Streaming: State Store (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;tws&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/structured-streaming-transform-with-state.html |title=Structured Streaming: transformWithState (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== What is the difference between stateless and stateful streaming? ==&lt;br /&gt;
A stateless query handles each record on its own: filters, projections, parsing, and joins with a static table. The only thing a stateless Structured Streaming query tracks is which input has been processed. A stateful query updates intermediate state in every batch.&amp;lt;ref name=&amp;quot;db-stateful&amp;quot;&amp;gt;{{Cite web |url=https://docs.databricks.com/aws/en/structured-streaming/stateful-streaming |title=What is stateful streaming? |publisher=Databricks |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; State makes results possible that depend on history, but it costs memory and storage, adds checkpointing time, and fixes parts of the query for the life of its checkpoint.&lt;br /&gt;
&lt;br /&gt;
== What are state, timers and TTL? ==&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;State&amp;#039;&amp;#039;&amp;#039; is data kept per key between records. In Spark&amp;#039;s built-in operators, the engine decides what to keep: partial aggregates per group and window, the keys already seen for deduplication, or buffered rows for a join.&amp;lt;ref name=&amp;quot;ss-store&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;&amp;#039;timer&amp;#039;&amp;#039;&amp;#039; is a callback that the application registers for a future time, so that it can act when nothing arrives, for example to close a session or raise an alert after silence. An event-time timer fires when the [[watermark]] passes its time; a processing-time timer fires when the wall clock does.&amp;lt;ref name=&amp;quot;flink-pf&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-release-2.3/docs/dev/datastream/operators/process_function/ |title=Process Function (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Time-to-live (TTL)&amp;#039;&amp;#039;&amp;#039; makes state expire a set time after it was written, so that state for keys that go quiet does not stay forever. Both Spark and Flink measure state TTL in processing time.&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;flink-state&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-release-2.3/docs/dev/datastream/fault-tolerance/state/ |title=Working with State (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How does Structured Streaming handle state? ==&lt;br /&gt;
Most stateful Spark queries use built-in operators, and Spark manages their state. Streaming aggregations, &amp;lt;code&amp;gt;dropDuplicates&amp;lt;/code&amp;gt; and stream-stream joins keep state per key in the state store and use the watermark to drop state that can no longer change; see [[Watermark]], [[Streaming deduplication]] and [[Stream-stream join]].&amp;lt;ref name=&amp;quot;ss-store&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For logic that does not fit them, Spark has arbitrary stateful operators. &amp;lt;code&amp;gt;mapGroupsWithState&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;flatMapGroupsWithState&amp;lt;/code&amp;gt; date from Spark 2.2 and are now called legacy; since Spark 4.0, the guide encourages &amp;lt;code&amp;gt;transformWithState&amp;lt;/code&amp;gt; instead.&amp;lt;ref name=&amp;quot;ss-arb&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/apis-on-dataframes-and-datasets.html#arbitrary-stateful-operations |title=Structured Streaming: Arbitrary Stateful Operations (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; In transformWithState, a stateful processor class declares named state variables of three types, &amp;#039;&amp;#039;&amp;#039;ValueState&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;ListState&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;MapState&amp;#039;&amp;#039;&amp;#039;, registers timers, sets optional TTL per variable and can be seeded with an initial state DataFrame. It is available in Scala, Java and Python.&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
transformWithState timers can be listed, added and removed; a key can have several; and timers are checkpointed, so they can fire after a restart. TTL works only with &amp;lt;code&amp;gt;TimeMode.ProcessingTime&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt; The operator requires the [[RocksDB state store]].&lt;br /&gt;
&lt;br /&gt;
Since Spark 3.4.0, a query can chain several stateful operators, for example an aggregation followed by a stream-stream join, in append mode. Late records are then filtered against the watermark of the previous micro-batch; &amp;lt;code&amp;gt;spark.sql.streaming.statefulOperator.allowMultiple=false&amp;lt;/code&amp;gt; restores the earlier behavior with a single stateful operator.&amp;lt;ref name=&amp;quot;jira40925&amp;quot;&amp;gt;{{Cite web |url=https://issues.apache.org/jira/browse/SPARK-40925 |title=SPARK-40925: Fix late record filtering to support chaining of stateful operators |publisher=Apache Software Foundation JIRA |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;sqlconf&amp;quot;&amp;gt;{{Cite web |url=https://github.com/apache/spark/blob/v4.3.0/sql/catalyst/src/main/scala/org/apache/spark/sql/internal/SQLConf.scala |title=SQLConf.scala (Apache Spark v4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; Chaining stateful operators is not supported in update or complete [[output mode]].&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;
== How does Flink handle state? ==&lt;br /&gt;
[[Apache Flink]]&amp;#039;s &amp;#039;&amp;#039;&amp;#039;keyed state&amp;#039;&amp;#039;&amp;#039; is scoped to the key of the current record and is available only on a &amp;lt;code&amp;gt;KeyedStream&amp;lt;/code&amp;gt;, created with &amp;lt;code&amp;gt;keyBy&amp;lt;/code&amp;gt;. The state primitives are &amp;lt;code&amp;gt;ValueState&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ListState&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ReducingState&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;AggregatingState&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;MapState&amp;lt;/code&amp;gt;, each created from a state descriptor.&amp;lt;ref name=&amp;quot;flink-state&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Flink timers are registered through the &amp;lt;code&amp;gt;TimerService&amp;lt;/code&amp;gt; of a &amp;lt;code&amp;gt;KeyedProcessFunction&amp;lt;/code&amp;gt;. When a timer fires, &amp;lt;code&amp;gt;onTimer&amp;lt;/code&amp;gt; runs with state scoped to the key that set the timer.&amp;lt;ref name=&amp;quot;flink-pf&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Flink TTL is configured with a &amp;lt;code&amp;gt;StateTtlConfig&amp;lt;/code&amp;gt; on any keyed state type, with per-entry TTL for list elements and map entries. The update type (by default &amp;lt;code&amp;gt;OnCreateAndWrite&amp;lt;/code&amp;gt;) decides when the TTL is refreshed, and the visibility setting decides whether expired values that have not been cleaned up yet can still be read. Only processing-time TTL is supported.&amp;lt;ref name=&amp;quot;flink-state&amp;quot; /&amp;gt; Flink keeps state in a state backend: the heap-based &amp;lt;code&amp;gt;HashMapStateBackend&amp;lt;/code&amp;gt; by default, or the &amp;lt;code&amp;gt;EmbeddedRocksDBStateBackend&amp;lt;/code&amp;gt;.&amp;lt;ref name=&amp;quot;flink-sb&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-release-2.3/docs/ops/state/state_backends/ |title=State Backends (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== transformWithState vs Flink keyed state ==&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! !! Spark &amp;lt;code&amp;gt;transformWithState&amp;lt;/code&amp;gt; !! Flink keyed state with &amp;lt;code&amp;gt;KeyedProcessFunction&amp;lt;/code&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Programming model&lt;br /&gt;
| A &amp;lt;code&amp;gt;StatefulProcessor&amp;lt;/code&amp;gt; class with &amp;lt;code&amp;gt;init&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;handleInputRows&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;handleExpiredTimer&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;handleInitialState&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;close&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| A &amp;lt;code&amp;gt;KeyedProcessFunction&amp;lt;/code&amp;gt; with &amp;lt;code&amp;gt;processElement&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;onTimer&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;flink-pf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Input per call&lt;br /&gt;
| All rows of a key in the micro-batch (one row in Real-time Mode)&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| One record&amp;lt;ref name=&amp;quot;flink-pf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! State types&lt;br /&gt;
| ValueState, ListState, MapState&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| ValueState, ListState, ReducingState, AggregatingState, MapState&amp;lt;ref name=&amp;quot;flink-state&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Timers&lt;br /&gt;
| Processing-time and event-time, several per key, checkpointed&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| Processing-time and event-time through the &amp;lt;code&amp;gt;TimerService&amp;lt;/code&amp;gt;&amp;lt;ref name=&amp;quot;flink-pf&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! TTL&lt;br /&gt;
| Per state variable, processing time only&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| Per state descriptor with &amp;lt;code&amp;gt;StateTtlConfig&amp;lt;/code&amp;gt;, processing time only, per entry for collections&amp;lt;ref name=&amp;quot;flink-state&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! Storage&lt;br /&gt;
| RocksDB state store, required&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| Heap (&amp;lt;code&amp;gt;HashMapStateBackend&amp;lt;/code&amp;gt;, default) or RocksDB&amp;lt;ref name=&amp;quot;flink-sb&amp;quot; /&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
! State schema changes&lt;br /&gt;
| State variables can be added or removed; value fields can evolve with Avro encoding&amp;lt;ref name=&amp;quot;tws&amp;quot; /&amp;gt;&lt;br /&gt;
| Migrated automatically when a job is restored from a savepoint with changed state types, for serializers that support it&amp;lt;ref name=&amp;quot;flink-se&amp;quot;&amp;gt;{{Cite web |url=https://nightlies.apache.org/flink/flink-docs-release-2.3/docs/dev/datastream/fault-tolerance/serialization/schema_evolution/ |title=State Schema Evolution (Apache Flink 2.3) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== What does state cost? ==&lt;br /&gt;
State has to be stored, checkpointed and restored. Databricks advises setting the number of shuffle partitions to one to two times the number of cluster cores for stateful queries, and notes that the partition count is fixed when the checkpoint is created.&amp;lt;ref name=&amp;quot;db-stateful&amp;quot; /&amp;gt; In open source Spark, &amp;lt;code&amp;gt;spark.sql.shuffle.partitions&amp;lt;/code&amp;gt; and the state store provider cannot change between restarts from the same checkpoint.&amp;lt;ref name=&amp;quot;misc&amp;quot;&amp;gt;{{Cite web |url=https://spark.apache.org/docs/4.3.0/streaming/additional-information.html |title=Structured Streaming: Miscellaneous Notes (Spark 4.3.0) |publisher=Apache Software Foundation |access-date=2026-11-23}}&amp;lt;/ref&amp;gt; See [[Streaming checkpoint]] for what else is fixed.&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[transformWithState]]&lt;br /&gt;
* [[State store]]&lt;br /&gt;
* [[RocksDB state store]]&lt;br /&gt;
* [[Watermark]]&lt;br /&gt;
* [[Stream-stream join]]&lt;br /&gt;
* [[Streaming deduplication]]&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:Pipelines and orchestration]]&lt;br /&gt;
[[Category:Apache Spark]]&lt;/div&gt;</summary>
		<author><name>Open Lakehouse Wiki sync</name></author>
	</entry>
</feed>