Event Streaming¶
Event streaming is a pattern where events are continuously published to an ordered, durable log (a stream), and consumers read from that log at their own pace, independently of producers. Unlike message queues (where a message is consumed once and deleted), a stream is retained and can be replayed, and multiple consumer groups can read the same events independently.
The key properties of event streaming are: persistence (events are stored durably), ordering (events within a partition are ordered), and replayability (consumers can reset their offset and re-read events from the beginning or any point in time). These properties make event streaming suitable for event sourcing, real-time analytics, change data capture, and building multiple independent views from the same event log.
Apache Kafka is the most widely used event streaming platform. AWS Kinesis and Google Cloud Pub/Sub provide managed alternatives. In PHP, producers publish events via the rdkafka extension or high-level libraries; consumers run as long-lived workers using RoadRunner, Swoole, or ReactPHP.
<?php
// Producer: publish an event to a Kafka topic
$conf = new RdKafka\Conf();
$conf->set('metadata.broker.list', 'kafka:9092');
$producer = new RdKafka\Producer($conf);
$topic = $producer->newTopic('orders');
$topic->produce(RD_KAFKA_PARTITION_UA, 0, json_encode([
'event' => 'OrderPlaced',
'orderId' => 42,
'total' => 9900,
'timestamp' => time(),
]));
$producer->flush(1000);
// Consumer: read events independently, at own pace
$conf->set('group.id', 'inventory-service');
$conf->set('auto.offset.reset', 'earliest'); // replay from beginning if needed
$consumer = new RdKafka\KafkaConsumer($conf);
$consumer->subscribe(['orders']);
while (true) {
$message = $consumer->consume(1000);
if ($message->err === RD_KAFKA_RESP_ERR_NO_ERROR) {
$event = json_decode($message->payload, true);
// process independently from the producer
}
}
?>
See also Apache Kafka documentation.
Related : Event Sourcing, Event Driven, Message Queue, Pub/Sub, Event Store, Event Replay