Jump to content

State store

From Open Lakehouse Wiki
State store
Also known as State store provider
Field Stream processing
Key parts Versioned key-value store per operator and partition, state store provider class, checkpointed files, maintenance task
Open source implementations Structured Streaming: HDFSBackedStateStoreProvider (default) and RocksDBStateStoreProvider
Related RocksDB state store, Streaming checkpoint, Stateful stream processing, Watermark
Last verified 23 November 2026 against the Spark 4.3.0 Structured Streaming programming guide (State Store section) and miscellaneous notes, SQLConf.scala, HDFSBackedStateStoreProvider.scala and RocksDBFileManager.scala at tag v4.3.0 of apache/spark, the state data source guide, SPARK-45511, the Databricks RocksDB state store page, and the Apache Flink 2.3 "State Backends" page.

In Apache Spark Structured Streaming, a state store is the versioned key-value store in which stateful operators keep the data they carry from one micro-batch to the next, such as running aggregates, deduplication keys, buffered join rows and user state. Each stateful operator has one state store per shuffle partition, running on an executor, and every batch produces a new version that is saved to the streaming checkpoint so the state survives failures.[1] Spark ships two implementations, the HDFS-backed state store and the RocksDB state store, and the choice between them mostly decides how much state a query can hold without memory problems.

Which operators use the state store?

Streaming aggregations, streaming deduplication with dropDuplicates, stream-stream joins and the arbitrary stateful operators (mapGroupsWithState, flatMapGroupsWithState and transformWithState) all keep state in the state store.[1] Stateless operations, including stream-static joins, do not.[2] See Stateful stream processing.

How does the state store work?

A state store is a versioned key-value store with reads and writes. Updates are applied in sets, transactionally, and each set increments the store's version; a retried task can therefore re-run its updates against the correct earlier version.[1] The version of a store matches the batch id of the query.

The state store provider is set with spark.sql.streaming.stateStore.providerClass. Its default is org.apache.spark.sql.execution.streaming.state.HDFSBackedStateStoreProvider, and it cannot be changed between restarts from the same checkpoint location.[3][4] Users can also write their own provider by extending the StateStoreProvider interface.[1]

State is partitioned by hashing the grouping key, which is why spark.sql.shuffle.partitions cannot change for a query that already has a checkpoint.[4]

Spark tries to run each state store on the same executor in every batch, using the preferred-location feature of RDDs, because loading a store on a new executor means reading its checkpointed files. The preference is not a guarantee; raising spark.locality.wait makes it more likely to hold. A background maintenance task, run every spark.sql.streaming.stateStore.maintenanceInterval (default 60 seconds), runs background maintenance in every loaded provider and unloads providers that are no longer active on an executor.[1][3]

HDFS-backed vs RocksDB state store

The HDFS-backed state store keeps all of its data in an in-memory map in the executor JVM and saves it to files in an HDFS-compatible file system.[1] Each batch writes a version.delta file with that batch's changes, and Spark consolidates deltas into a version.snapshot file after spark.sql.streaming.stateStore.minDeltasForSnapshot (default 10) delta files.[5][3]

The RocksDB state store, added in Spark 3.2, keeps state in RocksDB in native memory and on local disk instead of the JVM heap, and uploads its files to the checkpoint location. The Spark guide recommends it for queries with millions of keys in state, where the HDFS-backed store causes long JVM garbage collection pauses and uneven micro-batch times.[1] It is the only provider that supports transformWithState. See RocksDB state store.

HDFS-backed state store RocksDB state store
Provider class HDFSBackedStateStoreProvider (default in Apache Spark)[3] RocksDBStateStoreProvider[1]
Where live state is held In-memory map on the executor JVM heap[1] RocksDB, in native memory and on local disk[1]
Checkpoint files .delta per batch, .snapshot after consolidation[5] A version.zip per version plus shared SST files; optionally changelog files[6]
State size limit in practice Executor heap; large state causes GC pauses[1] Local disk and the configured memory bound[1]
transformWithState Not supported Supported

On Databricks, RocksDB is the default state store provider in Databricks Runtime 17.3 LTS and above.[7]

How big does state get?

State size is driven by the number of distinct keys each operator keeps and how long it keeps them. A watermark bounds it for windowed aggregations, deduplication and stream-stream joins; without one, aggregation and deduplication state grows for as long as new keys arrive.[8] Each operator reports state metrics in the query progress, including the number of rows in state (numTotalStateRows), updated and removed rows.[1]

The state data source reads the state of a checkpoint as a batch DataFrame, which helps to inspect state size and contents. It is experimental and, as of Spark 4.0, read-only.[1][9]

spark.conf.set(
    "spark.sql.streaming.stateStore.providerClass",
    "org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider",
)  # set before the query first starts; fixed for its checkpoint

state = spark.read.format("statestore").load("/checkpoints/page_counts")
state.groupBy("partition_id").count().show()

Comparison with Flink state backends

Apache Flink calls the same component a state backend. Without other configuration it uses the HashMapStateBackend, which holds state as objects on the Java heap, much like Spark's HDFS-backed store. The EmbeddedRocksDBStateBackend keeps state as serialized bytes in RocksDB and is the only Flink backend that offers incremental checkpoints; the ForStStateBackend is experimental.[10]

See also

References

  1. ↑ 1.00 1.01 1.02 1.03 1.04 1.05 1.06 1.07 1.08 1.09 1.10 1.11 1.12 1.13 Structured Streaming: State Store (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ Structured Streaming: Stream-static Joins (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 3.3 SQLConf.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ 4.0 4.1 Structured Streaming: Miscellaneous Notes (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 HDFSBackedStateStoreProvider.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ RocksDBFileManager.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ Configure RocksDB state store on Databricks. Databricks.Retrieved 2026-11-23.
  8. ↑ Structured Streaming: Streaming Deduplication (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  9. ↑ SPARK-45511: SPIP: State Data Source - Reader. Apache Software Foundation JIRA.Retrieved 2026-11-23.