How a publish travels¶
Publish writes the event with the backend's format, sends it to the exchange with the subject as
the routing key, and does not return until the broker has confirmed it.
What goes out¶
- The event is encoded by
Settings.Format. Its content type goes on the message'scontent_typeproperty and its headers into the headers table as strings. See Wire formats and message layout. - The message is persistent (
delivery_mode2), so a durable queue keeps it across a broker restart. - The
message_idproperty is the hex SHA-256 of the event'sSource, a NUL byte, and itsID. Hashing keeps it fixed-length, and it is never a raw URI in a property. The adapter uses it to match a returned message to its publish. It is not a deduplication key: the broker does not look at it. - The publish is mandatory: if no queue is bound for the routing key, the exchange returns it.
What it waits for¶
The publish channel is in confirm mode and Publish waits for the confirm. One of four things
happens:
| The broker | Publish returns |
|---|---|
| confirms, and did not return the message | nil |
| returned it as unroutable | ErrUnroutable, naming the subject |
| negatively acknowledged it | an error saying the broker refused the publish (basic.nack) |
| did not confirm before the context ended | the context's error, wrapped |
A subject no queue is bound for is refused and not dropped. An at-most-once source's transient queue is bound only while the source exists.
Refused before it is sent¶
If the event's properties would not fit in the content header frame, Publish refuses it locally. The
properties, meaning content_type, message_id and every header, travel in that frame, so the
negotiated AMQP frame size is the budget and max_message_size, which governs the body, is not. The
broker would otherwise close the channel, taking every other publish in flight with it. A frame size
of zero, the client asking for no limit, skips the check.
An event the format refuses, such as a non-UUID ID under MassTransit, fails as
messaging-rabbitmq: encoding the event.
Why a retry can deliver twice¶
RabbitMQ has no identity on the publisher's side. A publish that timed out, or whose connection was lost between the broker writing the message and the confirm arriving, is ambiguous: the caller cannot tell whether it was stored. Retrying it can store it twice, and both are delivered.
JetStream and SQS FIFO deduplicate on the event's identity and would hide this. Here nothing does, so consumers must be idempotent.
After Close¶
Publish after Close returns ErrClosed, wrapped with the subject.