Scrivio Interview library
InternalsIntermediate7 min read · updated 13 November 2026

A broker in a KRaft cluster that leads 400 partitions is killed with kill -9 and does not come back.

Walk through what happens step by step from the controller's point of view and from a producer's point of view, and compare it with a controlled shutdown of the same broker.

After kill -9 the broker simply stops heartbeating. Once broker.session.timeout.ms passes (9 seconds by default), the active controller fences it, writes metadata records that elect new leaders from the ISR for all 400 partitions and removes it from every ISR, and the other brokers apply those records and take over. Producers see disconnects and NOT_LEADER_OR_FOLLOWER errors or timeouts, refresh metadata and retry, so with idempotence nothing is lost or duplicated, but latency spikes for the whole fencing window. A controlled shutdown moves leadership away before the broker stops, so clients see only a brief leader move.

Why this question is asked

Brokers die in production for many reasons: an out-of-memory kill, a failed host, an operator mistake. Knowing what happens next, and how long it takes, is what lets us set producer timeouts sensibly, explain a latency spike in a postmortem, and choose between a hard restart and a graceful one. Interviewers ask this to see whether we can follow a failure across the controller, the other brokers and the clients. In this answer, we will recap the terms involved, follow the hard kill from the controller's side, then from a producer's side, compare it with a controlled shutdown, and finish with what happens when the broker comes back.

1A Quick Recap of Leaders, ISRs and Fencing

Every partition has one leader replica that takes all writes and, by default, all reads, and some follower replicas that copy from it. The in-sync replica set, or ISR, is the list of replicas that are fully caught up. Only an ISR member can be elected leader under normal settings, because only those replicas are known to hold every committed record.

In KRaft, each broker keeps a session with the active controller by sending heartbeats (every broker.heartbeat.interval.ms, 2 seconds by default). Fencing is what the controller does to a broker whose session has expired: it marks the broker as fenced in the metadata, which means it may not lead any partition, and it immediately starts moving leadership off it.

Every leader change also increments the partition's leader epoch, a counter that says which leadership term a replica or client is talking about. It is how Kafka tells an old leader's data and requests apart from the new leader's.

2The Controller's View After kill -9

kill -9 gives the broker no chance to say goodbye. Its sockets close, but the controller does not treat a closed socket as death; it waits for the heartbeat session to expire. So for up to broker.session.timeout.ms, 9 seconds by default in current versions (check your version), the cluster still believes the broker is alive and its 400 partitions still have it as leader.

When the session expires, the active controller fences the broker. For each of the 400 partitions it led, it picks a new leader from the remaining ISR members, preferring the next replica in the assignment list, and it writes a partition change record with the new leader, the shrunken ISR and an incremented leader epoch. For partitions where the dead broker was only a follower, it writes records that remove it from the ISR. All of these records go into __cluster_metadata and are committed by the controller quorum.

The other brokers fetch the new records, apply them to their metadata image and react. A broker that is now leader of orders-3 starts accepting produce requests for it; the remaining follower starts fetching from the new leader. Notice that no data moves during this step. The new leader already has every committed record because it was in the ISR.

Step throughA hard kill seen from the controller
Broker 3Active controllerBroker 5Producer
heartbeats stop (process killed)
produce request fails: connection refused
session timeout passes, fence broker 3
commit: new leaders, ISRs shrunk, epoch +1
fetch metadata log
partition records: you lead orders-3
metadata refresh, then retry to new leader
produce acknowledged

8 messages in order. Play them, or step through them one at a time.

The fencing window, up to the session timeout, is where most of the extra latency comes from.

3The Producer's View

A producer sending to one of the 400 partitions first sees its connection to broker 3 fail. It marks the partition's leader as unknown and refreshes metadata. Until the controller fences the broker, the refreshed metadata still points at broker 3, so the producer keeps failing or timing out and keeps retrying, with backoff. Once the new leader is in the metadata image, the producer either gets NOT_LEADER_OR_FOLLOWER from a broker that is no longer leader, or discovers the new leader on its next refresh, and its retries succeed.

Whether this is invisible depends on settings. Retries continue until delivery.timeout.ms (2 minutes by default) runs out, which is far longer than the fencing window, so a default producer does not lose records. With enable.idempotence=true (the default since Kafka 3.0), the broker uses the producer id and sequence numbers to drop any retry that it had already written, so we get no duplicates either. What the application does see is latency: records sent during those seconds wait in the producer's buffer, and with max.in.flight and a full buffer, send() can even block for max.block.ms.

Java
Properties p = new Properties();
p.put("acks", "all"); // wait for the ISR
p.put("enable.idempotence", "true"); // default since 3.0: no duplicates on retry
p.put("delivery.timeout.ms", "120000"); // retries keep going for up to 2 minutes
p.put("request.timeout.ms", "30000");
// during the fencing window: send() succeeds later, latency jumps by roughly 9 to 15 seconds

4How a Controlled Shutdown Differs

When we stop the broker with a normal SIGTERM, it performs a controlled shutdown. Before closing anything, it tells the controller that it wants to shut down, through its heartbeat. The controller then moves leadership of each of its partitions to another ISR member and removes it from the ISRs, writes those records, and only when that is done tells the broker it may stop.

So clients never wait for a session timeout. They see a NOT_LEADER_OR_FOLLOWER error on their next request, refresh metadata and move to the new leader, usually within a second or two, and in-flight writes are not left hanging on a dead connection. A controlled shutdown also flushes and closes the logs cleanly, which matters a lot for the restart, as we will see next.

Hard kill versus controlled shutdown
Aspectkill -9Controlled shutdown
Who noticescontroller, after heartbeats time outbroker asks the controller first
Leader moveafter up to 9 s of fencing waitbefore the broker stops
Client impacttimeouts, retries, seconds of latencyone NOT_LEADER error, fast retry
Data loss with acks=allnonenone
Restartlog recovery of unflushed segmentsclean start, no recovery

5When the Broker Comes Back

On restart the broker registers again, and the controller unfences it once it has caught up with the metadata log. It starts as a follower for all its partitions. Each replica checks where its log diverges from the current leader using leader epochs and truncates anything the new leader does not have, then fetches the missing records. When a replica catches up to the leader's log end offset it is added back to the ISR.

Leadership does not move back immediately. With auto.leader.rebalance.enable=true, which is the default, the controller periodically runs a preferred leader election and returns leadership to the first replica in each partition's assignment list, which was broker 3 for many of its 400 partitions. Until that happens, the other brokers carry extra leaders and extra load.

6Follow-up questions

Why can a hard kill also mean a long log recovery on restart?

Kafka does not fsync each write; it relies on replication and lets the operating system flush pages in the background. After kill -9 the broker has no clean shutdown marker, so on restart it cannot trust data after the last recovery point it checkpointed. It re-reads those segments, checks every batch's CRC, truncates any torn write at the end and rebuilds the indexes. With many partitions and the default small number of recovery threads per data directory, that can take many minutes, during which the broker serves nothing. A controlled shutdown writes the marker and skips all of this.

Can we shorten the fencing window?

We can lower broker.session.timeout.ms, but each reduction trades faster failover for more false positives: a long garbage collection pause or a brief network glitch would get a healthy broker fenced and cause unnecessary leader churn. Most teams keep the default and instead make sure producers have enough delivery timeout to ride out a failover.

7Conclusion

In this answer, we saw that a hard kill is detected only when the heartbeat session expires, after which the active controller fences the broker, elects new leaders from the ISR with a new leader epoch, and removes it from every ISR. Producers suffer a burst of retries and latency but, with acks=all, idempotence and a generous delivery timeout, no loss or duplication. A controlled shutdown moves leadership first, so clients barely notice and the restart needs no log recovery, and in both cases preferred leader election later moves leadership back.

What to say in the interviewAbout 90 seconds aloud

Open with

With kill -9 the broker just disappears, and the controller only learns it is gone when its heartbeats stop for broker.session.timeout.ms, which is 9 seconds by default. Until then the metadata still says it leads those 400 partitions.

Then, if they want more

When the session expires, the active controller fences it and writes metadata records that pick a new leader from the ISR for each partition it led, bump the leader epoch, and remove it from every ISR.

The other brokers fetch those records from the metadata log and start acting as leaders or followers; no data has to move, because ISR members already have all committed records. From the producer side, I first see connection errors, then timeouts or NOT_LEADER_OR_FOLLOWER, and the client refreshes metadata and retries.

With acks=all, idempotence on and delivery.timeout.ms well above the fencing window, nothing is lost or duplicated, but latency jumps by several seconds.

A controlled shutdown is different because the broker asks the controller to move its leadership and leave the ISRs before it stops, so clients see only a quick leader change and there is no session timeout wait and no log recovery afterwards.

When the broker comes back after the crash, it recovers its logs, registers, truncates and catches up as a follower, rejoins the ISRs, and auto preferred leader election moves leadership back to it.

About this answer

Last updated
13 November 2026
Versions it names
Kafka 3.0
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Walk through what happens step by step from the controller's point of view and from a...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/eafeab0f39/