Quick revision
The short answer
The producer connects to any broker in bootstrap.servers and sends a Metadata request; the response lists every broker with its advertised address and the leader of each partition. From then on it sends Produce requests directly to each partition's leader, grouped per broker, and refreshes metadata every five minutes by default or whenever an error suggests it is stale. When a leader moves, the old broker answers NOT_LEADER_OR_FOLLOWER or the connection fails, so the producer refreshes metadata and retries to the new leader.
What to remember
In this answer, we saw that bootstrap.servers is only used for the first Metadata request, which returns every broker's advertised address and each partition's leader. The producer then sends batches directly to leaders, grouped per broker, refreshes metadata periodically and on errors, and recovers from a leader move by refreshing and retrying, without duplicates when idempotence is on. Because clients connect to the advertised addresses, those must be reachable from the client's network.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The producer connects to any broker in bootstrap.servers and sends a Metadata request; the response lists every broker with its advertised address and the leader of each partition. From then on it sends Produce requests directly to each partition's leader, grouped per broker, and refreshes metadata every five minutes by default or whenever an error suggests it is stale. When a leader moves, the old broker answers NOT_LEADER_OR_FOLLOWER or the connection fails, so the producer refreshes metadata and retries to the new leader.
Why this question is asked
Many connection problems with Kafka, such as clients that connect and then time out, or a cluster that works from one network but not another, come from misunderstanding how clients discover brokers. Interviewers ask this question to see whether we know that the bootstrap list is only a starting point and how clients follow leaders as they move. In this answer, we will recap partitions and leaders, follow a producer from its first connection to its first send, see how metadata stays fresh, walk through a leader change, and finish with why advertised listeners matter.
1A Quick Recap of Partitions and Leaders
A topic is split into partitions, and each partition has one leader replica on some broker plus followers on others. Producers must send records for a partition to that partition's leader, because only the leader appends to the log. With twelve brokers, the leaders of a topic's partitions are spread across many of them, so a producer usually talks to most of the cluster.
bootstrap.servers in our producer lists three of the twelve brokers. That list is not a statement of where data goes. It only gives the client a few addresses where it can ask its first question.
2From Bootstrap to the First Produce Request
When the producer needs to send to a topic it has not seen, it opens a connection to one of the bootstrap brokers and sends a Metadata request for that topic. Any broker can answer, because every broker keeps a copy of the cluster metadata. The response lists all brokers with their IDs and advertised host and port, and for each partition, its leader, replicas and in-sync replicas.
6 messages in order. Play them, or step through them one at a time.
Now the producer knows where to send each partition's records. Its sender thread groups ready batches by destination broker, so one Produce request to broker 7 can carry batches for several partitions that broker 7 leads. The bootstrap brokers get no special traffic after this; broker 2 is used only if it happens to lead some partitions. Notice that the three bootstrap brokers do not even need to lead anything. They just need to be reachable.
3Keeping Metadata Fresh
Leaders move: a broker restarts, a controller rebalances leadership, or an operator reassigns partitions. So the client refreshes metadata in two ways. Periodically, every metadata.max.age.ms, five minutes by default, even if nothing seems wrong. And on demand, as soon as a response or a failed connection suggests the metadata is stale.
4What Happens When a Leader Moves
Suppose partition 0's leader moves from broker 7 to broker 4. The producer's next Produce request still goes to broker 7, which is no longer the leader, so it answers with the error NOT_LEADER_OR_FOLLOWER. If broker 7 is down instead, the connection fails or the request times out. Either way, the producer marks its metadata for the topic as stale, sends a new Metadata request to any reachable broker, learns that broker 4 now leads partition 0, and retries the batch there.
This is a retriable error, so the application usually sees nothing except a short delay. Retries continue until delivery.timeout.ms runs out. With idempotence enabled, the default in current producers, retries do not create duplicates and keep per-partition order. Recent versions also shorten this step: brokers can include the new leader's details in the error response (KIP-951), so the client can redirect without waiting for a full metadata refresh; check your version.
- Send batch for p0 to broker 7Cached metadata says 7 leads p0
- Error or connection failure?YesMark metadata staleNoDone: offsets returned
- Request fresh metadataFrom any reachable broker
- Learn new leader: broker 4Or use the leader hint in the error
- Retry the batch to broker 4Until
delivery.timeout.ms
5 steps in order. Play them, or step through them one at a time.
5Why Advertised Listeners Must Be Reachable
The addresses in the Metadata response are not the ones in our bootstrap list. They are each broker's advertised.listeners, the host and port the broker tells clients to use. If a broker advertises an internal name like kafka-7.internal:9092, a client outside that network can bootstrap successfully, receive metadata, and then fail to connect to the leaders. That is the classic 'connects, then times out' symptom. The fix is to give each network its own listener with addresses that network can reach, for example an INTERNAL and an EXTERNAL listener.
6Follow-up questions
Does the producer need all twelve brokers in bootstrap.servers?
No. It needs at least one reachable broker to get metadata. Listing two or three in different zones is enough to survive a broker being down at startup. Listing all of them adds no benefit and must be maintained as the cluster changes. A DNS name that resolves to several brokers works too.
What does the consumer do differently?
The same metadata process finds partition leaders for fetches. In addition, a consumer finds its group coordinator, the broker that manages its consumer group, with a FindCoordinator request, and it may be told to fetch from a follower if rack-aware follower fetching is enabled.
7Conclusion
In this answer, we saw that bootstrap.servers is only used for the first Metadata request, which returns every broker's advertised address and each partition's leader. The producer then sends batches directly to leaders, grouped per broker, refreshes metadata periodically and on errors, and recovers from a leader move by refreshing and retrying, without duplicates when idempotence is on. Because clients connect to the advertised addresses, those must be reachable from the client's network.
Open with
The bootstrap list is only a way in. When the producer first needs a topic, it connects to any of the three bootstrap brokers and sends a Metadata request.
Then, if they want more
Every broker can answer that, because every broker has the cluster metadata. The response gives all twelve brokers with their advertised addresses, and for each partition, the leader and replicas. From then on, the producer sends Produce requests straight to each partition's leader.
Its sender thread groups batches by broker, so one request to a broker carries batches for all the partitions it leads. The bootstrap brokers get no special role after that. Metadata is refreshed every metadata.max.age.ms, five minutes by default, and immediately when something suggests it is stale.
If a leader moves, the next request goes to the old broker, which answers NOT_LEADER_OR_FOLLOWER, or the connection fails if the broker is down. The producer marks the metadata stale, fetches fresh metadata, finds the new leader and retries the batch there.
That error is retriable, so the app usually just sees a small delay, and with idempotence on there are no duplicates. Newer brokers can even put the new leader in the error response so the client redirects faster.
One practical point: clients connect to the advertised listener addresses from the metadata, not the bootstrap addresses, so those must be reachable from the client's network, otherwise you get the classic connects-then-times-out problem.
About this answer
- Last updated
- 13 November 2026
- 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 "Explain how it learns where to send records for each partition, and what happens when a...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/47d5825ab1/