Jump to content

RocksDB state store

From Open Lakehouse Wiki
RocksDB state store
Also known as RocksDBStateStoreProvider
Field Stream processing
Introduced Apache Spark 3.2
Key parts One RocksDB instance per operator partition, local disk, uploaded SST files and .zip version files, optional changelog checkpointing, optional bounded memory
Open source implementations Structured Streaming in Apache Spark; a separate provider on Databricks
Related State store, Streaming checkpoint, transformWithState
Last verified 23 November 2026 against the Spark 4.3.0 Structured Streaming programming guide (RocksDB state store implementation, memory management, changelog checkpointing), SQLConf.scala, RocksDB.scala, RocksDBFileManager.scala and pom.xml at tag v4.3.0 of apache/spark, SPARK-43421, and the Databricks RocksDB state store page.

The RocksDB state store is a state store implementation for Apache Spark Structured Streaming that keeps the state of stateful operators in RocksDB, an embedded key-value database, in native memory and on the executor's local disk, and uploads its files to the streaming checkpoint for fault tolerance. It was added in Spark 3.2 for queries with large state, such as millions of keys, where the default HDFS-backed state store causes long JVM garbage collection pauses.[1] It is the only built-in provider that supports transformWithState.

How is the RocksDB state store enabled?

The provider is chosen with spark.sql.streaming.stateStore.providerClass. In Apache Spark, set it to org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider before a query first starts; the provider cannot be changed between restarts from the same checkpoint.[1][2]

spark.conf.set(
    "spark.sql.streaming.stateStore.providerClass",
    "org.apache.spark.sql.execution.streaming.state.RocksDBStateStoreProvider",
)
spark.conf.set(
    "spark.sql.streaming.stateStore.rocksdb.changelogCheckpointing.enabled", "true"
)

On Databricks, RocksDB is the default state store provider in Databricks Runtime 17.3 LTS and above. Older runtimes enable it with the provider class com.databricks.sql.streaming.state.RocksDBStateStoreProvider.[3]

Spark 4.3.0 is built against version 9.8.4 of the rocksdbjni library.[4] spark.sql.streaming.stateStore.rocksdb.formatVersion (default 5) is stored in the checkpoint when a query starts and used for its whole lifetime.[2]

How does it checkpoint state?

By default the RocksDB state store uses incremental snapshot checkpointing. At each commit it uploads the RocksDB manifest files and the SST data files that are new since the previous version.[5] In the checkpoint, SST and log files get unique names in shared directories, and each version is described by a version.zip file with the metadata that maps local file names to the uploaded files. Unlike the HDFS-backed store, which has to replay earlier .delta files, each .zip is a complete description of one version.[6]

What is changelog checkpointing?

With changelog checkpointing, each commit uploads only the changes made to the state since the last checkpoint, in a .changelog file, instead of a snapshot. Snapshots are still written periodically in the background, to bound recovery time and allow old changelogs to be removed. The Spark guide states that this avoids the cost of capturing and uploading snapshots and reduces streaming query latency.[5][6]

Changelog checkpointing was added in Spark 3.5.0 (SPARK-43421).[7] In Apache Spark it is off by default and is turned on with spark.sql.streaming.stateStore.rocksdb.changelogCheckpointing.enabled=true. It is compatible in both directions with snapshot checkpointing: an existing query can switch to changelog checkpointing, or back, on restart without discarding its state.[5] Snapshots are created after spark.sql.streaming.stateStore.minDeltasForSnapshot (default 10) changelog versions.[8][2]

On Databricks, changelog checkpointing is available in Databricks Runtime 13.3 LTS and above and enabled by default in 17.3 LTS and above; Databricks recommends it for all stateful Structured Streaming queries.[3]

How is RocksDB memory controlled?

RocksDB allocates memory for memtables (write buffers), the block cache and filter and index blocks. Left unbounded, the instances on one executor can together use enough memory to cause out-of-memory errors.[9]

Setting spark.sql.streaming.stateStore.rocksdb.boundedMemoryUsage=true caps the total memory of all RocksDB instances on a node at maxMemoryUsageMB (default 500), through RocksDB's write buffer manager. The cap is a soft limit: usage can briefly exceed it. Per-instance limits are set with writeBufferSizeMB and maxWriteBufferNumber, which by default use RocksDB's own defaults.[9]

Configuration

All keys start with spark.sql.streaming.stateStore.rocksdb.. Defaults are those of Spark 4.3.0.[1]

Key suffix Default Meaning
changelogCheckpointing.enabled false Upload changelogs instead of snapshots at commit
compactOnCommit false Run a range compaction at each commit
blockSizeKB 4 Approximate user data per block in a block-based SST table
blockCacheSizeMB 8 Block cache capacity
lockAcquireTimeoutMs 60000 Wait for the instance lock when loading
maxOpenFiles -1 Open file limit; -1 keeps all files open
resetStatsOnLoad true Reset RocksDB statistics on load
trackTotalNumberOfRows true Track total rows in state; costs an extra lookup per write
writeBufferSizeMB -1 Maximum memtable size; -1 uses the RocksDB default
maxWriteBufferNumber -1 Maximum number of memtables; -1 uses the RocksDB default
boundedMemoryUsage false Bound total RocksDB memory per node
maxMemoryUsageMB 500 Total memory limit per node when bounded
writeBufferCacheRatio 0.5 Share of the bound used for write buffers
highPriorityPoolRatio 0.1 Share of the bound used for high-priority blocks
allowFAllocate true Let RocksDB pre-allocate disk space with fallocate
compression lz4 RocksDB compression type

Turning off trackTotalNumberOfRows can improve write performance; the reported number of rows in state is then 0. It can be changed when the query restarts.[1]

Comparison with Flink's RocksDB backend

Apache Flink also has an embedded RocksDB state backend, EmbeddedRocksDBStateBackend. It stores state as serialized bytes, always takes asynchronous snapshots, and is the only Flink backend that offers incremental checkpoints.[10] Flink's default is the heap-based HashMapStateBackend, as Spark's default is the heap-based HDFS-backed state store.[10]

See also

References

  1. ↑ 1.0 1.1 1.2 1.3 Structured Streaming: RocksDB state store implementation (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 SQLConf.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 Configure RocksDB state store on Databricks. Databricks.Retrieved 2026-11-23.
  4. ↑ pom.xml (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 5.2 Structured Streaming: RocksDB State Store Changelog Checkpointing (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ 6.0 6.1 RocksDBFileManager.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ SPARK-43421: Implement changelog checkpointing for RocksDB state store. Apache Software Foundation JIRA.Retrieved 2026-11-23.
  8. ↑ RocksDB.scala (Apache Spark v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  9. ↑ 9.0 9.1 Structured Streaming: RocksDB State Store Memory Management (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.