How can I produce messages with Kafka 8.2 API in Java?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
If by "Kafka 8.2 API" you mean the Java producer API introduced in Kafka 0.8.2, the core workflow is still familiar today: create a KafkaProducer, configure serializers and brokers, build ProducerRecord instances, then send them asynchronously or synchronously.
What matters most is choosing the right producer settings for reliability. A producer that merely sends bytes is easy to write; a producer that behaves well under retries, broker failures, and batching needs a little more care.
Create a Minimal Producer
A producer needs bootstrap brokers plus key and value serializers.
That is enough to publish a message to the events topic, assuming the broker is reachable and the topic exists or auto-creation is enabled.
Use the Modern Producer Pattern Correctly
Even in older Kafka client generations, the new producer API was designed around asynchronous send. send() queues the record and returns immediately with a Future.
If you want to know whether the broker acknowledged the write, add a callback:
That is usually better than assuming the send succeeded just because no exception was thrown immediately.
Make Reliability Settings Explicit
For real applications, set the producer properties intentionally instead of relying on defaults you have not reviewed.
Why these matter:
- '
acks=allasks the broker side for stronger durability semantics' - '
retrieshelps with transient failures' - '
linger.msallows small batching windows for better throughput' - '
batch.sizecontrols how much data can accumulate per partition batch'
If you need strict ordering guarantees, review how retries interact with in-flight requests in the client version you are using.
Sending Synchronously When You Must
Kafka producers are optimized for async use, but you can block for the broker result by calling get() on the returned Future.
This is useful for tests, scripts, or workflows where the next step depends on confirmed delivery. It reduces throughput, so do not use it blindly in hot paths.
Topic, Serialization, and Keys
Keys are optional, but they matter. Kafka uses the key to choose a partition when a custom partitioner is not involved. If you want all events for one user to stay in order, give them the same key.
Both records are likely to land in the same partition, which preserves relative ordering for that key.
Serialization matters too. Strings are a fine starting point, but production systems often move to JSON, Avro, or Protobuf once the payload structure becomes important.
Common Pitfalls
The biggest mistake is confusing the version number in the question. Kafka 0.8.2 introduced the newer Java producer API, but many modern examples target later client versions. The core pattern is similar, but configuration defaults may differ.
Another common problem is forgetting flush() or close() in short-lived programs. If the process exits immediately after send(), buffered records may never be transmitted.
People also ignore callbacks and assume a send always worked. Kafka may reject or retry a write after the send() call has already returned.
Finally, avoid producing without thinking about keys, acknowledgments, and serializers. Those choices determine ordering, durability, and interoperability.
Summary
- Use
KafkaProducerwith explicit broker and serializer settings. - Build
ProducerRecordobjects and send them asynchronously by default. - Add callbacks or
Future.get()when you need delivery feedback. - Set reliability-related properties such as
acksandretriesdeliberately. - Use keys when ordering by entity matters.
- Always
flush()andclose()the producer in short-lived programs.
Related reading
- How can I queue a task to Celery from C#?
- How can I register a protobuf schema with references in other packages in Kafka schema registry?
- How can I retry failure messages from kafka?
- How can I run NodeJS in Docker with MongoDB and RabbitMQ?
- How can I receive and send over UDP asynchronously with F?
- How can I retrieve a user's public IP address via Amazon API Gateway Lambda node
- How can I propagate exceptions between threads?
- How can I properly compare two Integers in Java?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.