Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท AMQP and Architecture

Message Properties and Anatomy

Learn what is inside a RabbitMQ message: body, routing key, properties such as delivery_mode, correlation_id, headers and expiration, and how to serialize payloads.

A RabbitMQ message is more than a string. It is a body (bytes) plus a set of properties that describe it, plus delivery details added by the broker. Knowing what each part is for lets you build clean, debuggable systems and avoid putting everything in the body.

Code that uses pika.BasicProperties is read-only (it needs a real broker). The runnable blocks use only the Python standard library to build and inspect message-like structures.

The parts of a message

flowchart TB
    M["Message"] --> R["Routing key (set on publish)"]
    M --> P["Properties (metadata)"]
    M --> B["Body (opaque bytes)"]
    P --> P1["content_type, content_encoding"]
    P --> P2["delivery_mode, priority, expiration"]
    P --> P3["correlation_id, reply_to, message_id"]
    P --> P4["headers (custom key/value table)"]

The routing key travels with each publish; properties and body form the message itself.

The broker never looks inside the body. It uses the routing key and sometimes the headers to route, and it uses a few properties such as delivery_mode, expiration and priority to decide how to store and deliver.

The standard properties

AMQP 0-9-1 defines fourteen basic properties. These are the ones you will use:

Property Meaning
content_type MIME type of the body, for example application/json
content_encoding Encoding such as utf-8 or gzip
headers A table of custom key/value pairs
delivery_mode 1 = transient, 2 = persistent
priority 0 to 9 for priority queues
correlation_id Links a reply to a request (RPC)
reply_to Name of the queue to send the reply to
expiration Per-message TTL in milliseconds, as a string
message_id Your unique id for the message (useful for idempotency)
timestamp Creation time, seconds since the epoch
type Application-defined message type name, such as order.placed
user_id Must match the connected user if set; validated by the broker
app_id Name of the producing application

All are optional. Some of them (message_id, type, app_id) are only labels: RabbitMQ stores and forwards them without acting on them, but they make logs, tracing and idempotent processing much easier.

Setting properties with pika

Python
# real pika code, read-only
import json, time, uuid
import pika

properties = pika.BasicProperties(
    content_type="application/json",
    delivery_mode=pika.DeliveryMode.Persistent,   # 2
    message_id=str(uuid.uuid4()),
    timestamp=int(time.time()),
    type="order.placed",
    app_id="shop-web",
    headers={"tenant": "acme", "schema_version": 3},
    expiration="60000",                            # string, milliseconds
)

channel.basic_publish(
    exchange="orders",
    routing_key="order.placed",
    body=json.dumps({"order_id": 17, "total": 42.5}).encode("utf-8"),
    properties=properties,
)

Two details trip people up. expiration must be a string of digits (a quirk of the protocol), and delivery_mode=2 only makes the message persistent: to survive a restart it must also sit in a durable queue (see Queues in Depth).

Building an envelope you can inspect

Here is a stand-in that creates a message object, serializes the body as JSON, and validates the properties the same way a careful producer would. It only uses the standard library.

Python Build and inspect a message

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that message_id is derived from the content, so a retry of the same order creates the same id. That makes deduplication easy, as in Messaging Concepts and Patterns.

Choosing a body format

RabbitMQ carries bytes; you pick the format:

  • JSON: human-readable, universal, a little bulky. A fine default; set content_type so consumers know.
  • Protocol Buffers, Avro, MessagePack: compact and schema-friendly for high volume.
  • Plain text: fine for logs.
  • Compressed: if you gzip, say so in content_encoding.

Keep messages small. Brokers handle many small messages much better than a few huge ones. Put large files in object storage and send a reference (a URL or key) in the message. The protocol allows very large bodies (the maximum message size is configurable and large by default), but big messages stress memory, replication and consumers.

Headers

The headers table holds arbitrary key/value pairs (strings, numbers, booleans, nested tables, byte arrays, timestamps). Uses:

  • Tracing: trace_id, traceparent.
  • Schema or tenant: schema_version, tenant.
  • Routing on a headers exchange (Headers and the Default Exchange).
  • Retry counters, which dead-lettering also adds on its own in an x-death header.

Do not bury business data in headers; keep it in the body so it is versioned with the payload.

Information the broker adds

When a message is delivered, the consumer also gets delivery fields (not properties you set):

  • delivery_tag: a per-channel counter used to acknowledge.
  • redelivered: true if the message was delivered before.
  • exchange and routing_key: where it came from.

The stand-in below shows how a redelivered flag lets a consumer be extra careful.

Python Using the redelivered flag (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What value of delivery_mode marks a message persistent?

    delivery_mode 2 means persistent and 1 means transient. Persistence also needs a durable queue.
  2. 2. In what type must the expiration property be set?

    The per-message TTL is a string holding a number of milliseconds, a quirk of the protocol.
  3. 3. What does RabbitMQ do with the message body?

    The body is never inspected. You choose the format and tell consumers with content_type.
  4. 4. Which property links a reply to the request that caused it?

    correlation_id is copied into the reply so the requester can match answers to questions.

Summary

  • A message is routing key plus properties plus an opaque body.
  • Important properties: content_type, delivery_mode, message_id, correlation_id, reply_to, expiration, headers.
  • expiration is a string of milliseconds; persistence needs delivery_mode=2 and a durable queue.
  • Keep bodies small, tell consumers the format, and use headers for metadata only.
  • Deliveries add delivery_tag and redelivered.

Next: Inside the Broker: Erlang, Nodes and Storage