liveBook · Manning

State

As explained in Kafka Streams in Action, Second Edition

State is retained information representing the accumulated condition or progress of a stream-processing computation.

State is retained information that lets a stream-processing computation recall earlier events and connect them to the current event. Stateless operations use only the current event; stateful operations retrieve, update, and store information from related events. section 7.0 section 7.1

A stateful operation commonly follows these steps:

  • Use a shared key, such as an identifier, to locate the state being tracked. section 7.1
  • Retrieve the current value associated with that key. section 7.1
  • Update or merge the value with the current event. section 7.1
  • Initialize the value when the key has no prior state. section 7.1

Accumulating poker scores

A player’s score resets after each round, but the application must retain earlier scores to calculate a total. Kafka Streams groups records by player ID, reduces each player’s scores with addition, and writes the resulting totals to a topic. section 7.2 section 7.2

Listing 7.2 Stateful function example
public int count(Widget widget) {

  int widgetCount = hashMap.compute(widget.id,
   (key, value) -> (value == null) ? 1 : value + 1)

  return widgetCount;
}

Shows a minimal Java stateful function that retrieves a count by widget ID, initializes missing values, and increments existing values. section 7.1

Listing 7.3 Performing a reduce in Kafka Streams
KStream<String, Double> pokerScoreStream = builder.stream("poker-game",
        Consumed.with(Serdes.String(), Serdes.Double()));

pokerScoreStream
        .groupByKey()        #1
        .reduce(Double::sum,    #2
                Materialized.with(Serdes.String(), Serdes.Double()))
        .toStream()                  #3
        .to("total-scores",
                Produced.with(Serdes.String(), Serdes.Double()));    #4

Shows Kafka Streams code that groups poker scores by key, reduces them with Double::sum, materializes the result, and writes totals. section 7.2

How Kafka Streams in Action, Second Edition covers it

How deep it goes
Defines state through keyed retrieval, updating, initialization, and reduction, while pointing toward state stores, joins, and timestamps.
Examples
Uses widget counting and per-player poker-score accumulation to demonstrate keyed state and reduction.
What it assumes
Assumes Kafka key-value records and introduces stateful processing by contrasting it with current-event-only operations.
What you can do after
Apply it via Kafka Streams to aggregate keyed events and retain results across an unbounded stream.
How it connects
Connects state to stateless operations, aggregation, keys, reduce operations, joins, state stores, and timestamps.

Also explained in

Compare how 39 books explain state →

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.