Kafka Stream
Repartition
Data Processing
Stream Processing
Distributed Systems

What happens when a repartition occurs in Kafka Streams?

Codemia Knowledge Hub ·

A repartition in Kafka Streams redistributes records across partitions by their key. Kafka Streams writes the records to an internal intermediate topic, partitioned on the new key, and reads them back, so that every record with the same key lands in the same stream task. It is needed when a later operation such as groupByKey, aggregate or join requires the data to be partitioned differently from the input topic.

When Kafka Streams repartitions

  1. Key-changing operations: If an operation such as selectKey or map modifies the key, Kafka Streams may need to repartition so that records with the same key are routed to the same stream task.
  2. Grouping for aggregation: Aggregations (count, reduce, aggregate) need data grouped by key. If the current partitioning does not match the keys, a repartition occurs.
  3. Joins: For both KStream-to-KStream and KTable-to-KTable joins, the records of both sides must be partitioned on the join key.

What happens during a repartition

  1. Internal topic creation: Kafka Streams creates an internal Kafka topic for the repartition. Its name typically follows the pattern applicationId-<name>-repartition.
  2. Writing: Records that need repartitioning are written to this topic, partitioned by the new key (or the key derived from earlier transformations).
  3. Reading back: The repartitioned data is read back as a new KStream and fed to the downstream joins or aggregations.

Example: counting clicks per user per hour

An application reads user click events and counts clicks per user per hour. If the incoming data is not keyed by user ID, the repartition looks like this:

java
1KStream<byte[], String> clicks = builder.stream("clicks-topic");
2
3KStream<String, String> clicksByUser = clicks.selectKey((key, value) -> extractUserId(value))
4                                             .repartition(Repartitioned.as("clicks-by-user-repartition"));
5
6KTable<Windowed<String>, Long> clicksPerUserPerHour = clicksByUser
7    .groupByKey(Grouped.with(Serdes.String(), Serdes.String()))
8    .windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofHours(1)))
9    .count();

selectKey changes the key to the user ID, so Kafka Streams creates a repartition topic. That guarantees all clicks from the same user reach the same task and the aggregation is correct.

Cost and operational impact

  • Performance: Writing to and reading from an intermediate topic uses extra resources and adds latency.
  • Scalability: Repartition topics can be scaled by increasing their partition count, like any Kafka topic.
  • Fault tolerance: Repartition topics get Kafka's usual fault tolerance through replication.
  • Monitoring: Kafka metrics, including counts of messages produced to and consumed from these topics, show how repartitioning is performing.
ConditionRepartition Trigger
Key ModificationWhen operations like selectKey change the stream’s key.
Grouping for AggregationNecessary before operations like groupByKey.
Join OperationsRequired when joining streams or tables on different keys.

Repartitioning co-locates data by key, which stateful operations depend on, at the price of extra resource use and latency.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design