Kafka Broker
A Kafka broker is a server that runs Apache Kafka and is responsible for receiving, storing and delivering messages. Brokers are the workhorses of every Kafka system: all data flows through them and is stored on them.
This guide explains what a broker does, how it handles requests and stores data on disk, how brokers share partition leadership, the most important configuration settings, how to run and inspect brokers, and how to add or remove them safely. For how brokers fit into the whole system, see Kafka Architecture.
1. What Is a Kafka Broker?
In simple words, a Kafka broker is the middleman between applications that send data (producers) and applications that read data (consumers). When an application sends a message to Kafka, a broker stores it safely on disk. When another application wants that message, a broker delivers it.
A Kafka system usually has several brokers working together as a cluster. Each broker is identified by a unique number, its node ID (or broker ID), and stores part of the data.
2. A Simple Example: The Post Office
Imagine Kafka as a postal system:
- Producers are people who send letters.
- Consumers are people who collect letters.
- A broker is a post office that stores the letters and hands them out.
When one post office is too busy, several post offices (brokers) share the work, and each keeps copies of important letters, so nothing is lost if one office closes. Unlike a real post office, a Kafka broker keeps the letters after delivery for a set time, so other people can read them too.
3. What a Broker Does
| Responsibility | What it means |
|---|---|
| Accept writes | Receive messages from producers and append them to partition logs |
| Serve reads | Return messages to consumers, starting from the offset they ask for |
| Store data | Keep partition logs on disk according to retention rules |
| Replicate | Copy data from partition leaders on other brokers, and send data to followers |
| Lead partitions | Act as the leader for some partitions and a follower for others |
| Coordinate consumer groups | Some brokers act as group coordinators, tracking group members and committed offsets |
| Answer metadata requests | Tell clients which broker leads each partition |
| Enforce security | Encrypt connections, authenticate clients and check permissions |
4. Key Features of a Kafka Broker
| Feature | Description |
|---|---|
| Message storage | Messages are stored on disk, so data is not lost when applications stop or restart; it remains available until its retention time expires |
| High performance | Efficient disk and network handling lets a broker process very large numbers of messages per second |
| Scalability | Add brokers when data grows; partitions can be moved onto them to share the load |
| Fault tolerance | Data is replicated across brokers, so if one fails, another already has a copy |
| Load balancing | Partition leadership is spread across brokers, so no single broker carries all the traffic |
| Multiple consumers | Many consumer groups can read the same data independently |
| Retention control | Data can be kept for hours, days or indefinitely, by time or by size, per topic |
5. How a Broker Handles Requests
A broker handles thousands of client connections at once using a small set of thread pools:
Clients (producers / consumers)
|
v
[ Network threads ] read requests from sockets, send responses
| (num.network.threads, default 3)
v
[ Request queue ]
|
v
[ I/O threads ] process requests: append to log, read from log
| (num.io.threads, default 8)
v
[ Partition logs on disk / page cache ]
- Network threads accept connections, read incoming requests and put them in a request queue.
- I/O threads take requests from the queue and do the work: appending messages to the log or reading them.
- For
acks=allwrites and long-polling fetches, requests can wait in purgatory until followers have copied the data or new data arrives, without blocking a thread. - Responses go back through the network threads to the client.
This design, plus batching and the operating system’s page cache, is what lets a single broker serve very high traffic.
6. How a Broker Stores Data on Disk
Each partition a broker hosts is a folder inside its log directory, named topic-partition. The folder contains segment files:
/var/lib/kafka/data/
orders-0/
00000000000000000000.log <- messages, offsets 0 .. 52309
00000000000000000000.index <- maps offsets to file positions
00000000000000000000.timeindex <- maps timestamps to offsets
00000000000000052310.log <- active segment (new data goes here)
00000000000000052310.index
00000000000000052310.timeindex
leader-epoch-checkpoint
partition.metadata
orders-1/
payments-0/
__consumer_offsets-12/
| File | Purpose |
|---|---|
.log | The messages themselves, appended in order |
.index | Finds where a given offset is in the log file, so reads are fast |
.timeindex | Finds the offset for a given timestamp, for example “read from yesterday 10:00” |
| File name number | The first offset stored in that segment |
New messages always go to the newest, active segment. When it reaches a size or age limit (by default 1 GB, log.segment.bytes), the broker starts a new segment. Retention then deletes or compacts whole old segments, which is very cheap compared with deleting individual messages.
7. Partition Leaders and Followers on Brokers
Every partition has one leader replica and zero or more followers, each on a different broker. A broker is therefore usually a leader for some partitions and a follower for others:
Broker 1 Broker 2 Broker 3
orders-0 LEADER follower follower
orders-1 follower LEADER follower
orders-2 follower follower LEADER
- Producers and consumers talk to the leader of each partition.
- Followers continuously fetch new data from the leader to stay in sync.
- The first replica in each partition’s list is the preferred leader. After a broker restart, Kafka moves leadership back to preferred leaders automatically (
auto.leader.rebalance.enable=true), so the load stays balanced.
See Kafka Fault Tolerance for how leaders are replaced when a broker fails.
8. Broker and Controller Roles in KRaft
In KRaft mode, each Kafka node has one or both of two roles, set with process.roles:
| Role | Job | process.roles |
|---|---|---|
| Broker | Stores data and serves producers and consumers | broker |
| Controller | Manages cluster metadata and elects partition leaders | controller |
| Combined | Does both on one node | broker,controller |
Combined mode is convenient for development and small clusters. For production, dedicated controllers (usually three) and separate brokers are recommended, so heavy data traffic cannot slow down cluster management. See Kafka KRaft.
9. Important Broker Configuration
A broker is configured in server.properties. This example is a combined node in a three-node KRaft cluster:
# ---- Identity and role ----
node.id=1
process.roles=broker,controller
controller.quorum.voters=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093
# ---- Network ----
listeners=PLAINTEXT://:9092,CONTROLLER://:9093
advertised.listeners=PLAINTEXT://kafka1:9092
controller.listener.names=CONTROLLER
listener.security.protocol.map=PLAINTEXT:PLAINTEXT,CONTROLLER:PLAINTEXT
# ---- Storage ----
log.dirs=/var/lib/kafka/data
# ---- Safe defaults for new topics ----
num.partitions=3
default.replication.factor=3
min.insync.replicas=2
offsets.topic.replication.factor=3
# ---- Retention ----
# Keep data for 7 days
log.retention.hours=168
# Start a new segment file every 1 GB
log.segment.bytes=1073741824
# ---- Threads ----
num.network.threads=3
num.io.threads=8
| Setting | Purpose |
|---|---|
node.id | Unique number for this node in the cluster |
process.roles | broker, controller or both |
controller.quorum.voters | The controller nodes; newer versions can also use controller.quorum.bootstrap.servers for dynamic quorums |
listeners / advertised.listeners | Where the broker listens, and the address it tells clients to use |
log.dirs | Folders where partition data is stored; several disks can be listed |
default.replication.factor | Number of copies for automatically created topics |
min.insync.replicas | Minimum in-sync copies for acks=all writes |
log.retention.hours / .bytes | How long, or how much, data to keep by default |
auto.create.topics.enable | Whether topics are created automatically when first used; often disabled in production |
Many settings can be changed while the broker is running, without a restart, using kafka-configs.sh (dynamic broker configuration).
10. Listeners and Advertised Listeners
Listeners are the most common source of connection problems. Two settings matter:
listeners: the network interfaces and ports the broker listens on.advertised.listeners: the address the broker tells clients to use for later connections.
1. Client connects to a bootstrap server: kafka1:9092
2. Broker replies with cluster metadata: "partition orders-1 is led by kafka2:9092"
3. Client connects directly to: kafka2:9092 <- an advertised listener
If advertised.listeners contains an address the client cannot reach, such as a Docker-internal host name or localhost on a remote server, the first connection works but every later request fails. Use separate listeners for different networks, for example one for clients inside Docker and one for clients on your machine:
listeners=INTERNAL://:29092,EXTERNAL://:9092,CONTROLLER://:9093
advertised.listeners=INTERNAL://kafka1:29092,EXTERNAL://localhost:9092
listener.security.protocol.map=INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,CONTROLLER:PLAINTEXT
inter.broker.listener.name=INTERNAL
controller.listener.names=CONTROLLER
11. Running a Kafka Broker
11.1 With Docker
The official image starts a single combined broker and controller in KRaft mode with sensible defaults:
docker run -d --name kafka -p 9092:9092 apache/kafka:latest
11.2 From the Kafka download
In KRaft mode, a new node’s storage must be formatted once with a cluster ID before the first start:
# 1. Generate a cluster ID (once per cluster)
KAFKA_CLUSTER_ID="$(bin/kafka-storage.sh random-uuid)"
# 2. Format the log directories of this node
bin/kafka-storage.sh format -t "$KAFKA_CLUSTER_ID" -c config/server.properties
# 3. Start the broker
bin/kafka-server-start.sh config/server.properties
Every node in the same cluster must be formatted with the same cluster ID. Stop a broker gracefully with bin/kafka-server-stop.sh, so it can move leadership away before shutting down.
12. Inspecting Brokers from the Command Line
# Which brokers are reachable, and which API versions do they support?
bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092
# Disk usage of each partition on each broker
bin/kafka-log-dirs.sh --bootstrap-server localhost:9092 --describe
# Where are the leaders and replicas of a topic?
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic orders
# Show a broker's configuration
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type brokers --entity-name 1 --describe --all
# Change a setting on a running broker (no restart)
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type brokers --entity-name 1 --alter --add-config log.cleaner.threads=2
# KRaft: show the controller quorum
bin/kafka-metadata-quorum.sh --bootstrap-server localhost:9092 describe --status
13. Inspecting Brokers with Java
The Admin client lets applications and tools inspect the cluster. This example lists the brokers, the active controller and one broker’s retention setting:
package com.example.kafka;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.Config;
import org.apache.kafka.clients.admin.DescribeClusterResult;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.config.ConfigResource;
public class BrokerInspector {
public static void main(String[] args) throws Exception {
Properties props = new Properties();
props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
try (Admin admin = Admin.create(props)) {
DescribeClusterResult cluster = admin.describeCluster();
System.out.println("Cluster ID: " + cluster.clusterId().get());
System.out.println("Controller: " + cluster.controller().get());
Collection<Node> brokers = cluster.nodes().get();
for (Node broker : brokers) {
System.out.printf("Broker %d at %s:%d (rack: %s)%n",
broker.id(), broker.host(), broker.port(), broker.rack());
}
// Read one broker setting
ConfigResource broker1 = new ConfigResource(ConfigResource.Type.BROKER, "1");
Map<ConfigResource, Config> configs = admin.describeConfigs(List.of(broker1)).all().get();
System.out.println("log.retention.hours = "
+ configs.get(broker1).get("log.retention.hours").value());
}
}
}
Cluster ID: 4L6g3nShT-eMCtK--X86sw
Controller: kafka2:9092 (id: 2 rack: null)
Broker 1 at kafka1:9092 (rack: eu-west-1a)
Broker 2 at kafka2:9092 (rack: eu-west-1b)
Broker 3 at kafka3:9092 (rack: eu-west-1c)
log.retention.hours = 168
14. Adding and Removing Brokers
14.1 Adding a broker
- Start the new broker with a new, unique
node.id, formatted with the cluster’s ID. - It joins the cluster, but existing partitions do not move automatically; only new topics use it.
- Move some partitions onto it with
kafka-reassign-partitions.sh, or a tool such as Cruise Control, to balance the load.
# 1. Generate a proposed plan that includes the new broker 4
bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 \
--topics-to-move-json-file topics.json --broker-list "1,2,3,4" --generate
# 2. Save the proposed plan as plan.json, then apply it
bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 \
--reassignment-json-file plan.json --execute
# 3. Check progress
bin/kafka-reassign-partitions.sh --bootstrap-server localhost:9092 \
--reassignment-json-file plan.json --verify
14.2 Removing a broker
- Move all its partitions to other brokers with a reassignment.
- Wait until the reassignment has finished and every partition is fully in sync.
- Stop the broker gracefully, and in KRaft mode unregister it from the cluster metadata.
14.3 Rolling restarts
To upgrade or reconfigure brokers without downtime, restart them one at a time, waiting until there are no under-replicated partitions before moving to the next broker.
15. Kafka Broker vs Kafka Cluster
| Kafka broker | Kafka cluster | |
|---|---|---|
| What it is | A single Kafka server | A group of brokers (and controllers) working together |
| Stores | Some of the partitions | All partitions of all topics |
| Failure impact | Partitions it leads move to other brokers | Survives broker failures if data is replicated |
| Scaling | Limited by one machine | Add brokers to grow capacity |
| Use | Development and testing | Production systems |
Most production systems use a cluster of at least three brokers. See Kafka Cluster.
16. Sizing and Hardware Tips
- Disk: the most important resource. Size it for incoming data per day × retention days × replication factor, plus headroom. Several disks can be listed in
log.dirs. - Memory: a modest Java heap (often 4–8 GB) is enough; leave the rest of the RAM to the operating system’s page cache, which Kafka relies on heavily.
- Network: often the real limit; replication multiplies the traffic between brokers.
- CPU: matters most for compression, TLS encryption and many small requests.
- Separate machines: do not share broker disks with other busy applications.
Incoming data: 200 GB per day
Retention: 7 days
Replication factor: 3
Total storage = 200 GB x 7 x 3 = 4,200 GB
Across 3 brokers = about 1,400 GB each, plus ~30% headroom -> ~1.8 TB per broker
17. Monitoring Brokers
| Metric | What it tells you | Healthy value |
|---|---|---|
UnderReplicatedPartitions | Partitions missing in-sync copies on this broker | 0 |
OfflinePartitionsCount | Partitions with no leader | 0 |
RequestHandlerAvgIdlePercent | How busy the I/O threads are | Well above 0.3 |
NetworkProcessorAvgIdlePercent | How busy the network threads are | Well above 0.3 |
BytesInPerSec / BytesOutPerSec | Traffic in and out of the broker | Within network capacity |
| Request latency (produce, fetch) | How fast the broker answers | Stable and low |
| Disk usage | Free space in log.dirs | Comfortably below full |
Brokers expose these metrics through JMX. Tools such as a Prometheus JMX exporter and Grafana dashboards make them easy to watch and alert on.
18. Best Practices
- Run at least three brokers, spread across racks or availability zones (
broker.rack). - Set safe defaults:
default.replication.factor=3andmin.insync.replicas=2. - Disable automatic topic creation in production and create topics deliberately.
- Configure
advertised.listenerscarefully for every network clients use. - Use dedicated controllers for larger production clusters.
- Leave most RAM to the page cache, with a moderate Java heap.
- Enable security: TLS, authentication and ACLs.
- Restart brokers one at a time, waiting for full replication between restarts.
- Rebalance partitions after adding or before removing brokers.
- Monitor under-replicated partitions, disk space and request latency.
19. Common Mistakes to Avoid
| Mistake | What goes wrong | Better approach |
|---|---|---|
Wrong advertised.listeners | Clients connect once, then fail with timeouts | Advertise addresses clients can actually reach |
| A single broker in production | Any failure stops the whole system | Run at least three brokers |
| Giving Kafka a huge Java heap | Less memory left for the page cache; slower reads | Moderate heap; leave RAM to the OS |
| Expecting new brokers to take load automatically | New brokers sit idle | Reassign partitions after adding brokers |
| Restarting several brokers at once | Partitions go offline | Rolling restarts, one broker at a time |
| Running out of disk | Brokers stop accepting writes | Monitor disk and size retention properly |
Duplicate node.id values | Brokers conflict and fail to join | Give every node a unique ID |
20. Summary
A Kafka broker is the core building block of Apache Kafka. It accepts messages from producers, stores them on disk in partition logs made of segment files, replicates them to other brokers, and delivers them to consumers. Brokers share the work by leading some partitions and following others, and in KRaft mode they can also act as controllers. Configure listeners and safe replication defaults carefully, run at least three brokers across zones, rebalance partitions when the cluster changes, and monitor replication, disk and latency. With these practices, brokers keep data fast, safe and scalable for modern microservices and event-driven systems. Continue with Kafka Cluster to see how brokers work together.
21. Frequently Asked Questions (FAQ)
Q1. What is a Kafka broker in simple words?
A Kafka broker is a server running Kafka. It receives messages from producers, stores them on disk, and delivers them to consumers when they ask.
Several brokers usually work together as a cluster, each storing part of the data and copies of other brokers’ data for safety.
Q2. How many brokers does a Kafka cluster need?
For learning and development, one broker is enough. For production, use at least three, so each partition can have three copies on different brokers and the cluster survives the loss of one broker while still accepting safe writes.
Large companies run clusters with dozens or hundreds of brokers; add brokers as storage and traffic grow.
Q3. What is the difference between a broker and a controller?
- A broker stores data and serves producers and consumers.
- A controller manages the cluster: it tracks which brokers are alive, stores topic and partition metadata, and elects partition leaders.
In KRaft mode, a node can be a broker, a controller or both (process.roles). Production clusters usually run three dedicated controllers.
Q4. What is a broker ID or node ID?
It is a unique number that identifies each Kafka node in a cluster, set with node.id in KRaft mode (broker.id in older versions).
Kafka uses it everywhere: replica lists such as Replicas: 1,2,3 refer to node IDs. Never give two nodes the same ID, and keep a node’s ID when you restart or replace its hardware.
Q5. What is a bootstrap server?
A bootstrap server is a broker address a client uses for its first connection, for example bootstrap.servers=kafka1:9092,kafka2:9092. From it, the client downloads the cluster metadata and then connects directly to the brokers that lead the partitions it needs.
List two or three brokers, so the client can still start if one of them is down.
Q6. Why can my client connect to Kafka but not send or receive messages?
Almost always because of advertised.listeners. The first connection to the bootstrap address works, but the broker then tells the client to use an address it cannot reach, such as a Docker-internal host name or localhost on another machine.
Fix it by advertising an address the client can resolve and reach, and use separate listeners for different networks, such as inside and outside Docker.
Q7. Where does a Kafka broker store data?
In the folders listed in log.dirs. Each partition has its own folder, such as orders-0, containing segment files: .log files with the messages, plus .index and .timeindex files for fast lookups.
Several disks can be listed in log.dirs; Kafka spreads partitions across them.
Q8. What is a log segment?
A partition’s log is split into segment files. New messages are always written to the latest (active) segment; when it reaches a size limit, 1 GB by default, or an age limit, the broker starts a new one.
Retention removes data by deleting whole old segments, which is fast and avoids rewriting files.
Q9. Does a broker delete messages after consumers read them?
No. Reading a message does not remove it. The broker keeps messages until the topic’s retention rules remove them, for example after 7 days or when the topic exceeds a size limit.
This lets many consumer groups read the same data and lets consumers replay old messages.
Q10. What happens when a broker fails?
The controller notices the broker has stopped sending heartbeats. For every partition that broker was leading, it elects a new leader from the in-sync replicas on other brokers. Producers and consumers switch to the new leaders automatically.
With replication factor 3, no acknowledged data is lost. When the broker returns, it catches up from the current leaders.
Q11. Do partitions move automatically when I add a new broker?
No. A new broker joins the cluster, but existing partitions stay where they are; only new topics or partitions may be placed on it.
To use the new broker, move some partitions with kafka-reassign-partitions.sh or an automated tool such as Cruise Control. Reassignment copies data in the background, so it can take time for large partitions.
Q12. What is the preferred leader?
The preferred leader of a partition is the first broker in its replica list. Kafka spreads preferred leaders evenly across brokers when topics are created.
After failures, leadership may pile up on some brokers. With auto.leader.rebalance.enable=true, the default, Kafka periodically moves leadership back to preferred leaders to keep the load balanced.
Q13. How much memory does a Kafka broker need?
Less Java heap than you might expect: a few gigabytes, often 4–8 GB, is usually enough. The rest of the machine’s memory should be left free for the operating system’s page cache, which Kafka uses to serve recent data quickly.
A machine with 32–64 GB of RAM and a 6 GB heap is a common starting point for production brokers.
Q14. How much disk space does a broker need?
Estimate it like this:
total = daily data x retention days x replication factor
per broker = total / number of brokers, plus 30-50% headroom
For example, 100 GB per day, 7 days, replication factor 3 and 3 brokers gives about 700 GB per broker, so plan for roughly 1 TB each. Always monitor free space; a full disk stops a broker from accepting writes.
Q15. Can I change broker settings without a restart?
Many settings, yes. Kafka supports dynamic broker configuration with kafka-configs.sh, for example thread counts, some log cleaner settings and listener security settings:
bin/kafka-configs.sh --bootstrap-server localhost:9092 \
--entity-type brokers --entity-name 1 --alter --add-config log.cleaner.threads=2
Other settings, such as node.id or log.dirs, require a restart. The documentation marks each setting’s update mode.
Q16. How do I upgrade Kafka brokers without downtime?
Use a rolling upgrade:
- Upgrade and restart one broker.
- Wait until it has caught up and there are no under-replicated partitions.
- Repeat for the next broker.
With replication factor 3 and min.insync.replicas=2, producers and consumers keep working throughout. Always read the upgrade notes for your target version first.
Q17. Is it a good idea to run Kafka brokers in Kubernetes?
Yes, many teams do, usually with an operator such as Strimzi that handles broker configuration, storage, rolling upgrades and certificates.
Brokers are stateful, so use persistent volumes, stable network identities (StatefulSets) and correct advertised listeners for clients inside and outside the cluster. A managed cloud Kafka service is another option that removes most of this work.
Q18. What is the difference between a Kafka broker and a RabbitMQ broker?
| Kafka broker | RabbitMQ broker | |
|---|---|---|
| Storage model | Append-only log, kept for a retention period | Queues; messages usually removed once acknowledged |
| Reading | Consumers pull and track offsets; replay possible | Broker pushes to consumers; no replay by default |
| Strength | High-volume event streaming | Flexible routing and task queues |
Q19. Which metrics should I watch on a broker?
- Under-replicated partitions and offline partitions: should be 0.
- Request handler and network idle percentages: low values mean the broker is overloaded.
- Produce and fetch latency.
- Bytes in and out per second.
- Disk usage of every log directory.
Q20. How can I quickly check that my brokers are healthy?
A few commands give a fast overview:
# Are all brokers reachable?
bin/kafka-broker-api-versions.sh --bootstrap-server localhost:9092
# Any partitions missing replicas?
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --under-replicated-partitions
# Is the KRaft controller quorum healthy?
bin/kafka-metadata-quorum.sh --bootstrap-server localhost:9092 describe --status
If every broker answers and no partitions are under-replicated, the cluster is in good shape.