Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kafka producers are very slow

I am new in Kafka and I have a question that I'm not able to resolve.

I have installed Kafka and Zookeeper in my own computer in Windows (not in Linux) and I have created a broker with a topic with several partitions (playing between 6 and 12 partitions).

When I create consumers, they works perfectly and read at good speed, but referring producer, I have created the simple producer one can see in many web sites. The producer is inside a loop and is sending many short messages (about 2000 very short messages).

I can see that consumers read the 2000 messages very quicly, but producer sends message to the broker at more or less 140 or 150 messages per second. As I said before, I'm working in my own laptop (only 1 disk), but when I read about millions of messages per second, I think there is something I forgot because I'm light-years far from that.

If I use more producers, the result is worse.

Is a question of more brokers in the same node or something like that? This problem have been imposed to me in my job and I have not the possibility of a better computer.

The code for creating the producer is

public class Producer {

    public void publica(String topic, String strKey, String strValue) {
        Properties configProperties = new Properties();
        configProperties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        configProperties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());
        configProperties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(configProperties);
        ProducerRecord<String, String> rec = new ProducerRecord<String, String>(topic, strValue);
        producer.send(rec);
    }
}

and the code for sending messages is (partial):

Producer prod = new Producer();

for (int i = 0; i < 2000; i++)
{
    key = String.valueOf(i);
    prod.publica("TopicName", key, texto + " - " + key);
    // System.out.println(i + " - " + System.currentTimeMillis());
}
like image 641
Enrique López Moreno Avatar asked Oct 06 '17 12:10

Enrique López Moreno


People also ask

Why is Kafka slow?

Kafka ConsumersIf there are way too many producers writing data to the same topic when there are a limited number of consumers then then the reading processes will always be slow.

How do I reduce Kafka lag?

How about increase the partitions of topic and also increase consumers up to partitions. Consuming concurrency can increase performance. If you store offsets on the zookeeper, it can be bottleneck. Reduce commits of offset and use dedicated zookeeper if possible.

How many producers can Kafka support?

One producer for all topics and partitions Probably the optimal choice for most applications. The Kafka producer batches any written message before sending them to the Kafka cluster for up to batch.


1 Answers

You may create your Kafka producer once and use it every time you need to send a message:

public class Producer {
    private final KafkaProducer<String, String> producer; // initialize in constructor

    public void publica(String topic, String strKey, String strValue) {
        ProducerRecord<String, String> rec = new ProducerRecord<String, String>(topic, strValue);
        producer.send(rec);
    }
}

Also take a look at the producer and broker configurations available here. There are several options with which you can tune for your application's needs.

like image 153
alirabiee Avatar answered Oct 26 '22 02:10

alirabiee