A partition is a file, and a file sits on a machine
Creating a topic with 3 partitions and 3 replicas of each: kafka-topics.sh --create --topic orders --partitions 3 --replication-factor 3 --bootstrap-server broker1:9092. The flag counts every copy, including the one producers will write to, so 3 means 3 brokers each store the full log of each partition.
Where the copies land
| Partition | Broker 1 | Broker 2 | Broker 3 | Broker 4 |
|---|---|---|---|---|
| orders-0 | replica | replica | replica | |
| orders-1 | replica | replica | replica | |
| orders-2 | replica | replica | replica | |
| orders-3 | replica | replica | replica |
kafka-topics.sh --describe --topic orders lists, for each partition, the ids of the brokers holding a replica, in assignment order, under the heading Replicas. That column is the first thing to read when a broker goes down: every partition whose Replicas list names the dead broker has lost 1 copy, and every partition whose list does not is untouched.
- ▸A full copy of 1 partition's log, with the same records at the same offsets as every other copy.
- ▸Stored on 1 broker, and no 2 replicas of the same partition share a broker.
- ▸Counted by the replication factor: 3 means 3 brokers each hold the whole log.
- ▸Kept current by copying from 1 of the others, which the next section shows.
The placement rule sets 2 limits. The replication factor cannot exceed the number of brokers, because the extra copy would have nowhere to go; asking for 4 replicas on a 3-broker cluster fails at creation with InvalidReplicationFactorException and no topic is made. And the factor is per topic, so 1 cluster can keep 3 copies of orders and 1 copy of a scratch topic nobody would miss. Not every copy can take writes from producers, or 3 copies would hand out 3 different offsets for the same record; the next section shows what the other copies do instead.
One copy takes the writes
Records enter orders-0 at broker 1. Brokers 2 and 3 fetch them from broker 1, and the consumer reads them from broker 1.
The fetch loop
| Tick | Leader log end | Follower asks from | Leader returns | Follower log end after |
|---|---|---|---|---|
| 1 | 5 | offset 3 | offsets 3 and 4 | 5 |
| 2 | 8 | offset 5 | offsets 5, 6 and 7 | 8 |
| 3 | 8 | offset 8 | nothing yet | 8 |
| 4 | 9 | offset 8 | offset 8 | 9 |
Take 1 record through the loop. A producer writes order-9001 to orders-0 at 10:00:00.000. Broker 1, the leader, appends it at offset 1204. Broker 2's fetch loop asks for offset 1204 a few milliseconds later and gets it; broker 3's loop does the same. By 10:00:00.020 all 3 replicas hold order-9001 at offset 1204, and the followers are asking for 1205, which does not exist yet, so the leader holds their requests open until a producer sends the next record.
Every broker leads something
| Partition | Leader | Replicas | Isr |
|---|---|---|---|
| orders-0 | 1 | 1,2,3 | 1,2,3 |
| orders-1 | 2 | 2,3,4 | 2,3,4 |
| orders-2 | 3 | 3,4,1 | 3,4,1 |
| orders-3 | 4 | 4,1,2 | 4,1,2 |
This is the per-partition part of what kafka-topics.sh --describe prints. The Isr column is the subject of the next section; for now read Leader against Replicas and note that each leader is the first id in its own list. Preferred matters later: when a broker returns from a failure, the cluster moves leadership back to the preferred replica so the spread stays even, and section 4 shows that return.
Sending to the wrong broker
- Receives every record a producer sends for the partition
- Assigns each record its offset
- Answers fetch requests from followers and from consumers
- Exactly 1 per partition at any moment
- Fetches from the leader in a loop, like a consumer
- Appends at the offsets the leader assigned
- Accepts no writes from producers
- Ready to become leader, which section 4 shows
Caught up is measured in seconds
A follower that copies continuously is never more than a few milliseconds behind, but a follower on a sick disk, or behind a saturated network link, can fall far behind, and the cluster needs a crisp rule for which followers still count. The rule defines the in-sync replica set, written ISR. A follower is in the ISR while it has caught up to the leader's end of log at some point in the last replica.lag.time.max.ms milliseconds, 30000 by default. The leader of a partition is always in its own ISR.
The ISR lists the safe successors
A follower falls behind and comes back
| Time | Leader log end | Broker 3 log end | Broker 3 last reached the end | ISR |
|---|---|---|---|---|
| 09:00:00 | 500 | 500 | 09:00:00 | 1,2,3 |
| 09:00:10 | 900 | 620 | 09:00:04 | 1,2,3 |
| 09:00:35 | 1900 | 700 | 09:00:04 | 1,2 |
| 09:01:20 | 3700 | 3700 | 09:01:20 | 1,2,3 |
Running --describe at 09:00:35 prints Isr: 1,2 against Replicas: 1,2,3: 1 replica is assigned and not caught up. The lag rule covers a follower that is alive and stuck; a broker that stops altogether leaves every ISR sooner, about 9 seconds after it falls silent, through the session timeout section 4 introduces.
- ▸In: reached the leader's end within the last replica.lag.time.max.ms, 30 seconds by default.
- ▸Out: no full catch-up for longer than that on a live broker; a stopped broker leaves when its session expires.
- ▸Back in: the moment the follower's log reaches the leader's end again.
The number of copies is a choice per topic
Replication factor is the count of replicas a topic keeps for each partition, chosen when the topic is created with --replication-factor, or taken from the broker's default.replication.factor when the flag is left out. A factor of 1 keeps 1 copy, which means the partition has a leader and no followers; the loss of that broker takes the partition offline until the broker returns, with its records gone for good if the disk went with it. kafka-topics.sh --describe prints the factor on its first line, before the per-partition rows.
Failures each extra copy survives
| Replication factor | Brokers that can be down while the partition still serves | Disk for 100 GB of records |
|---|---|---|
| 1 | 0 | 100 GB |
| 2 | 1 | 200 GB |
| 3 | 2 | 300 GB |
| 4 | 3 | 400 GB |
Disk, network and broker time
Factor 3 for most production topics
Changing a topic's factor after creation means adding or removing a replica for every partition, which is a partition move: the new broker must copy the full log from the leader before it is a replica worth having, and for the 350 GB topic above that is 350 GB of fetch traffic to add 1 copy. Kafka 4.4 does it through kafka-reassign-partitions.sh, and a later lesson covers the tool. The practical point now is that the factor is a creation-time decision that costs a full copy of the data to revise, so it is worth getting right on the day the topic is created.
- Set default.replication.factor=3 on every broker so an accidental topic still has copies
- Choose the factor from how the topic's records would be recovered if lost
- Keep factor 3 or more for topics whose records exist nowhere else
- Create topics at factor 1 when their records matter
- Plan to raise the factor later as though it were a flag flip
- Size disk per topic without multiplying by the factor
Broker 2 and its 3 replicas
Broker 2 on the 4-broker cluster loses power at 14:02:10. It held 3 replicas: the leader of orders-1 and followers of orders-0 and orders-3. The controller, the part of the cluster that tracks which brokers are alive, stops receiving broker 2's heartbeats; after broker.session.timeout.ms, 9000 milliseconds by default, it declares broker 2 gone, removes it from the ISR of every partition it held, and for each partition it led picks a new leader from the remaining ISR. Then 2 different things happen, depending on the role broker 2 played in each partition.
Picking a new leader
| Time | Event | A producer writing to orders-1 sees |
|---|---|---|
| 14:02:10 | broker 2 loses power | its open send gets no reply |
| 14:02:19 | controller declares broker 2 gone, broker 3 leads orders-1 | still waiting on a silent connection |
| 14:02:40 | the producer's request timeout expires | metadata refresh names broker 3 |
| 14:02:40.1 | broker 4 already fetches from broker 3 | the resend succeeds at the next offset |
The pause clients see
The pause has 2 parts. The controller needs around 9 seconds, the session timeout, to elect a new leader, and a client uses that leader only after it gives up on broker 2. When the broker process crashes on a running machine, the operating system resets the connection, requests fail at once, and the client retries until the election lands: about 10 seconds with defaults. A machine that loses power sends nothing back, so the client waits out its request.timeout.ms, 30000 by default, then refreshes metadata and goes to broker 3: about 30 seconds. Consumers do the same with their fetches, resuming from the offset they had reached.
Broker 2 returns and takes orders-1 back
At 14:40 broker 2 is powered on. Its 3 replicas fetch what they missed, including 38 minutes of orders-1 records from broker 3, and re-enter their ISRs. Broker 2 is still first in the Replicas list of orders-1, and with auto.leader.rebalance.enable on by default, the controller checks every 300 seconds for partitions not led by their preferred leader, so leadership moves back to broker 2 within 5 minutes of catching up. Broker 3 is up for that move and answers clients with NOT_LEADER_OR_FOLLOWER at once, so they redirect after 1 metadata refresh.
- ▸Follower lost: the ISR shrinks by 1 and clients notice nothing.
- ▸Leader lost: after the session timeout a new leader is chosen from the ISR, and clients pause, refresh metadata and retry.
- ▸Offsets never change, so a consumer resumes from the offset it had reached.
- ▸Broker back: its replicas fetch to catch up, rejoin the ISR, and preferred leadership returns within the rebalance interval.
> A Kafka broker at LinkedIn carrying member activity events, such as page views, loses power at 14:02. For the partitions it followed, the ISR shrinks and nobody notices. For the partitions it led, the controller promotes an in-sync follower after the 9-second session timeout, while the producers of page-view events sit on silent connections until their 30-second request timeout expires, then refresh metadata and resend to the new leaders. Consumers resume from the offsets they had reached. Those partitions pause for about 30 seconds and catch up, and 40 minutes later the repaired broker rejoins, fetches what it missed, and takes its preferred partitions back.
Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.
kafka.apache.org
- Brokers Hold Replicas
- Replication
- Leaders and Followers
- Replicated Logs: Quorums, ISRs, and State Machines
- Replication Factor
- default.replication.factorIncreasing replication factor
- When a Broker Dies
- Availability and Durability GuaranteesGraceful shutdown
- Further
- Replica Management
Apache Kafka, Kafka, and the Kafka logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.
Every partition lives on several brokers, 1 of them takes the writes, and the rest stand ready to take over.
- Category
- Kafka
- Difficulty
- beginner
- Duration
- 20 minutes
- Challenges
- 8 hands-on challenges
Topics covered: Brokers Hold Replicas, Leaders and Followers, In-Sync Replicas, Replication Factor, When a Broker Dies
Lesson Sections
- Brokers Hold Replicas (concepts: Replication and leaders)
A partition is a file, and a file sits on a machine The log lesson left a partition as an ordered list of records, each at an offset, growing at 1 end. That list is stored on disk, and the disk belongs to a broker: 1 Kafka server process on 1 machine, with an integer id such as 1 or 2. A cluster is a set of brokers, usually 3 or more, each with its own disk and its own network link. Every partition of every topic is stored on at least 1 broker, and the question this lesson answers is what happen
- Leaders and Followers (concepts: Replication and leaders)
One copy takes the writes Among a partition's replicas, 1 is the leader and the rest are followers. A producer sends every record for that partition to the leader, and the leader appends it to its log and assigns the offset. Followers write nothing on their own; each asks the leader for the records it does not yet have and appends them to its own log, at the offsets the leader assigned. By default a consumer reads from the leader too, so the leader is the single point of contact for a partition,
- In-Sync Replicas (concepts: In-sync replicas)
Caught up is measured in seconds Read the rule carefully, because it is a rule about time. A follower that is 2,000 records behind at this instant is in sync if it reached the leader's end 10 seconds ago, which happens during a burst: the leader took a flood of records, and the follower is fetching through it. A follower that is 1 record behind is out of sync if it has reached the leader's end at no point in the last 30 seconds, which happens when the follower is stuck. The leader keeps a clock
- Replication Factor (concepts: Replication and leaders)
The number of copies is a choice per topic Failures each extra copy survives The middle column is the reason to replicate. With factor 3, a partition keeps serving reads and writes through the loss of any 2 of its 3 brokers, because 1 in-sync copy remains to lead. The jump from 1 to 2 buys the most: it turns a broker failure from an outage into a change of leader. The jump from 2 to 3 buys the ability to take 1 broker down on purpose, for an upgrade or a disk swap, while still holding a spare co
- When a Broker Dies (concepts: Replication and leaders)
Broker 2 and its 3 replicas For orders-0 and orders-3, where broker 2 was a follower, the ISR shrinks from 3 ids to 2 and nothing else changes: the leaders keep appending, the other follower keeps fetching, and producers and consumers of those partitions see nothing at all. For orders-1, where broker 2 led, every producer and consumer has lost its point of contact, and the partition is without a leader until the controller names one. Picking a new leader The ISR of orders-1 was brokers 2, 3 and