'Perl Kafka is not sending messages

On one terminal I am running the following command to consume messages

/bin/kafka-console-consumer.sh --topic tracked-coords --from-beginning --bootstrap-server localhost:9092

And I have the following producer

#!/usr/bin/perl
  
use Net::Kafka::Producer;

use Number::Format 'format_number';

my $producer    = Net::Kafka::Producer->new(
    'bootstrap.servers' => 'localhost:9092'
);

for (my $index = 1;;$index++) {
  my $msg = "message: " . format_number($index);
  print "$msg\n" if (($index % 10000) == 0);
  $producer->produce(
    payload => $msg,
    topic   => "tracked-coords"
  )->then(sub {
    my $delivery_report = shift;
    print "Message is sent with offset " . $delivery_report->{offset};
  })->catch(sub {
    my $error = shift;
    print $error->{error} . "\n";
    die "Cannot send any more";
  });
}

Why does the consumer (a script that comes with Kafka) final messages are

...
message: 99,995
message: 99,996
message: 99,997
message: 99,998
message: 99,999
message: 100,000

My hypothesis is that the producer is running asynchronous and therefore is buffering them. How could I stop this behaviour?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source