<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Brian Hamner</title>
    <description>The latest articles on DEV Community by Brian Hamner (@bhamner).</description>
    <link>https://dev.to/bhamner</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4271715%2Fa7a3ec49-2763-400a-a1bc-ddce375ce738.png</url>
      <title>DEV Community: Brian Hamner</title>
      <link>https://dev.to/bhamner</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhamner"/>
    <language>en</language>
    <item>
      <title>Using Redis Streams Between Laravel Applications</title>
      <dc:creator>Brian Hamner</dc:creator>
      <pubDate>Tue, 01 Dec 2026 17:40:40 +0000</pubDate>
      <link>https://dev.to/bhamner/using-redis-streams-between-laravel-applications-6hp</link>
      <guid>https://dev.to/bhamner/using-redis-streams-between-laravel-applications-6hp</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;The other thing I liked was being able to run multiple workers within each group.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Since I was doing this in Laravel, I wanted something closer to what I was already used to working with.&lt;/p&gt;

&lt;h2&gt;
  
  
  The package
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  Publishing messages
&lt;/h3&gt;

&lt;p&gt;I can define a stream much like I would an Eloquent model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Bhamner&lt;/span&gt;\&lt;span class="nc"&gt;RedisStreams&lt;/span&gt;\&lt;span class="nc"&gt;Stream&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderStream&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Stream&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Publishing a message then looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;OrderStream&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'type'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'order.placed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'order_id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'123'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Consuming messages
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;For instance, if I want to read and acknowledge a message myself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$entry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OrderStream&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;group&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'billing'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'worker-1'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;ensure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$entry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Process the entry.&lt;/span&gt;

    &lt;span class="nv"&gt;$entry&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;ack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;For most of my use cases I'd just define a consumer, similar to how I'd write a Laravel job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt;\&lt;span class="nc"&gt;Streams&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Bhamner&lt;/span&gt;\&lt;span class="nc"&gt;RedisStreams&lt;/span&gt;\&lt;span class="nc"&gt;Consumer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Bhamner&lt;/span&gt;\&lt;span class="nc"&gt;RedisStreams&lt;/span&gt;\&lt;span class="nc"&gt;Entry&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BillingConsumer&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Consumer&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$stream&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'orders'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$group&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'billing'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'0'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Entry&lt;/span&gt; &lt;span class="nv"&gt;$entry&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Process the order.&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run it using Artisan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan redis-streams:work &lt;span class="s2"&gt;"App\Streams\BillingConsumer"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;h3&gt;
  
  
  Handling failures
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F23uqjv4yp88smzwvp3n9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F23uqjv4yp88smzwvp3n9.png" alt=" " width="800" height="409"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Consumers still need to account for the possibility of processing a message more than once.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;If you're interested in trying it out, I've included more examples and documentation on GitHub. Feedback is welcome.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/bhamner/laravel-redis-streams" rel="noopener noreferrer"&gt;https://github.com/bhamner/laravel-redis-streams&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>redis</category>
      <category>php</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
