Skip to content

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.

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/v1alpha
kind: Restore
metadata:
name: qa-restore
spec:
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: 3

The scheme field of the plugin decides how records are assigned to the partitions of the target topic.

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.

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: 3

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: automatic

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: 3

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: 3

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