RabbitMQ / Basic: protocol resources, routing and ownership / Lesson 7
Message contracts and delivery metadata
Separate business message identity and payload from broker delivery information.
On this page
Lesson goals
- Define a versioned payload and stable occurrence identity.
- Distinguish publisher properties from delivery-specific metadata.
Define the business message
A message contract should state its producer, business meaning, occurrence identity, schema version and consumer expectations. An accepted order and a later shipped order are distinct events; a retry of the same occurrence should retain its identity. The broker transports bytes and properties but does not automatically validate business meaning. Agree on unknown fields, unsupported versions and sensitive data before publishing.
Inspect a synthetic message
The first fixture represents a JSON message with an application-assigned identifier and a versioned order payload. It is a readable envelope, not a wire-format frame or a live publish. The content type communicates a decoding convention, but consumers still need to validate the actual payload. Keep credentials and unnecessary personal information out of properties, headers and diagnostic examples.
{"message_id":"evt-7","content_type":"application/json","body":{"version":1,"order_id":"O7","status":"accepted"}}
The identifier represents one business occurrence; the content type describes the expected payload format.
Recognize a redelivery
The second fixture shows the same occurrence with a redelivery indicator for illustration. Delivery metadata describes a delivery attempt, while the message identifier is an application contract for the business occurrence. A consumer should not generate a new occurrence identity merely because a message is delivered again. The redelivery indicator alone is not a durable deduplication store or proof that the destination effect already exists.
{"message_id":"evt-7","content_type":"application/json","body":{"version":1,"order_id":"O7","status":"accepted"},"redelivered":true}
The same message identity is retained; the added field represents delivery context in this teaching envelope.
Handle invalid and unknown input
Define what happens when content type, encoding, version or required fields are missing or unsupported. Preserve sanitized identity and failure context in the approved failure path, and avoid an immediate unbounded retry for a permanently invalid payload. A rejected message can represent unfinished business work, so assign an owner and a reconciliation process rather than simply discarding it.
Test the contract safely
In an isolated virtual host and disposable destination, publish a synthetic valid message, an unsupported version and a repeated occurrence identity. Verify routing, validation, acknowledgment and the destinationโs documented duplicate behavior separately. Inspect what happens when a worker stops after its effect but before acknowledgment. Record actual observations; the JSON fixtures alone do not demonstrate broker delivery or durable processing.
Common errors and fixes
A retry receives a new business identifier.
- Why it happens
- Delivery attempt and occurrence identity were conflated.
- How to fix it
- Retain the original occurrence identifier on a retry.
- Prevent it next time
- Define identity creation and persistence in the contract.
A content type is accepted without checking the payload.
- Why it happens
- Metadata was treated as schema validation.
- How to fix it
- Validate the decoded version and required fields.
- Prevent it next time
- Test invalid and unsupported examples.
The redelivery flag is treated as proof an effect exists.
- Why it happens
- Delivery metadata was confused with durable destination state.
- How to fix it
- Inspect the destination idempotency record.
- Prevent it next time
- Test failure between effect and acknowledgment.
2 practice exercises
1. Do the two fixtures represent different business events?
Show worked solution
No. They retain the same message_id and payload; the second illustrates another delivery of the same occurrence. A genuinely later order event needs its own occurrence identity.
2. Does application/json guarantee that the body meets the order contract?
Show worked solution
No. It states an expected format. Decode and validate the version and required fields, then follow the documented invalid-message path if validation fails.
Key takeaways
- Keep occurrence identity stable across retries.
- Validate payload contracts independently of delivery metadata.