Do not send files through your message bus
How large messages drive up the cost of Azure Service Bus and other brokers, and how the claim check pattern fixes it.
- BITServices team
- #azure
- #architecture
Message brokers are built to move small messages fast. When files and large documents start travelling through them, the bill grows with every byte, and often nobody notices until it is one of the largest lines on the invoice. This post uses Azure Service Bus for the numbers, but the same applies to SQS, SNS, Pub/Sub, Kafka and RabbitMQ.
How Service Bus charges for size
On the Standard tier you pay per operation. A send, a receive and a delete of a message up to 64 KB is one billable operation each. A larger message counts as one operation for every 64 KB. A 1 MB file sent once and received once is 32 operations, not 2.
Prices from the Azure retail price list for East US at the time of writing:
| Standard tier | Price per million operations |
|---|---|
| first 13M per month | included |
| 13M to 100M | $0.80 |
| 100M to 2,500M | $0.50 |
| above 2,500M | $0.20 |
On the Premium tier you pay for capacity instead, $0.9275 per messaging unit per hour, about $677 a month. Large messages fill that capacity fast, so you buy more units.
And the broker is only part of the cost. Every file also crosses the network to the broker and back out to every consumer, and every consumer spends CPU and memory to read it, even when it only needs a few fields.
How it happens
It usually starts with a small payload that grows. A service publishes an event with the full object because it is convenient. Later the object gets an attachment, a PDF, a scan or a blob of device data. Consumers receive the whole thing, and some of them only pass it on to the next service. One file can cross the broker three or four times on its way through the system.
The claim check pattern
The fix has a name: claim check. The data goes into storage once, and the message only carries a reference to it.
- The producer writes the file to object storage, for example Azure Blob Storage or S3.
- It writes the metadata, such as owner, type and size, to the database.
- It publishes a message with the ID and nothing else.
- A consumer that needs the file reads it from storage by ID. A consumer that only needs the metadata reads that, and never touches the file.
Messages become a few hundred bytes. Each one is one billable operation, the broker needs far less capacity, and the file crosses the network only when someone actually needs it.
Details that matter
- Do not lose messages. Write the metadata and the outgoing event in the same database transaction, and publish the event after the transaction commits. This is the transactional outbox. If the service fails between the two steps, the event is still in the outbox and goes out later.
- Make consumers idempotent. Brokers deliver at least once. Reading by ID makes this easy: processing the same ID twice should do no harm.
- Clean up storage. Files that nobody needs after processing should get a lifecycle rule that moves or deletes them.
- Migrate one message type at a time. Run the old and new formats side by side until every consumer reads the new one, then remove the old one.
What it saves
In one of our projects binaries travelled through Azure Service Bus between services. After the change binaries went to Blob Storage, metadata to SQL Server and only IDs to the bus. The client saves more than $100k a month, and the application works the same way.
If you want a quick check of your own setup, look at the average message size per queue or topic. Anything above a few kilobytes is worth a closer look.