How to acknowledge consume message in kafka using php-rdkafka?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. It enables developers to publish and subscribe to streams of records, similar to a message queue or enterprise messaging system. Among various programming languages that can interface with Kafka, PHP can be utilized through the php-rdkafka extension, which provides a low-level Kafka consumer and producer client.
Understanding Message Consumption in Kafka
In Kafka, messages are stored in topics which can be split into partitions to allow for parallel consumption. Consumers read messages from these partitions. One crucial aspect of Kafka consumption is message acknowledgment, often referred to as committing offsets. An offset is a unique identifier for a record within a partition. By committing an offset, the consumer is essentially informing Kafka that it has successfully processed all prior messages up to that offset. Failure to properly commit offsets can lead to message duplication or loss.
Installing php-rdkafka
Before you can consume messages with Kafka using PHP, you need to set up the php-rdkafka extension. You can typically install it via PECL:
You'll then need to add the extension to your PHP configuration:
Setting Up the Consumer
Consuming messages from Kafka involves setting up a Consumer object, configuring it, subscribing to topics, and then continually polling the broker for new messages. Here’s a basic setup:
Consuming Messages
To consume messages, you can use a loop that will keep running to receive new messages as follows:
Acknowledging Messages
To ensure that messages are acknowledged (offsets are committed), you can either enable auto-commit or manually control when offsets are committed. The choice often depends on the processing requirements and the desired level of control over the message acknowledgment.
Auto-commit
This is enabled by default. The auto.commit.interval.ms setting in TopicConf determines how frequently offsets are committed. This is a hands-off approach but may lead to duplicated processing if your consumer fails between commits.
Manual Commit
If you need more control or want to ensure that messages are only committed after certain conditions are met (e.g., after successfully processing a message), you can manually commit offsets:
Summary Table
Here is a table summarizing the key points related to message consumption and acknowledgment in Kafka using php-rdkafka:
| Feature | Description |
| Auto-offset reset | Controls how offsets are reset (e.g., smallest, earliest) |
| Auto-commit | Commits offsets periodically (controlled by auto.commit.interval.ms) |
| Manual commit | Allows offset commits to be managed manually by calling offsetStore |
| Group ID | Identifies the consumer group for coordination and offset tracking |
Conclusion
Using php-rdkafka to consume and acknowledge messages from Kafka requires understanding of Kafka's offset behavior and a careful consideration of the commit strategy. Depending on the application's needs, developers can choose between higher throughput with potential message duplication (auto-commit) or lower throughput with more strict processing guarantees (manual commit).
Related reading
- How to acknowledge current offset in spring kafka for manual commit
- How to add a header keyvalue pair when publishing a message with pika
- How to add initial users when starting a RabbitMQ Docker container?
- How to add JVM parameters to Apache Kafka?
- How to add plugin to RabbitMQ docker image?
- How to alter the TTL for a particular topic in Kafka
- How to always consume from latest offset in kafka-streams
- How to apply an SMT to a single topic in Kafka connect?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.