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
- Key-changing operations: If an operation such as
selectKeyormapmodifies the key, Kafka Streams may need to repartition so that records with the same key are routed to the same stream task. - Grouping for aggregation: Aggregations (
count,reduce,aggregate) need data grouped by key. If the current partitioning does not match the keys, a repartition occurs. - 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
- Internal topic creation: Kafka Streams creates an internal Kafka topic for the repartition. Its name typically follows the pattern
applicationId-<name>-repartition. - Writing: Records that need repartitioning are written to this topic, partitioned by the new key (or the key derived from earlier transformations).
- Reading back: The repartitioned data is read back as a new
KStreamand 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:
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.
| Condition | Repartition Trigger |
| Key Modification | When operations like selectKey change the stream’s key. |
| Grouping for Aggregation | Necessary before operations like groupByKey. |
| Join Operations | Required 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.
Related reading
- What happens if I don't close the kafka producer
- What happens if offset specified by kafka consumer is not present in Broker?
- What happens if Zookeeper fails completely?
- What happens in Kafka when partitions are reassigned (esp. logsizes)?
- What happens to a Kafka consumer group when all consumers are removed
- What happens to consumer offsets if a new partition(s) is added to a Kafka topic?
- What happens to existing topic's partitions when a new broker is added to the Kafka cluster?
- What happens to fetched messages when RabbitMQ consumer crashes?
System design interview problems on this topic
- Design a Distributed Messaging System: solution and walkthrough
- Design a Log Collection and Analysis System: solution and walkthrough
- Design an Ad Click Aggregation System: solution and walkthrough
- Design a Webhook Notification Service: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.