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 messagesAcks 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:
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 streams | Kafka | |
|---|---|---|
| Model | Append-only replicated log | Append-only replicated log |
| Partitioning | Super streams | Partitions |
| Consumer groups with offsets | Single active consumer + offset tracking | Consumer groups |
| Ecosystem | RabbitMQ's: same broker as your queues | Connect, Streams, Schema Registry, huge |
| Scale | Large | Larger |
| Running cost | Already running RabbitMQ | A separate cluster |
Streams fill the gaps that pushed teams from RabbitMQ to Kafka: replay, large fan-out to many consumers, and big backlogs. If you already run RabbitMQ and need an audit log, a replayable event feed, or fan-out to dozens of consumers, a stream is often enough. If events are the backbone of the whole company, with CDC, stream processing, and a large connector ecosystem, that's still Kafka.
Lab
- Run
streams.py. - Publish 100,000 messages to
events.stream, then read fromx-stream-offset: "5m"and from a timestamp a minute ago. - In the UI, open
events.streamand compare its view with a quorum queue's: there's no Ready/Unacked, because nothing is consumed away. - Create a stream with
x-max-age: 1mand small segments (x-stream-max-segment-size-bytes: 100000), publish, wait, and watch old segments get removed.