Repartitioning
A Restore can write the records of a topic to a target topic with a different number of partitions, for example to scale a topic up or down.
By default, a Restore writes every record to the partition it had in the backup, so the target topic needs at least as many partitions as the source topic.
To restore into a topic with a different number of partitions, add the topic-repartitioning plugin to the Restore.
The topicSelectors field of the plugin selects the target topics it applies to.
Without topicSelectors, the plugin applies to every topic of the Restore, see Enabling plugins for a subset of topics.
Example
Section titled “Example”This example restores production data into a QA environment, where topics have fewer partitions.
The following Restore writes the production clickstream topic with 12 partitions to the qa.clickstream topic with 3 partitions, and restores the sessions topic to qa.sessions without repartitioning.
It uses the most common scheme: each record goes to a partition based on a hash of its key, so records with the same key end up in the same partition.
apiVersion: kannika.io/v1alphakind: Restoremetadata: name: qa-restorespec: source: "my-storage" sink: "qa-kafka-cluster" config: topics: - source: clickstream target: qa.clickstream - source: sessions target: qa.sessions plugins: # Repartition only qa.clickstream into 3 partitions - name: topic-repartitioning topicSelectors: - name: literal: "qa.clickstream" spec: scheme: keyHash algorithm: murmur2 partitions: 3Choosing a scheme
Section titled “Choosing a scheme”The scheme field of the plugin decides how records are assigned to the partitions of the target topic.
- Repartitioning by key (
keyHash): the most common choice, it keeps records with the same key in order on one partition, which most consumers rely on. - Auto-repartitioning (
automatic): when you do not need control over which partition a record lands in. - Repartitioning by header (
headerHash): to keep records together by a header instead of the key. - Round-robin repartitioning (
roundRobin): to spread records evenly when the order per key does not matter. - Random repartitioning (
random): to spread records randomly when the order per key does not matter.
All schemes except automatic take the number of partitions of the target topic in partitions.
See the Topic Repartitioning plugin for the full reference, including the hash algorithms.
Repartitioning by key
Section titled “Repartitioning by key”The keyHash scheme assigns partitions based on a hash of the record key.
This is the most common choice: records with the same key stay in order on one partition, as Kafka does by default, and most consumers rely on that.
config: topics: - source: clickstream target: qa.clickstream - source: sessions target: qa.sessions plugins: # Repartition only qa.clickstream into 3 partitions by key - name: topic-repartitioning topicSelectors: - name: literal: "qa.clickstream" spec: scheme: keyHash partitions: 3Auto-repartitioning
Section titled “Auto-repartitioning”The automatic scheme removes the original partition from each record and lets Kafka assign the partition in the target topic.
Use it when you do not need control over which partition a record lands in.
config: topics: - source: clickstream target: qa.clickstream # qa.clickstream has fewer partitions than clickstream disablePreflightChecks: true - source: sessions target: qa.sessions plugins: # Repartition only qa.clickstream, Kafka assigns the partitions - name: topic-repartitioning topicSelectors: - name: literal: "qa.clickstream" spec: scheme: automaticRepartitioning by header
Section titled “Repartitioning by header”The headerHash scheme assigns partitions based on a hash of the value of the header set in name.
Use it to keep records together by a header instead of by the key.
config: topics: - source: clickstream target: qa.clickstream - source: sessions target: qa.sessions plugins: # Repartition only qa.clickstream into 3 partitions by the tenant header - name: topic-repartitioning topicSelectors: - name: literal: "qa.clickstream" spec: scheme: headerHash name: "tenant" partitions: 3Round-robin repartitioning
Section titled “Round-robin repartitioning”The roundRobin scheme spreads records evenly over the partitions, ignoring keys.
Use it to balance the target topic when the order per key does not matter.
config: topics: - source: clickstream target: qa.clickstream - source: sessions target: qa.sessions plugins: # Repartition only qa.clickstream evenly over 3 partitions - name: topic-repartitioning topicSelectors: - name: literal: "qa.clickstream" spec: scheme: roundRobin partitions: 3Random repartitioning
Section titled “Random repartitioning”The random scheme assigns each record to a random partition, ignoring keys.
Use it when the order per key does not matter and an even spread on average is enough.
config: topics: - source: clickstream target: qa.clickstream - source: sessions target: qa.sessions plugins: # Repartition only qa.clickstream randomly over 3 partitions - name: topic-repartitioning topicSelectors: - name: literal: "qa.clickstream" spec: scheme: random partitions: 3
