Skip to main content

RabbitMQ Streams

RabbitMQ has been the message broker of my choice for a long time. It has served me well over the years and I still like to use it today. Recently, I was able to add an extra reason to the list why I like RabbitMQ when I noticed that a new feature was added in RabbitMQ 3.9; Streams.

RabbitMQ Streams

From the documentation:

Streams are a new persistent and replicated data structure in RabbitMQ 3.9 which models an append-only log with non-destructive consumer semantics.

With streams you get Kafka like functionality in RabbitMQ without all the complexity that comes with maintaining and managing your Kafka cluster. It has been created with the following use cases in mind:

  • Large amount of subscribers; in traditional queuing we use a dedicated queue for each consumers. This becomes ineffective whehn we have large number of consumers.
  • Time-travelling; Streams will allow consumers to attach at any point in the log and read from there.
  • Performance: Streams have been designed with performance as a major goal
  • Large logs: Streams are designed to store larger amounts of data in an efficient manner with minimal in-memory overhead.

If you want a good introduction, have a look at the following video:

Using RabbitMQ Streams with .NET

Although I hope that there will soon be a MassTransit Rider implementation for RabbitMQ Streams, right now the way to go is through the RabbitMQ.Stream.Client NuGet package.

Let’s build a small sample application to try it out…

Enable the RabbitMQ Streams plugin

Before we can start we need to enable RabbitMQ Streams. This is available as a separate plugin and is not activated out of the box:

rabbitmq-plugins enable rabbitmq_stream

Tip: If you want to test it locally you can use the RabbitMQ image I’ve created a Dockerfile where this plugin is already enabled: https://github.com/wullemsb/docker-rabbitmq

Create the stream

First we need to construct a configuration object. RabbitMQ streams is using a separate port (by default this is port 5552).

Now that we are connected to our RabbitMQ cluster, we can create a new Stream through CreateStream(). This is an idempotent operation so you can safely call this multiple times.

It is important when creating the stream to specify a retention policy to prevent the stream from growing infinitely. In our example we limit the queue to 200000 bytes.

Add a producer

Now it is time to create our producer.

And we can start publishing events through the Send() method.

The Send() method expect a publishingId that should be incremented for each send and our message payload.

Add a consumer

Almost there. Time to consume these published messages…

Notice the OffsetSpec. This allows us to specify from where the stream should be consumed. In this example we have set it to OffsetTypeFirst meaning the beginning of the stream.

    If we now run the application, we get output like this:

    Here is the full example:

    Popular posts from this blog

    XUnit - Assert.Collection

    A colleague asked me to take a look at the following code inside a test project: My first guess would be that this code checks that the specified condition(the contains) is true for every element in the list.  This turns out not to be the case. The Assert.Collection expects a list of element inspectors, one for every item in the list. The first inspector is used to check the first item, the second inspector the second item and so on. The number of inspectors should match the number of elements in the list. An example: The behavior I expected could be achieved using the Assert.All method:

    Angular --deploy-url and --base-href

    As long you are running your Angular application at a root URL (e.g. www.myangularapp.com ) you don’t need to worry that much about either the ‘--deploy-url’ and ‘--base-href’ parameters. But once you want to serve your Angular application from a server sub folder(e.g. www.mywebsite.com/angularapp ) these parameters become important. --base-href If you deploy your Angular app to a subfolder, the ‘--base-href’ is important to generate the correct routes. This parameter will update the <base href> tag inside the index.html. For example, if the index.html is on the server at /angularapp/index.html , the base href should be set to <base href="/angularapp/"> . More information: https://angular.io/guide/deployment --deploy-url A second parameter that is important is ‘--deploy-url’. This parameter will update the generated url’s for our assets(scripts, css) inside the index.html. To make your assets available at /angularapp/, the deploy url should

    Azure DevOps/ GitHub emoji

    I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.