Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Kafka producer send() gives up waiting for topic metadata

Your error

org.apache.kafka.common.errors.TimeoutException: Topic my-topic not present in metadata after 60000 ms.1 more line it can show

Fix it fast

Most common cause, 1 of 3

The topic was never created and the broker does not auto-create it

Create the topic before the application sends to it.

Fix
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create \
  --topic my-topic --partitions 3 --replication-factor 1
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Topic not present in metadata after 60000 ms means KafkaProducer.send() waited the whole max.block.ms (60 seconds by default) without learning about the topic. Usually the topic does not exist and the broker will not auto-create it, or the producer cannot reach the address the broker advertises. Create the topic or fix advertised.listeners. Applies to Kafka 4.4 brokers and clients.

  • Applies to Apache Kafka 4.4.0 brokers, kafka-clients 4.4.0 (Java producer)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Run the topic tool on the host or inside the container where the producer runs, using the exact bootstrap address the producer uses. A clear answer that the topic is absent points to cause 1. A command that hangs points to cause 2. A partition count that is not higher than the partition number in the error points to cause 3.

Run this first
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic my-topic

Then find the line you see in this list and open its fix:

  1. 1The topic was never created and the broker does not auto-create itkafka-topics.sh --list answers quickly, but the topic name from your code is not in it
  2. 2The producer cannot reach the broker address in advertised.listenersThe producer log repeats a WARN line saying Connection to node ... could not be established. Node may not be available, and the topic tool hangs too
  3. 3The record asks for a partition number the topic does not haveThe message names a partition and a partition count: Partition 3 of topic my-topic with partition count 3

What does the error look like?

  • org.apache.kafka.common.errors.TimeoutException: Topic my-topic not present in metadata after 60000 ms.
  • org.apache.kafka.common.errors.TimeoutException: Partition 3 of topic my-topic with partition count 3 is not present in metadata after 60000 ms.

Cause 1 of 3

The topic was never created and the broker does not auto-create it

You seekafka-topics.sh --list answers quickly, but the topic name from your code is not in it

Check

The producer reports Topic my-topic not present in metadata when the cluster has no topic by that name and refuses to make one. Kafka 4.4 ships with auto.create.topics.enable set to true, so a broker that sets it to false only serves topics that were created in advance. Compare the name in your code, letter by letter, with the list.

Check
bin/kafka-topics.sh --bootstrap-server localhost:9092 --list

Fix

Create the topic before the application sends to it. Keep the replication factor at or below the number of brokers you run.

Fix
bin/kafka-topics.sh --bootstrap-server localhost:9092 --create \
  --topic my-topic --partitions 3 --replication-factor 1

Verify

The describe command now prints a PartitionCount and a Leader for every partition, and the next send() returns a record offset instead of timing out.

Verify
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic my-topic

Confirmed by kafka.apache.org

Did this fix it?

Cause 2 of 3

The producer cannot reach the broker address in advertised.listeners

You seeThe producer log repeats a WARN line saying Connection to node ... could not be established. Node may not be available, and the topic tool hangs too

Check

A topic that really exists still produces the same timeout when no metadata reply ever arrives. After the first contact, clients use the addresses the brokers advertise, and advertised.listeners falls back to listeners when it is not set. If that address is localhost, a private IP or a container name the producer cannot resolve, every metadata request fails until max.block.ms runs out.

Check
grep -E "^(listeners|advertised.listeners)" config/server.properties
nc -vz broker1.example.com 9092

Fix

Advertise an address the producer can resolve and reach, then restart the broker. Kafka does not accept 0.0.0.0 as an advertised address. When clients sit on different networks, such as Docker containers and the host, give each network its own listener.

Fix
# config/server.properties on broker 1
advertised.listeners=PLAINTEXT://broker1.example.com:9092

Verify

The connection warnings stop, the describe command returns at once, and a test send succeeds.

Verify
bin/kafka-console-producer.sh --bootstrap-server broker1.example.com:9092 --topic my-topic

Confirmed by kafka.apache.org

Did this fix it?

Cause 3 of 3

The record asks for a partition number the topic does not have

You seeThe message names a partition and a partition count: Partition 3 of topic my-topic with partition count 3

Check

In the Kafka 4.4.0 producer source, waitOnMetadata keeps asking for fresh metadata until the requested partition number is lower than the topic's partition count. Partition numbers start at 0, so a topic with 3 partitions has 0, 1 and 2. A ProducerRecord built with partition 3 waits out max.block.ms and fails with this longer message.

Check
grep -rn "new ProducerRecord" src/

Fix

Leave the partition argument out so the partitioner chooses one, or compute it from partitionsFor(topic).size(). If the topic really needs more partitions, add them, keeping in mind that keyed records may map to different partitions afterwards.

Fix
bin/kafka-topics.sh --bootstrap-server localhost:9092 --alter \
  --topic my-topic --partitions 6

Verify

The describe output shows the new PartitionCount and records sent to the higher partition numbers are acknowledged.

Verify
bin/kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic my-topic

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Create topics in a deploy script with kafka-topics.sh --create and keep every topic name in one config file that the code reads.
  • Call producer.partitionsFor(topic) during start-up; it blocks for the same max.block.ms, so a missing topic fails the health check instead of the first real send.
  • Never hard-code partition numbers in ProducerRecord; derive them from the partition count the client reports.
  • Test the bootstrap address from inside the container or host that runs the producer, not from your own laptop.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.