liveBook · Manning

State store

As explained in Kafka Streams in Action, Second Edition

A state store is a Kafka Streams component that persistently maintains intermediate processing state for an application.

A state store is Kafka Streams’ local key-value storage for stateful processing. DSL aggregations, reductions, joins, and KTables create stores automatically; persistent stores are the default. Stores hold per-key results while records flow through a topology, allowing processing to make decisions from prior records instead of treating every event independently. section 7.4 section 7.5 section 8.2

Stores are backed by Kafka changelog topics. Writes are recorded in the store and its changelog, so persistent stores retain local disk contents across restarts and in-memory stores can be restored after restart. For cached aggregations, changelog records are produced when the cache flushes at commit time or when full; with caching disabled, each record reaches the store and changelog. section 7.4

Figure 7.17 As the key-value records get written to the store, they also get written to the changelog topic for data durability.
figure

Follow the paired write: each key-value update enters the local store and its Kafka changelog, which supplies durability and restoration data. section 7.4

Treat store ownership, placement, and naming as deployment concerns:

  • A stateful task exclusively accesses its assigned store. Tasks share neither data nor a store, avoiding locking and concurrent access for that store. section 7.4.3 section 10.2.4
  • Kafka Streams creates store directories under STATE_DIR_CONFIG, then application ID, task ID, RocksDB implementation, and processor or supplied store name. Each instance sharing a filesystem needs a unique state directory. section 7.4.4
  • Name stores explicitly with Materialized.as(...). Generated names can shift when upstream operators change, creating a new empty store and changelog rather than restoring the former state. section 7.4.5 section 7.5
  • Provide Serdes for store keys and values where required, since stores work with byte arrays; a named Materialized store can also be made available to interactive queries. section 8.2 section 13.2

Name a counting store before evolving the topology

A minimal count creates a generated aggregation store. Make that identity stable by supplying Materialized.as("counting-store") to count(). If a filter is later inserted before count(), generated processor and store names may change; the explicit name prevents that name-shift failure mode. section 7.4.4 section 7.4.5

Use a store in a custom stock processor

For stock-trend analysis, create an in-memory key-value store through Stores, provide its StoreBuilder from ProcessorSupplier.stores(), then retrieve it in init() with ProcessorContext.getStateStore(). A scheduled punctuator iterates the store every 10 seconds of stream time and forwards only entries whose price or share differential reaches the threshold. section 10.2 section 10.2.1 section 10.2.4

A named store also supports direct inspection. Interactive Queries accesses local stateful results, while application instances divide stores by task and partition across machines. In tests, TopologyTestDriver can retrieve a KeyValueStore by name and assert that its contents match the latest output. section 13.2 section 13.3 section 14.1.4

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Explains store types, changelog restoration, task ownership, filesystem placement, naming, querying, custom access, and tests without detailing storage-engine internals.
Examples
Uses a key count, a filtered topology, stock-performance analysis, loan rollups, and poker-score reduction tests.
What it assumes
Builds from Kafka Streams stateful operations while briefly explaining tasks, partitions, changelogs, and serialization needs.
What you can do after
Operate/configure stateful Kafka Streams applications with stable store names, appropriate store types, query access, and store-level tests.
How it connects
Connects state stores to Kafka Streams, changelog topics, KTable, Materialized, Interactive Queries, Processor API, and TopologyTestDriver.

Also explained in

Compare how 3 books explain state store →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kafka Streams in Action, Second Edition. The section links point to the parts of the book it draws on.