Open-source project
php-rdkafka/php-rdkafka avatar
php-rdkafka/php-rdkafka

php-rdkafka loses queued messages unless you call flush() before the producer is destroyed

Production-ready, stable Kafka client for PHP

2,177 stars278 forksCMIT

At a glance

What is it?
php-rdkafka/php-rdkafka is a PHP extension written in C that binds librdkafka. It is a thin client with producer, high-level consumer, legacy low-level consumer and metadata APIs, and its worst failure is silent message loss at shutdown.
Who is it for?
Use php-rdkafka if you run PHP 8.1 or newer and want a low-level Kafka client that follows librdkafka's configuration names, with the high-level KafkaConsumer for consuming. Skip it if you expect an opinionated framework that hides librdkafka settings and shutdown rules.
Can I use it commercially?
Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository received new commits within the last day.
What is it written in?
Mainly C, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 26, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • php-rdkafka/php-rdkafka2,177 ★ · -4
Star history
Repository2026-10-082026-11-26
php-rdkafka/php-rdkafka2.2k2.2k

-4 Stars in the last 49 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Not calling flush can lead to message loss

The README carries two warnings for producers. One says to make sure the producer follows proper shutdown, and the other says outright that not calling flush can lead to message loss. Messages passed to produce are queued inside librdkafka, and the call returns before they reach a broker.

php
$rk->flush($timeout_ms);

The flush call should run before the producer instance is destroyed, so that all queued and in-flight produce requests complete before termination. The README asks for a reasonable value for $timeout_ms. In a PHP script that ends right after producing, forgetting the call drops whatever is still queued, and nothing raises an error.

For code that does not care about unsent messages, there is purge:

php
// Forget messages that are not fully sent yet
$rk->purge(RD_KAFKA_PURGE_F_QUEUE);

$rk->flush($timeout_ms);

Purge discards the queue, and the flush that follows waits for what is already in flight.

A producer is a Conf, a broker list and a topic

Producing starts with configuration, then a producer, then a topic:

php
<?php
$conf = new RdKafka\Conf();
$conf->set('log_level', (string) LOG_DEBUG);
$conf->set('debug', 'all');
$rk = new RdKafka\Producer($conf);
$rk->addBrokers("10.0.0.1:9092,10.0.0.2:9092");

The topic comes from $rk->newTopic("test"), and a message goes out with the produce method:

php
<?php

$topic->produce(RD_KAFKA_PARTITION_UA, 0, "Message payload");

The first argument is the partition, and RD_KAFKA_PARTITION_UA means unassigned, which lets librdkafka choose the partition. The second argument is message flags, either 0 or RD_KAFKA_MSG_F_BLOCK to block produce on a full queue. With 0 and a full queue, the call does not block, so a high-volume producer has to handle that case or use the block flag. The payload can be anything.

Configuration names such as log_level and debug come from the librdkafka configuration reference, which the README links.

The high-level KafkaConsumer is the one to use, and the low-level consumers are legacy

Two notes in the README say the low-level consumer is a legacy API and ask you to prefer the high-level consumer. The RdKafka\KafkaConsumer class supports automatic partition assignment and revocation, and the README points to an example page for it.

The legacy path builds an RdKafka\Consumer, adds brokers, creates a topic and calls consumeStart(0, RD_KAFKA_OFFSET_BEGINNING). The offset argument accepts RD_KAFKA_OFFSET_BEGINNING, RD_KAFKA_OFFSET_END or RD_KAFKA_OFFSET_STORED. A loop then calls $topic->consume(0, 1000), where the first argument is the partition again and the second is the timeout, and checks for null, for RD_KAFKA_RESP_ERR__PARTITION_EOF and for an error string.

A second legacy pattern forwards several topics and partitions into one queue using newQueue() and consumeQueueStart, then calls $queue->consume(1000). Because the partition is managed by hand in both, a new project gains nothing from them. They matter for maintaining older code.

Offsets are stored on the broker unless you opt into files

A section on stored offsets says librdkafka by default stores offsets on the broker. A deprecated alternative stores them in a local file. In that case, the file is created in the current directory with a name based on the topic and the partition, and the directory changes through the offset.store.path configuration property.

The file approach is labeled deprecated, so anything new should leave offsets on the broker. The consequence for older consumers is that the offset state lives in the working directory of whatever process ran them, and running the same script from a different directory starts from different offsets.

The table of contents also names three configuration parameters worth reading about: queued.max.messages.kbytes, topic.metadata.refresh.sparse together with topic.metadata.refresh.interval.ms, and internal.termination.signal. Their explanations fall in the part of the README beyond what is shown here, so read them in the repository before tuning memory or metadata refresh.

Version support splits at PHP 8.1 and the 7.x branch

The current version supports PHP >= 8.1.0, librdkafka >= 1.5.3 and Kafka >= 0.8. Version 6.x supports PHP 7.x through 8.x with librdkafka 0.11 through 2.x, and older versions support PHP 5. The default branch of the repository is called 7.x.

The GitHub release list does not match that. The latest releases shown are 6.0.5 on 2024-11-04, 6.0.4 on 2024-10-24 and 6.0.3 on 2022-07-02, with no 7.x release among them. A user on PHP 7 needs the 6.x branch, and a user on 8.1 or later should find out from PECL or the documentation which version they will receive before installing.

The repository is not archived. The last push was 2026-10-20. Upgrade notes are in UPGRADE.md at the top level, and a CREDITS file and CONTRIBUTING.md sit next to it.

The extension is a low-level binding, by stated design

The README says the goal is to be a low-level, un-opinionated librdkafka binding focused on production and long-term support. It supports high-level and low-level consumers, a producer and metadata APIs. The description calls it stable, production-ready and fast, which are the maintainers' own words and come with no benchmark in the README.

The source tree shows how the binding is built. The repository holds C files for conf, kafka_consumer, message, metadata, metadata_broker, metadata_collection, metadata_partition, metadata_topic and oauthbearer, each next to a .stub.php file and an _arginfo.h file, plus config.m4 for Unix builds and config.w32 for Windows. Installation steps are not in the README itself, which only links to the setup page on the documentation site.

Because the extension follows librdkafka, tuning means reading librdkafka's configuration reference, and errors come back as librdkafka codes or strings.

Tests need a Kafka broker, and docker-compose.yml supplies one

The docker-compose.yml defines a build-env service built from .docker/Dockerfile with PHP_VERSION 8.3, and a zookeeper service and a kafka service from wurstmeister images. The environment sets TEST_KAFKA_BROKERS to kafka:9092 and TEST_KAFKA_BROKER_VERSION to 2.6. The Kafka service auto-creates topics and creates test-topic with the compact setting.

An optional kafka-oauth service listens on 29092 with SASL plaintext and the OAUTHBEARER mechanism, used only for the oauthbearer_integration tests, and it starts with `docker compose --profile oauth up -d`. The compose file sets TEST_KAFKA_OAUTH_BROKERS for it.

A contributor needs Docker to run the full test suite, and the integration tests depend on a Kafka 2.6 broker. The license is MIT, and the repository has an examples directory whose README leads to the documentation examples.

Editorial conclusion

Use php-rdkafka if you run PHP 8.1 or newer and want a low-level Kafka client that follows librdkafka's configuration names, with the high-level KafkaConsumer for consuming. Skip it if you expect an opinionated framework that hides librdkafka settings and shutdown rules. Before going live, test that every producer path calls flush() with a sensible timeout, because queued messages are lost without it.

Frequently asked questions

What is PHP coding used for with php-rdkafka?

Here PHP code talks to Apache Kafka brokers through php-rdkafka, a C extension based on librdkafka. It supports producing, consuming and reading metadata.

What is PHP full form, and which PHP does php-rdkafka need?

The repository does not expand the name PHP. The current version of php-rdkafka supports PHP 8.1.0 and newer, while the 6.x branch covers PHP 7.x and 8.x.

How do I avoid losing messages with php-rdkafka?

Call $rk->flush($timeout_ms) before the producer is destroyed. The README warns that not calling flush can lead to message loss.

Official sources

  1. License: MIT
  2. php-rdkafka/php-rdkafka on GitHub
  3. Project website
  4. README
  5. Releases
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/php-rdkafka-php-rdkafka.svg)](https://hysenlabs.com/projects/php-rdkafka-php-rdkafka)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/php-rdkafka-php-rdkafka.svg)](https://hysenlabs.com/projects/php-rdkafka-php-rdkafka)

Was this useful?