Apache Kafka
TimeoutException
Topic Metadata
Error Handling
Programming Troubleshooting

org.apache.kafka.common.errors.TimeoutException Topic not present in metadata after 60000 ms

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed event streaming platform capable of handling trillions of events a day. One common issue users might encounter when working with Kafka is the org.apache.kafka.common.errors.TimeoutException. This exception can occur when a topic is not present in Kafka's metadata within the default timeout period, which is typically 60000 milliseconds (or 60 seconds). This article delves into the potential reasons for this error, how to troubleshoot it, and best practices to avoid it in the future.

Understanding the TimeoutException

The TimeoutException typically occurs when a Kafka client tries to produce, consume, or fetch metadata for a topic that does not exist or is not recognized by the cluster. The error message "Topic not present in metadata after 60000 ms" clearly points out that the topic in question is either not created or not propagated properly across the cluster. Here's a brief breakdown of the error components:

  • Topic not present in metadata: Indicates the absence of the required topic in the metadata known to the Kafka broker that the client connects to.
  • After 60000 ms: Indicates that the broker or the cluster failed to recognize the topic within the 60-second window.

Common Causes and Troubleshooting

Below are some of the common causes for this error and troubleshooting steps you can take:

  1. Topic does not exist:
    • Cause: The specified topic has not been created.
    • Resolution: Ensure that the topic is created before it is accessed. In Kafka, you can create topics manually using the Kafka CLI or ensure that automatic topic creation is enabled.
  2. Network Issues:
    • Cause: Broken or slow network connections between the Kafka client and the brokers.
    • Resolution: Check network connectivity, firewall rules, and broker settings.
  3. Broker Configuration:
    • Cause: Misconfiguration in the broker may prevent it from properly reading or writing metadata.
    • Resolution: Verify the broker's configuration files (e.g., server.properties) for any incorrect settings.
  4. Cluster Metadata Synchronization:
    • Cause: The metadata has not been synchronized across all brokers.
    • Resolution: Investigate the broker logs to determine if there are any synchronization issues.
  5. Client Timeout Settings:
    • Cause: The default metadata fetch timeout may be too low for some scenarios, especially in high-latency networks.
    • Resolution: Adjust the client’s metadata fetch timeout settings by increasing the metadata.max.age.ms.

Example

Consider a scenario where a Kafka producer is attempting to send records to a topic named myTopic. If myTopic does not exist or is not recognized within the default metadata waiting period, the producer will throw a TimeoutException.

java
1Properties props = new Properties();
2props.put("bootstrap.servers", "localhost:9092");
3props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
4props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
5
6Producer<String, String> producer = new KafkaProducer<>(props);
7try {
8    producer.send(new ProducerRecord<>("myTopic", "key", "value")).get();
9} catch (ExecutionException e) {
10    if (e.getCause() instanceof TimeoutException) {
11        System.out.println("Topic not present in metadata after 60000 ms");
12    }
13} catch (InterruptedException e) {
14    Thread.currentThread().interrupt();
15}

Best Practices

  • Regularly monitor and maintain Kafka clusters.
  • Ensure proper network connectivity and configurations.
  • Use replication and synchronization mechanisms efficiently.

Summary Table

Issue ComponentCauseResolution
Topic does not existTopic not created or misspelledPre-create topic or enable auto-creation
Network issuesPoor connectivity or firewall rulesCheck and rectify connectivity settings
Broker configurationMisconfiguration in broker settingsVerify and correct broker's server.properties
Cluster synchronizationMetadata sync delay among brokersMonitor logs and ensure synchronization is active
Client timeout settingsLow default timeout settingAdjust metadata.max.age.ms in client config

By understanding and addressing each component of this issue, users can minimize downtime and ensure a smoother operation of their Kafka-powered applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.