Kafka Producer
Reconnection
Technical Guide
Kafka Configuration
Problem Solving

How to reconnect kafka producer once closed?

System Design practice on Codemia

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

Practice system design

Introduction

A Kafka producer can recover from many transient network problems while it is still open, but a producer that has been explicitly closed is finished. There is no supported way to reopen the same KafkaProducer instance, so the practical answer is to create a new producer object from the same configuration.

Understand What close() Means

Calling close() shuts down the producer's I/O threads, releases buffers, and ends the lifecycle of that object. After that point, the instance should be treated as unusable.

That distinction matters because people often mix up two cases:

  • the producer is still open and Kafka retries internally
  • the producer has been closed and must be replaced

If the second case happened, reconnection really means instantiating a fresh producer.

Create A New Producer Instance

Keep the producer configuration in one place so you can rebuild the client cleanly.

java
1import java.util.Properties;
2import org.apache.kafka.clients.producer.KafkaProducer;
3import org.apache.kafka.clients.producer.Producer;
4import org.apache.kafka.clients.producer.ProducerConfig;
5import org.apache.kafka.common.serialization.StringSerializer;
6
7public class ProducerFactoryExample {
8    public static Producer<String, String> newProducer() {
9        Properties props = new Properties();
10        props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
11        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
12        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
13        props.put(ProducerConfig.ACKS_CONFIG, "all");
14        return new KafkaProducer<>(props);
15    }
16}

By isolating creation in one method, the rest of your code can swap in a replacement producer without rebuilding configuration logic everywhere.

Simple Reconnect Pattern

A lightweight wrapper can recreate the producer after a close or a fatal failure.

java
1import org.apache.kafka.clients.producer.KafkaProducer;
2import org.apache.kafka.clients.producer.ProducerRecord;
3
4public class ProducerHolder {
5    private KafkaProducer<String, String> producer;
6
7    public ProducerHolder() {
8        this.producer = (KafkaProducer<String, String>) ProducerFactoryExample.newProducer();
9    }
10
11    public synchronized void send(String topic, String key, String value) {
12        producer.send(new ProducerRecord<>(topic, key, value));
13    }
14
15    public synchronized void recreate() {
16        producer.close();
17        producer = (KafkaProducer<String, String>) ProducerFactoryExample.newProducer();
18    }
19}

In real code, you would recreate only when necessary and add better exception handling, logging, and shutdown behavior. The important point is the lifecycle: replace the producer rather than trying to revive it.

Let Kafka Retry While The Producer Is Still Open

Do not recreate the client for every transient error. Kafka producers already support retries, request timeouts, batching, and metadata refresh while the instance is alive.

That means your first step should be distinguishing between temporary send failures and a genuinely closed producer. If the producer is still open, constant recreation usually makes throughput and stability worse.

Design For Safe Replacement

If several threads share one producer, replacement needs coordination. A common pattern is to keep the producer behind one service object and let only that service own creation, sending, and shutdown.

That avoids code paths where one thread closes the producer while another thread is trying to send. It also gives you one place to flush pending records before planned shutdown.

Common Pitfalls

The most common mistake is assuming close() behaves like a disconnect that can later be reversed. It does not. A closed producer instance is done.

Another mistake is creating a brand-new producer for every message. Producers are designed to be reused, and recreating them constantly throws away batching and connection reuse.

A third issue is closing the producer too aggressively during application shutdown or error handling without considering in-flight records. If you care about delivery, flush and close in an orderly way.

Summary

  • A closed Kafka producer cannot be reopened.
  • Reconnection means creating a new KafkaProducer instance from the saved configuration.
  • Do not confuse producer closure with temporary broker or network problems.
  • Reuse open producers instead of recreating one per message.
  • Centralize lifecycle management so replacement and shutdown stay safe.

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.