What are Kafka Producers and Consumers?

Kafka producers and consumers are the two client roles that write to and read from Kafka topics — producers publish records, and consumers subscribe to topics to process the data producers send.

Key Points

Example

A ride-sharing app's driver-location service acts as a producer, continuously publishing GPS updates to a "driver-locations" topic, while a separate matching service acts as a consumer, reading those updates in real time to pair drivers with nearby riders.

// Plain Java client (kafka-clients library)
Properties p = new Properties();
p.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
p.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
p.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);

try (KafkaProducer<String, String> producer = new KafkaProducer<>(p)) {
    producer.send(new ProducerRecord<>("driver-locations", "driver-7", "18.52,73.85"));
}

Properties c = new Properties();
c.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
c.put(ConsumerConfig.GROUP_ID_CONFIG, "matching-service");
c.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
c.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

try (KafkaConsumer<String, String> consumer = new KafkaConsumer<>(c)) {
    consumer.subscribe(List.of("driver-locations"));
    while (true) {
        for (ConsumerRecord<String, String> r : consumer.poll(Duration.ofMillis(500))) {
            System.out.println(r.key() + " is at " + r.value() + " (offset " + r.offset() + ")");
        }
    }
}

Interview Tip

A concise interview answer is

Producers are client applications that publish records to Kafka topics, and consumers are client applications that subscribe to those topics and process the records, usually as part of a consumer group so the work can be parallelized, and because Kafka persists data, producers and consumers don't need to be online at the same time.

Last updated: 1 November 2026

Previous question: How does Kafka handle ZooKeeper outages?

Next question: How do producers send data to Kafka?

More Kafka (Optional) interview questions

All Kafka (Optional) questions