Learn Labs
Queue Types

Streams

Replayable, non-destructive logs inside RabbitMQ, and when to use them

A stream is RabbitMQ's answer to Kafka: an append-only, replicated log where consuming doesn't remove anything. Consumers choose where to start reading, and many consumers can read the same data independently, as often as they like.

Declaring and reading

ch.queue_declare("lab.stream", durable=True,
               arguments={"x-queue-type": "stream", "x-max-age": "1D"})
for i in range(10):
  ch.basic_publish("", "lab.stream", body=f"event-{i}")

cch.basic_qos(prefetch_count=100)                       # required for stream consumers
cch.consume("lab.stream", arguments={"x-stream-offset": "first"})
two independent readers:
x-stream-offset="first"      ['event-0', 'event-1', ..., 'event-9']
x-stream-offset=5            ['event-5', 'event-6', 'event-7', 'event-8', 'event-9']
read again (nothing was consumed away):
x-stream-offset="first"      ['event-0', ..., 'event-9']
stream still holds 10 messages
lab.streame0e1e2e3e4e5e6e7e8e9← publishers appendx-stream-offset="first"x-stream-offset=5
x-stream-offset: "first"the oldest retained messagex-stream-offset: "last"the start of the last chunkx-stream-offset: "next"only new messages (the default)x-stream-offset: 5a numeric offsetx-stream-offset: <timestamp>the first message at or after a point in timex-stream-offset: "10m"an interval: the last 10 minutes

Acks on a stream don't delete anything; they only drive flow control, which is why a prefetch is mandatory.

Retention

Streams are trimmed by age and/or size, by whole segments, just like Kafka:

x-max-age: 7Ddrop segments older than 7 days (Y, M, D, h, m, s)x-max-length-bytes: 20000000000or when the stream exceeds 20 GBx-stream-max-segment-size-bytes: 500000000segment size (the unit of deletion)

The pre-loaded events.stream keeps 7 days in 50 MB segments.

The stream protocol

AMQP works with streams, as the lab shows, but the dedicated stream protocol (port 5552, plugin rabbitmq_stream, enabled here) is much faster, and adds features: server-side offset tracking (a named consumer stores its offset in the stream), super streams (partitioned streams, like a Kafka topic with partitions), and single active consumer per partition. Use a stream client library (rstream for Python, the official Java, Go, .NET clients) when throughput matters.

Streams vs Kafka

RabbitMQ streamsKafka
ModelAppend-only replicated logAppend-only replicated log
PartitioningSuper streamsPartitions
Consumer groups with offsetsSingle active consumer + offset trackingConsumer groups
EcosystemRabbitMQ's: same broker as your queuesConnect, Streams, Schema Registry, huge
ScaleLargeLarger
Running costAlready running RabbitMQA separate cluster

Lab

  1. Run streams.py.
  2. Publish 100,000 messages to events.stream, then read from x-stream-offset: "5m" and from a timestamp a minute ago.
  3. In the UI, open events.stream and compare its view with a quorum queue's: there's no Ready/Unacked, because nothing is consumed away.
  4. Create a stream with x-max-age: 1m and small segments (x-stream-max-segment-size-bytes: 100000), publish, wait, and watch old segments get removed.

On this page