liveBook · Manning

Count

As explained in Apache Kafka in Action

count is a Kafka Streams aggregation operation that maintains the number of records for each key or grouping and produces aggregate results.

Count is a stateful Kafka Streams aggregation that tracks how many records belong to each key. For example, grouping product price-change events by product ID produces the number of changes per product rather than one unreliable total across partitions. section 12.2.1

Counting price changes by product

A typical topology groups the stream by key and counts each group: prices.groupByKey().count(). For every new record, the aggregation ignores the record value and increments the previous count by one. A product receiving three price-change events therefore has a current count of three. section 12.2.1 section 12.4.2

The aggregation maintains more than a counter:

  • It starts each previously unseen key at the neutral state 0. section 12.4.2
  • It stores the current count in a table or state store, allowing later processing to use the aggregate. section 12.4.2
  • It relies on persistent state and a compacted changelog topic so a processor can recover after a crash instead of recounting everything. section 12.4.2
  • It counts independently per key because stream processing assigns partitions across application instances; it does not directly provide a global topic total. section 12.2.1
Figure 12.8 To count the number of messages per key, a state store with the current count per key is needed. The changes to the state store are stored in a compacted changelog topic.

The state-store and compacted-changelog diagram makes the durability path behind per-key counts concrete. section 12.4.2

How Apache Kafka in Action covers it

How deep it goes
Explains grouping, incrementing, initial state, state stores, partition limits, and crash recovery, but stops short of implementation internals.
Examples
Uses product price-change events, groupByKey().count(), per-key increments, and a state-store changelog diagram.
What it assumes
Assumes keyed stream records and basic partition processing while explaining aggregation state and initialization.
What you can do after
Apply it via Kafka Streams to maintain recoverable counts for each record key.
How it connects
Connects count with Kafka Streams, stateful processing, aggregations, state stores, changelog topics, tables, and partitioning.

Also explained in

Compare how 5 books explain count →

Related concepts

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