Message queue in PHP using Kafka with an ack's
Production-ready flow to use Apache Kafka with PHP in Docker

Search for a command to run...
Production-ready flow to use Apache Kafka with PHP in Docker

No comments yet. Be the first to comment.
One persistent issue our team faced was managing duplicated events in Laravel's queue system. After numerous attempts to solve it, we finally arrived at a solution that helped us handle duplicated messages without losing critical data. The Problem: D...

We constantly attend job interviews where questions about SOLID principles are mandatory. After that, there are usually a couple of questions about design patterns. The candidate may believe that they have finally found a company where these principl...

Fixing error - HTTPS and ERR_TOO_MANY_REDIRECTS

’ve lost a lot of time struggling to find solution — how to share state between vue.js & Phaser. Currently, I’m interested in learning node.js and decided to do it by developing a browser multiplayer game. Although I have some experience in Vue.js, s...
On a new project that I am working on as PHP Tech Lead. The team was faced with the issue of establishing various kinds of decisions. Including the unification of technologies:
This article will talk about testing Apache Kafka as a message broker.
Kafka is currently already used in the company and this technology is more scalable than the same RabbitMQ, so we are looking in its direction. Before we start implementing Kafka in our projects, we will test whether this technology covers our needs.
Requirements for a message broker:
In fact, all this is already there and works great (at small and medium volumes) for RabbitMQ. But as I wrote above, we do not want to breed a zoo of technologies.
The Kafka official website has several links to the integration repositories:
Only one repository is "live" at the moment and has a lot of stars on GitHub - arnaud-lb / php-rdkafka.
In the 21st year, Docker is like a whip to Indiana Jones for a developer.
git clone https://github.com/Phillaf/php-kafka-demo
docker-compose up -d
From the first try, of course, the project did not come together (at least on my MacOs BigSur).
In order to fix the error, it was enough to simply remove the well-defined version of Kafka.

kafka-ui:
image: provectuslabs/kafka-ui
container_name: kafka-ui
ports:
- "8080:8080"
restart: always
environment:
- KAFKA_CLUSTERS_0_NAME=local
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9092
- KAFKA_CLUSTERS_0_ZOOKEEPER=zookeeper:2181
- KAFKA_CLUSTERS_0_READONLY=false
php ./public/consumer_low.php
We will see our messages in the console.Congratulations, you are now Kafka adepts.
As I wrote above, we are interested in full control over messages, our application wants to receive a message exactly 1 time, no more and no less. At the same time, we want to be sure that if in the process of processing a message, the consumer suddenly ends his work (as a result of deployment, an error, or a server crash, ...) then after restarting, this message will be processed, and will not sink into oblivion.
$conf->set('enable.auto.commit', 'false');
$conf->set('group.id', 'group_1');
Start listening to the queue:
$topic->consumeStart($partition, RD_KAFKA_OFFSET_STORED);
1st parameter - number of the partition to be read (by default, the topic has the 1st partition and the value of the variable will be = 0). 2nd parameter - number - offset from which to start reading messages. There are 3 preset modes:
Receiving a message from the queue:
$msg = $topic->consume($partition, 1000);
$topic->offsetStore($partition, $msg->offset);
Voila, we have implemented consistent work with messages in the queue using Apache Kafka.
I would be glad to hear your questions or comments)