Kafka Exception TopicExistsException
org.apache.kafka.common.errors.TopicExistsException
Non-retriable
Admin
TopicExistsException is a Kafka exception (org.apache.kafka.common.errors.TopicExistsException).
Common Causes
- Idempotent topic creation code not using --if-not-exists flag or equivalent AdminClient option, running on restart or re-deploy
- Topic auto-created by a consumer/producer before the admin script ran, resulting in creation with default configs instead of intended settings
- CI/CD pipeline retrying a failed topic-creation step that partially succeeded on a previous run
Solutions
- Use AdminClient.createTopics() with the option to ignore TOPIC_ALREADY_EXISTS error, or check existence with describeTopics() before creating
- Add --if-not-exists to kafka-topics.sh calls in scripts to make them idempotent
- If topic was auto-created with wrong config, delete and recreate with correct partition/replication settings, or alter the config post-creation
Example Stack Trace
org.apache.kafka.common.errors.TopicExistsException: Topic with this name already exists.Diagnostic Commands
# Describe topic partitions, leaders, and ISR
kafka-topics.sh --bootstrap-server localhost:9092 --describe --topic <topic-name>
# List topics and search for the target topic
kafka-topics.sh --bootstrap-server localhost:9092 --list | grep <topic-name>Related
Protocol error: TOPIC_ALREADY_EXISTS (code 36)
Related Admin exceptions: InvalidConfigurationException · InvalidPartitionsException · InvalidReplicationFactorException · InvalidTopicException · PolicyViolationException · ThrottlingQuotaExceededException · UnsupportedVersionException
Hitting
TopicExistsException in production? Conduktor Console gives you real-time visibility into clients, consumer groups, and broker health. Browse every Kafka exception or protocol error code.