The Kafka Producer API

Different ways to use the Kafka Producer API

Conceptually, sending messages to Kafka using the Producer API is quite simple. At a high-level, it involves the following steps:

  1. Use configuration (the java.util.Properties object) to specify Kafka broker and other parameters.

  2. Create a KafkaProducer instance (using the configuration object).

  3. Use the send method on the KafkaProducer instance.

We can use the Kafka Producer API in two different ways—synchronous and asynchronous. It’s important to note that the API methods themselves are not named in a specific way to distinguish between these modes. That makes it even more important to understand how they work, their differences, and when to use the appropriate one for our use case and performance requirements.

The synchronous producer

When using the synchronous mode, the client application waits for a response from Kafka. This approach is useful when the application requires immediate feedback on the success or failure of each message sent to Kafka.

Note that this may impact the application’s overall performance, because the producer has to wait for a response before continuing, which can cause delays.

Sample code

Go through the example below and click the “Run” button to execute the code.

Get hands-on with 1200+ tech skills courses.