Apache Kafka is a great tool for building event-driven applications. In this post, we will connect Kafka with Laravel using a real e-commerce example: processing an order with payment and stock update.
What is Kafka ?
Apache Kafka is a distributed messaging platform, mainly used for:
- Real-time event processing
- Microservice Integration
- Scalable data collection and analysis
Initial Setup
To use Kafka with Laravel:
- Install the
rdkafka
PHP extension. - Install the kafka client:
RUN pecl install rdkafka \
&& docker-php-ext-enable rdkafka
- Install composer package:
composer require edenhill/php-rdkafka
Producer: Sending Order To Kafka
// app/Kafka/KafkaProducer.php
namespace App\Kafka;
use RdKafka\Producer;
class KafkaProducer
{
public function send(string $topicName, array $data): void
{
$conf = new \RdKafka\Conf();
$producer = new Producer($conf);
$topic = $producer->newTopic($topicName);
$topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode($data));
$producer->flush(1000);
}
}
public function placeOrder(Request $request, KafkaProducer $producer)
{
$orderData = [
'order_id' => 123,
'user_id' => 1,
'product_id' => 45,
'quantity' => 2,
'amount' => 150.00
];
$producer->send('order-events', $orderData);
return response()->json(['status' => 'Order sent for processing']);
}
Consumer
Let's create a Kafka consumer that:
// app/Kafka/KafkaConsumer.php
namespace App\Kafka;
use RdKafka\Consumer;
use Illuminate\Support\Facades\Log;
class KafkaConsumer
{
public function consume(string $topicName)
{
$conf = new \RdKafka\Conf();
$conf->set('group.id', 'order-processing');
$consumer = new Consumer($conf);
$consumer->addBrokers('localhost:9092');
$topic = $consumer->newTopic($topicName);
$topic->consumeStart(0, RD_KAFKA_OFFSET_BEGINNING);
while (true) {
$message = $topic->consume(0, 1000);
if ($message && $message->payload) {
$order = json_decode($message->payload, true);
Log::info('Processing order', $order);
$paymentSuccess = $this->processPayment($order);
if ($paymentSuccess) {
$this->updateStock($order['product_id'], $order['quantity']);
Log::info("Order {$order['order_id']} paid and stock updated");
} else {
Log::warning("Payment failed for order {$order['order_id']}");
}
}
}
}
protected function processPayment(array $order): bool
{
// Simulate payment logic
return rand(0, 1) === 1;
}
protected function updateStock(int $productId, int $quantity): void
{
// Update your database here
Log::info("Reducing stock of product {$productId} by {$quantity}");
}
}
Create artisan command to run consumer
php artisan make:command KafkaProcessOrders
// app/Console/Commands/KafkaProcessOrders.php
public function handle()
{
app(\App\Kafka\KafkaConsumer::class)->consume('order-events');
}
Run it with:
php artisan kafka:process-orders
Conclusion
Using Laravel with Kafka is a powerful way to build modern, event-driven applications. Instead of doing everything inside one controller, you can send events to Kafka and let different consumers handle the work — like processing payments, updating stock, or sending emails.
This approach brings many benefits:
✅ Better performance (non-blocking)
✅ Easier to scale
✅ Cleaner and more organized code
Even if your system is small today, using this architecture makes it ready to grow.
Start simple — like in this example — and improve step by step.