Using Redis Streams Between Laravel Applications

2026-12-02来源 dev.toEnglish
Using Redis Streams Between Laravel Applications

A while back I started using Redis Streams to handle communication between several Laravel applications. I liked the idea of an application being able to publish an event without needing to know which other applications were interested in it.

One of the reasons I went with streams instead of Redis Pub/Sub was that I didn't want to lose events if one of the applications happened to be down. With streams, each application can have its own consumer group and pick up where it left off, provided the messages are still in the stream.

The other thing I liked was being able to run multiple workers within each group.

Working directly with Redis wasn't particularly difficult, but there was a fair amount of code involved in managing the consumers. I needed to acknowledge messages, reclaim pending messages from failed workers and figure out what to do with messages that kept failing.

Since I was doing this in Laravel, I wanted something closer to what I was already used to working with.

The package

There are already a few Laravel packages for Redis Streams. What I wanted was a little different in terms of how I'd interact with streams in my applications.

I ended up writing a package that borrows a little from Eloquent and a little from Laravel's queue system. The idea was to make publishing to a stream feel like creating a record and consuming messages feel like writing a job.

Publishing messages

I can define a stream much like I would an Eloquent model.

use Bhamner\RedisStreams\Stream; class OrderStream extends Stream { protected string $name = 'orders'; } 

Publishing a message then looks like this:

OrderStream::create([ 'type' => 'order.placed', 'order_id' => '123', ]); 

Consuming messages

I also didn't want to build the package entirely around events and listeners. There are other things I might want to use streams for, and sometimes I just want to work directly with a stream or consumer group without having to drop down to raw Redis commands.

For instance, if I want to read and acknowledge a message myself:

$entry = OrderStream::group('billing') ->consumer('worker-1') ->ensure('0') ->pop(block: 2000); if ($entry) { // Process the entry. $entry->ack(); } 

Most of the time I'd rather let a worker handle the details, but I wanted the flexibility to do things myself when I needed to.

For most of my use cases I'd just define a consumer, similar to how I'd write a Laravel job.

namespace App\Streams; use Bhamner\RedisStreams\Consumer; use Bhamner\RedisStreams\Entry; class BillingConsumer extends Consumer { protected string $stream = 'orders'; protected string $group = 'billing'; protected string $start = '0'; public function handle(Entry $entry): void { // Process the order. } } 

And run it using Artisan:

php artisan redis-streams:work "App\Streams\BillingConsumer" 

The worker takes care of acknowledging messages, recovering pending entries and handling failures. I just need to worry about what happens in handle().

Handling failures

For failed messages, I went with a fairly simple approach. The message stays pending until it's been idle for a configurable amount of time, at which point a worker can claim it again. If it keeps failing and reaches the delivery limit, the worker copies it to a dead-letter stream and acknowledges the original.

Consumers still need to account for the possibility of processing a message more than once.

I don't see this as a replacement for Laravel queues. I use both. Streams just make more sense for some of the things I've needed to do across applications.

I've recently released v1 of the package. It's MIT licensed and also works with Upstash, which is nice for smaller projects where you might not want to maintain a Redis server.

If you're interested in trying it out, I've included more examples and documentation on GitHub. Feedback is welcome.

https://github.com/bhamner/laravel-redis-streams

原文:https://www.a8q9.cn/en/article/using-redis-streams-between-laravel-applications-fa5d72.html

相关文章