RillMQ: a message broker in Rill that RabbitMQ clients can talk to
RillMQ is a lock-free message broker written in Rill. It speaks AMQP 0-9-1, so RabbitMQ clients connect unchanged. Version 0.2.1 no longer hangs on a client that stops reading. How it works, side-by-side measurements against RabbitMQ 4.3.2 (2.5 times on confirmed durable publish), and two weak spots still in the cluster.

RillMQ is a message broker written in Rill. It keeps messages in memory, writes them to a journal and speaks AMQP 0-9-1, so RabbitMQ's own client libraries connect to it unchanged. The first 62 commits took one day and produced 0.1.0. Six more made 0.2.1, and they are nearly all about one question: what happens when a client stops reading? The acceptance test is RabbitMQ's .NET client. The code and ready-made packages are on GitHub: github.com/parisxmas/RillMQ.
This post covers how it works, what changed in 0.2.1, side-by-side measurements against RabbitMQ, and two weak spots in the cluster that are still there. Every number here was measured on the official 0.2.1 package.
Two doors, the same queues
RillMQ speaks two protocols. The first is its own text protocol. You can talk to it by hand with nc, and because a body's length comes first, a parser never has to look inside a message.
$ nc localhost 6789
PUB jobs 5
hello
+OK 1
SUB jobs
+OK
MSG 1 jobs 5
hello
ACK jobs 1
+OK
The second is AMQP 0-9-1. The point is not the protocol itself, it is that a client that has never heard of RillMQ works. The target is the ordinary code everyone writes:
var factory = new ConnectionFactory { HostName = "127.0.0.1", Port = 5672 };
using var conn = factory.CreateConnection();
using var ch = conn.CreateModel();
ch.QueueDeclare("orders", durable: true, exclusive: false, autoDelete: false);
ch.BasicPublish("", "orders", null, Encoding.UTF8.GetBytes("hello"));
A message the .NET client publishes over AMQP is delivered to a subscriber on the native protocol. Both doors open onto the same queue and the same journal.
There are also four exchange kinds (direct, fanout, topic, headers), exchange-to-exchange bindings, virtual hosts, three rights per person and host, TLS, EXTERNAL sign-in with a client certificate, publisher confirms, mandatory and Connection.Blocked.
No locks
There is not a single lock in RillMQ, and nothing is shared between strands. A strand is Rill's lightweight thread. Instead of a data structure several connections can reach, a queue is itself a strand, and nothing else touches its messages.
| strand | how many | what it owns |
|---|---|---|
| registry | one | which queues exist, and the channel to each |
| queue | one per queue | its messages, subscribers and what is in flight |
| connection reader | one per connection | the bytes arriving |
| connection writer | one per connection | the socket's write side |
| watchdog | one per listener | noticing a connection whose writes have stopped moving |
Everything else is a channel. A connection that wants to publish drops a request on the queue's channel with a reply channel inside it. Stopping is one close(ch): every strand watches the same done channel, so they all hear it at once.
This design has a cost, and the code paid it once. An AMQP session both asks queues questions and is where their deliveries come out. Merging the two into one inbox deadlocked: the session stood waiting for an answer while the queue stood trying to put a delivery into the pipe the session had stopped emptying. 20,000 publishes and one Queue.Declare were enough. The fix was two inboxes, and offering a send in the same select as a receive.
A client that stops reading
A broker's real test is not a fast client but a slow one. In 0.1.0 a window of 64 messages per consumer was keeping that problem out of sight. A client could turn the window off, though, because that is what Basic.Qos with a zero means. Then the session filled the socket, parked in a write, and nobody was left to read acknowledgements. The client was waiting for the broker to read, and the broker was waiting for the client to read.
test/slow.sh is new, and it is made of clients too simple to hide this: they read and write on one thread. I ran three of its cases against both versions, 50,000 messages each:
| 50,000 messages | 0.1.0 | 0.2.1 |
|---|---|---|
| AMQP, no window | stuck in 10 of 10 runs | finished 10 of 10, 0.42 s |
| AMQP, acknowledging in batches of 5,000 | stuck in 5 of 5 runs | finished 5 of 5, 0.39 s |
native protocol, prefetch=0 | stuck in 5 of 5 runs | finished 5 of 5, 0.35 s |
Stuck means no progress for 20 seconds, or for the batch row, no finish within 45 seconds. The AMQP client usually stopped at around 39,000 messages and the native one at 10,320. These three turned out to have three different causes:
- The session wrote to the socket itself. Now a strand of its own does the writing and the session only adds to a buffer, so it goes on reading acknowledgements whatever the client does. A client a megabyte behind stops being given deliveries, and one that leaves 64 MB of replies unread is disconnected.
- The native protocol had asked the runtime to buffer its writes. Flushing a full socket parks whichever strand was about to park, and a subscriber stopped hearing with every buffer empty. It looked like a deadlock and was not one. A strand now gathers the writes, at a cost of about four per cent of delivery. Going without would cost a third.
- The queue only offered a passed-over message again when something next happened. A consumer acknowledging in batches was waiting for more before saying anything, so nothing happened. While there is a message and a window with room, the queue now looks again every five milliseconds.
Two more problems came out behind those. A client that stopped reading and stayed connected held its connection for ever, so a watchdog now cuts off a connection whose write has not moved for stall=<ms>, 30 seconds by default. It counts progress in 64 KB pieces, so a client that is only slow keeps its connection. The worse one was in the journal. The journal answers a publish with a send that waits, and a client that left with more owed to it than the channel held stopped the journal for good. The queues stopped behind it and new connections went quiet. Only timing had been hiding this. A closing connection now empties what it leaves behind.
Linux had the last word. 0.2.0 passed on a Mac and was tagged, but its checks failed on Linux and it was never released. An epoll set has one entry per descriptor, so a strand waiting to write replaced the wait of the strand reading the same socket. kqueue keeps the two apart. The fix went into the language rather than the broker, as Rill 0.16.1, and that is why the first public version after 0.1.0 is 0.2.1. Timers are descriptors on Linux too, so the watchdogs became one per listener instead of two per connection.
Writing to disk
Each queue has one file, and it is only ever appended to. A queue is rebuilt by reading the records in order and keeping what was published and not acknowledged. Every record ends with an FNV-1a checksum. It is not there for disk corruption, it is there for the last record, the one the power cut off halfway: nothing after a record that does not add up is read, and the file is cut back to that point.
The publisher's +OK goes out after the fsync. The syncs are batched naturally rather than by a timer. While one batch is going to disk, everything arriving piles up behind it and goes out together on the next turn. If a sync costs a millisecond, that is a millisecond per batch, not per message.
The journal also shortens itself. Once records outnumber live messages four to one, the queue hands over what it holds, and the journal writes a new file, syncs it, renames it over the old one and syncs the directory. So restart time depends on what is left in the queue, not on its history. Measured after kill -9:
| messages waiting | journal | all ready |
|---|---|---|
| 500,000 | 41.5 MB | 0.14 s |
| 2,000,000 | 166 MB | 0.56 s |
| 4,000,000 | 332 MB | 1.15 s |
The broker answers PING within about 65 ms. The table shows the time until every message is visible.
Side by side with RabbitMQ
In the HyperLogLog measurements, starting one server differently skewed the results. So here the conditions were held fixed: Apple M4, the official RillMQ 0.2.1 package, RabbitMQ 4.3.2 with default settings and no plugins, RabbitMQ.Client 6.8.1, 200,000 messages of 64 bytes. Both servers were started the same way, only one ran at a time, and five rounds alternated between them, each on a fresh broker. The figures are medians.

| through the same .NET client | RabbitMQ | RillMQ | ratio |
|---|---|---|---|
| durable publish, waiting for confirms | 318,000/s | 794,000/s | 2.49 |
| transient publish | 267,000/s | 831,000/s | 3.11 |
| deliver and acknowledge | 172,000/s | 266,000/s | 1.54 |
| deliver, no-ack | 312,000/s | 670,000/s | 2.15 |
| idle memory | 135 MB | 1.9 MB | |
| growth holding 200,000 durable messages | 94 MB | 54 MB | |
| CPU time (first three jobs) | 4.7 s | 1.3 s |
Four things to know when reading this table:
- The memory row turned over. RillMQ 0.1.0 grew by 112 MB here and lost this row. The same 0.2.1 source built with a Rill from before 0.16 grows by 110 MB, and built with 0.16.1 by 53 MB, so the broker's code is not what changed. A
selectarm that answered with what it took kept a reference, which lost about 70 bytes on every frame read. A publish is three frames, so that is about 42 MB of the 57 MB drop. The README still has the old figures. - A queue bigger than memory is still RabbitMQ's ground. RabbitMQ can write a durable message down and let go of it. RillMQ keeps every message it holds in memory and uses the journal only to come back from. That is a deliberate choice, and winning this row with 64-byte messages does not change it.
- The new writers cost nothing visible. Deliver and acknowledge was 269,000/s on 0.1.0 and is 266,000/s now, and no-ack delivery rose from 453,000/s to 670,000/s. I did not separate how much of that comes from the broker and how much from the compiler.
- This is not everything RabbitMQ can do. One machine, one connection, one queue, 64 bytes. RabbitMQ could use all ten cores, RillMQ ran on one worker. The transient publish row is also a loose measurement, since the test polls the queue's count every 50 ms in a run of about 250 ms. Quorum queues, streams, plugins and years in production are not in this table.
The program is a single 602 KB file, and its compressed package is 244 KB. OpenSSL is its only dependency. The source now needs Rill 0.16.1: built with the older compiler, 0.2.1 passed the build but managed only 19,000 confirmed publishes a second.
What measuring found
The most instructive fix was in durable publish. That row used to be 329,000, and the cause looked like the disk. But 100,000 confirmed publishes took 251 ms with a journal and 231 ms without one. It was not the disk. Every confirm was getting its own Basic.Ack frame and its own write. Basic.Ack already has a multiple bit meaning "and everything before this one". Sending one frame when the session runs out of work took the time from 251 ms to 126 ms.
Then there is the afternoon the broker seemed to lose messages. The cluster checks started failing and the hunt was for a lost wake-up in the runtime. The real cause was a stale broker still running with another directory, and a new broker that exited with code 0 when it could not get its port. I made the same mistake while measuring for this post: my crashed script left a broker on the port, and the next run connected to it. That is why the tests ask "is the process on this port really the one I just started".
The cluster
A queue's owner is the hash of its name over the number of nodes. Nobody has to be asked, and every node works out the same answer. Whichever node a client connects to, if that node does not hold the queue, a stand-in that looks like a queue takes over.
A publish is confirmed after a majority of nodes has the same journal bytes. A leader that cannot reach a majority refuses. That is the safety argument: nobody was promised anything that is left on an isolated leader. When a node goes, a candidate whose journal is at least as long as its voters' takes the queue over with a majority of votes. The old leader finds out through a -STALE answer.
All 202 checks in the test suite passed against the official 0.2.1 package. 72 of them go through RabbitMQ's .NET client, 18 are the slow clients, and 8 kill a node. The release job also runs the shell suites on Linux and macOS, on both x86-64 and ARM, before it publishes anything.
Two weak spots still in the cluster
The six new commits are about clients, not the cluster, and these two problems are unchanged in 0.2.1. The README only measured what the cluster costs a publisher, so I looked at reading as well, with three nodes. All three ran on the same machine and the same disk, so the absolute numbers may be worse than a real cluster's. The measurement used one connection and 100,000 messages:
| publish | deliver and acknowledge | |
|---|---|---|
| one node, with a journal | 125,000/s | 279,000/s |
| 3 nodes, queue on the connected node | 26,000/s | about 100,000/s |
| 3 nodes, queue on another node | 26,000/s | 4,200-10,000/s |
Publishing falling to about a fifth is the expected cost of replication. But there are two problems:
- Reading a deep queue on another node is very slow. With a backlog of 50,000 messages the rate is about 28,000-32,000/s. With 100,000 it is 4,200-10,000/s, so doubling the backlog cuts the rate at least threefold. No messages are lost, every one arrived in every run measured. For now, connecting consumers to the node that owns the queue avoids this.
- Cluster nodes do not give memory back. Node RSS does not come down even after queues drain: after about 1.2 million messages one node was at 1.8 GB with empty queues. Since the memory limit looks at RSS, once a node passes the default 1 GB the queues it owns start refusing publishes with
-ERR broker is holding all it was told it could. In that state the first 513 messages were accepted and the rest refused. The refusal itself is correct behaviour, and nothing disappears silently. The problem is an empty cluster node that believes it is full. There is one lead now: when a run read through another node, two nodes each kept about 230 MB, and when the queue was on the connected node, the other two kept about 60 MB and the connected node nothing. I have not found the cause, and the leak Rill 0.16 fixed was not it. A single node also grows by 4-8 MB per new queue, nothing like a cluster.
So RillMQ's cluster should be treated as experimental for now. I saw no message loss in these measurements, but the cluster is not yet ready to stay under load for long.
What else is missing
An election is one round, and only the rule that the lowest-numbered live node stands keeps two nodes from standing at once. That is a rule, not a proof. Nodes cannot join or leave a running cluster, and links between nodes have no TLS. Rights are per host, not per queue name. Connection.Blocked asks a publisher to stop but does not force it. All of this is listed at the end of the README.
Trying it
macOS and Linux packages are in the v0.2.1 release. It needs OpenSSL 3, and on macOS brew install openssl@3 is enough.
$ ./rillmq 6789 dir=data amqp=5672 manage=15672
Then point the RabbitMQ client you already have at port 5672. The management page is at http://localhost:15672/. It shows each queue's rates in and out, its trend over the last minute, and how long the oldest message has been waiting.
Comments
Be the first to comment.
Sign in with your Google account to comment. Your name and comment are public. Your email and IP address are kept to deal with abuse, but never shown.