FrankTan
FrankTan

Reputation: 1686

Kafka is slow to produce messages in first seconds

i m working with kafka, and i made a producer like that:

synchronized (obj) {

        while (true){

            long start = Instant.now().toEpochMilli();
            for (int i=0; i< NUM_MSG_SEC  ; i++)  
            {

                PriceStreamingData data = PriceStreamingData.newBuilder()
                        .setUser(getRequest().getUser())
                        .setSecurity(getRequest().getSecurity())
                        .setTimestamp(Instant.now().toEpochMilli())
                        .setPrice(new Random().nextDouble()*200)
                        .build();


                record = new ProducerRecord<>(topic, keyBuilder.build(data), 
                        data);



                producer.send(record,new Callback(){
                    @Override
                    public void onCompletion(RecordMetadata arg0, Exception arg1) {
                        counter.incrementAndGet();
                        if(arg1 != null){
                            arg1.printStackTrace();
                        }


                    } 
                });

            }
            long diffCiclo = Instant.now().toEpochMilli() - start;
            long diff = Instant.now().toEpochMilli() - startTime;


            System.out.println("Number of sent: " + counter.get() +  
                    " Millisecond:" + (diff) + " - NumberOfSent/Diff(K): " + counter.get()/diff );

            try {
                if(diffCiclo >= 1000){
                    System.out.println("over 1 second: "  + diffCiclo);

                }
                else {
                    obj.wait( 1000 - diffCiclo );

                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }




        }
    }

as you can see it is extremely simple, it just make a new message and send it. If i see the logs:

    NumberOfSent/Diff(K)   

in the first 10 seconds it perform very bad just

  30k per second

after 60 seconds i have

  180k  per second 

why ? and how can i already start the process going already to 180k ?

my kafka producer configuration is the Follwing

  Async producer ( but also with sync producer the situation dose not change)
  ACKS_CONFIG = 0
  BATCH_SIZE_CONFIG = 20000 
  COMPRESSION_TYPE_CONFIG = none
  LINGER_MS_CONFIG = 0

last detail:

  NUM_MSG_SEC is set to 200000 or bigger number 

Upvotes: 1

Views: 2565

Answers (1)

FrankTan
FrankTan

Reputation: 1686

I found the solution by myself and I hope this post can be useful for other people too.

The problem stand in the

 ProducerConfig.BATCH_SIZE_CONFIG 

and

ProducerConfig.LINGER_MS_CONFIG

My parameters were 20000 and 0, in order to fix the issue I did set them them to higher values 200000 and 1000. Finally I started the JVM with the parameters:

-XX:MinMetaspaceFreeRatio=100
-XX:MaxMetaspaceFreeRatio=100

because I saw it takes longer to set the metaspace to a decent value.

Now the producer start directly at 140k and in 1 second already is to 180k.

Upvotes: 3

Related Questions