Apache Kafka Partition Assignments

Problem Statement

Optimize the performance of the Apache Kafka cluster by smartly assigning Topic partitions to the various brokers. The goal is to minimize the latency between the different brokers (for, for instance, replication or election workloads) while dividing the partitions somewhat evenly over the different brokers.

Full example scenario

As a Data Engineer at a software company, you are tasked with optimizing the performance of your Apache Kafka cluster. Kafka topics are used to manage data streams, and each topic is divided into partitions. Efficient partitioning of these topics can significantly reduce latency and improve throughput. Given data about the cluster’s brokers and topics, which number of partitions should each topic have, and which brokers should host them, in order to maximize throughput by minimizing data transfer between brokers? For every topic, this transfer cost is calculated as the average transfer rate between all broker pairs, weighted by how many partitions of that topic each one holds.

  • The number of partitions assigned to each broker must not exceed that broker’s maximum capacity.

  • The number of partitions assigned to each topic must lie within its specified minimum and maximum range, and every topic must be assigned.

  • The total number of partitions assigned to each broker should be as balanced as possible, with no broker deviating more than 20% from the broker average.

  • For each topic, no broker can hold more than 40% of that topic’s partitions.

  • The brokers can be found in kafka_brokers.csv, with the columns Broker_ID,Max_Partitions, the topics can be found in kafka_topics.csv, with the columns Topic_ID,Min_Partitions,Max_Partitions, and the data transfer rates can be found in kafka_rates.csv, with the columns From_Broker,To_Broker,Transfer_Rate.

What an optimal solution looks like

Broker B1 — T1: 4 partitions Topic #1 4 partitions Broker B1 — T2: 3 partitions Topic #2 3 partitions Broker B1 — T3: 3 partitions Topic #3 3 partitions Broker B1 — T4: 3 partitions Topic #4 3 partitions Broker B1 — idle: 37 partitions 37 idle 🖥️ Broker B1 (13 occupied/50) Broker B2 — idle: 51 partitions Idle 51 partitions Broker B2 — T1: 4 partitions Topic #1 4 partitions Broker B2 — T3: 3 partitions Topic #3 3 partitions Broker B2 — T4: 2 partitions Topic #4 2 partitions 🖥️ Broker B2 (9 occupied/60) Broker B3 — T1: 2 partitions Topic #1 2 partitions Broker B3 — T2: 6 partitions Topic #2 6 partitions Broker B3 — T3: 2 partitions Topic #3 2 partitions Broker B3 — T4: 3 partitions Topic #4 3 partitions Broker B3 — idle: 42 partitions 42 idle 🖥️ Broker B3 (13 occupied/55) Broker B4 — idle: 55 partitions Idle 55 partitions Broker B4 — T2: 6 partitions Topic #2 6 partitions Broker B4 — T4: 4 partitions Topic #4 4 partitions 🖥️ Broker B4 (10 occupied/65) Broker 1 ↔ Broker 2 — T1: 4×4 partitions, rate 5 → cost 160 Broker 1 ↔ Broker 2 — T3: 3×3 partitions, rate 5 → cost 90 Broker 1 ↔ Broker 2 — T4: 3×2 partitions, rate 5 → cost 60 Broker 1 ↔ Broker 3 — T1: 4×2 partitions, rate 3 → cost 48 Broker 1 ↔ Broker 3 — T2: 3×6 partitions, rate 3 → cost 108 Broker 1 ↔ Broker 3 — T3: 3×2 partitions, rate 3 → cost 36 Broker 1 ↔ Broker 3 — T4: 3×3 partitions, rate 3 → cost 54 Broker 1 ↔ Broker 4 — T2: 3×6 partitions, rate 4 → cost 144 Broker 1 ↔ Broker 4 — T4: 3×4 partitions, rate 4 → cost 96 Broker 2 ↔ Broker 3 — T1: 4×2 partitions, rate 4 → cost 64 Broker 2 ↔ Broker 3 — T3: 3×2 partitions, rate 4 → cost 48 Broker 2 ↔ Broker 3 — T4: 2×3 partitions, rate 4 → cost 48 Broker 2 ↔ Broker 4 — T4: 2×4 partitions, rate 7 → cost 112 Broker 3 ↔ Broker 4 — T2: 6×6 partitions, rate 2 → cost 144 Broker 3 ↔ Broker 4 — T4: 3×4 partitions, rate 2 → cost 48 $160 $90 $60 $48 $108 $36 $54 $144 $96 $64 $48 $48 $112 $144 $48

Built with Modeler

The problem above is stated as a single shot: you hand over the whole problem and get a model back. Modeler takes the other route — you start with the business question you actually have, and it works with you until the problem is pinned down, the data is in, the code runs, and the answer holds up when the problem changes.

The tabs below walk the same Kafka partition-assignment problem through that route, one Modeler capability at a time.

Instead of writing the whole prompt up front, you start with the business question you actually have. Modeler searches the knowledge base, asks one clarifying question at a time — always the most important one first — and tells you which parts of the model are still missing. The options it offers come from examples in the knowledge base and from how similar problems are usually modeled in your industry, so you can answer with a number instead of prose.

I’m a data engineer at a software company running an Apache Kafka cluster. Each topic is split into partitions, and I need to decide how many partitions each topic should have and which brokers to place them on, so that inter-broker network traffic (from things like replication) stays low and no single broker gets overloaded. Can you help me build this as an optimization model?

This reads as an assignment problem — partitions of each topic are the items, brokers are the bins — with a traffic-minimization objective instead of a simple count. Let me check the knowledge base for how similar cluster-placement problems are usually modeled before asking anything.

The core driver of inter-broker traffic in Kafka is replication — each partition has a leader on one broker and replicas on others, and data flows from the leader to each replica. Before anything else, I need to know how precisely the user wants that traffic modeled, since it decides whether replication needs its own variables or can be approximated directly from where partitions land — so that has to be the first question.

Good, I have useful context from the knowledge base. Let me now start building the specification with you by asking questions one at a time.

Question 1 of ~4: Does every partition share the same replication factor, and should the model represent replication explicitly, or approximate inter-broker traffic some other way?

  1. All topics use the same global replication factor, modeled explicitly (e.g., 3 replicas per partition, each a separate placement decision).

  2. Each topic has its own fixed replication factor, modeled explicitly as an input.

  3. Replication is not modeled explicitly — inter-broker traffic is approximated purely from the pairwise transfer rates between brokers that co-host partitions of the same topic.

Which option best matches your situation, or would you like to describe it differently?

Option 3 — no explicit replication factor, just approximate inter-broker traffic from the pairwise transfer rate between two brokers, weighted by how many partitions of that topic each one holds — specifically as the product of the two partition counts, even though that makes the objective quadratic rather than linear; it’s the most faithful description of the actual cost. I have 4 brokers, each with a maximum partition capacity, and 4 topics, each with a minimum and maximum number of partitions it must be split into — every topic must be assigned. Two more things: I want the load balanced across brokers (no broker should end up more than 20% away from the average partition count across brokers), and for any single topic, no one broker should hold more than 40% of that topic’s partitions.

That single answer resolves the traffic-modeling question and volunteers everything else at once: the exact objective weighting (product of partition counts, accepted as quadratic), the topic partition-count ranges, the broker capacity constraint, a 20% load-balance rule, and a 40% per-topic concentration rule. Nothing about the objective or the constraints is still ambiguous — this is a non-convex MIQP, and the specification is buildable as-is.

Got it. Here is what I have captured so far:

• Objective: minimize the sum, over every topic and broker pair, of the transfer rate times the product of partition counts on the two brokers (quadratic, non-convex)
• 4 brokers, each with a maximum partition capacity; 4 topics, each with a minimum and maximum partition count — every topic must be assigned
• Every broker’s load within 20% of the fleet average
• No broker holding more than 40% of any single topic’s partitions

That’s a complete specification to build from — no open questions remain. I’ll save this and move on to bringing in your data.

Modeler updates the specification after every answer, even while it is still incomplete — so you can always see what it has understood and what it is still waiting on.