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




