Message Structure in Messaging Systems

In an asynchronous messaging system, a message is the self-contained envelope that carries data from a sender to one or more receivers through a broker. Every message splits cleanly into two parts: a header (routing, tracing, and delivery metadata) and a payload (the business data itself).

Anatomy of a Message

Separating metadata from business data lets brokers, gateways, and observability tools route, deduplicate, expire, and trace messages without ever needing to parse the inner domain payload.

Message envelope structure: a Header containing messageId, correlationId, groupId, sequence and expiry, above a Payload that carries a Command, Event, or Document Message envelope structure: a Header containing messageId, correlationId, groupId, sequence and expiry, above a Payload that carries a Command, Event, or Document
A message pairs transport metadata in the header with domain data in the payload · Made with Akriti

Message Header (Metadata)

The header holds transport and operational metadata that is related to delivering or tracing the message rather than the domain entity itself. Common header fields include:

Header Field Purpose Practical Example
Message ID Globally unique identifier for this exact message; used by consumers as an idempotency key to ignore duplicates "msg_01J9X7A2K9"
Correlation ID (Trace ID) Ties together all messages and HTTP calls triggered by one originating user request for distributed tracing "trace_8f4c12b0"
Causation ID Points to the immediate parent messageId that caused this message to be emitted "msg_01J9X79ZZ1"
Message Group ID (Partition Key) Groups related messages (such as all updates for order_42) so the broker routes them to the same partition or FIFO queue "order_42"
Sequence Number Monotonic counter within a group used to preserve order or detect missed/out-of-order updates 3
Creation Time Timestamp when the sender produced the message; used to measure end-to-end consumer lag "2026-11-25T16:10:00Z"
Expiry Time (TTL) Deadline after which an unprocessed message is stale and should be discarded or dead-lettered "2026-11-25T16:15:00Z"
Schema / Content Type Format and version of the payload so consumers deserialize safely "application/json; v=2"

Message Payload (Body)

The payload is the actual domain data the sender wants to transfer. Based on the sender’s intent, payloads fall into three categories:

  1. Command (Action) Payload: Asks a specific receiver to execute an operation — for example, CREATE_SHIPMENT or GET_ORDER.
  2. Event Payload: Announces a state change that already happened in the sender — for example, ORDER_CREATED.
  3. Document Payload: Carries a structured representation of an entity or record — for example, a full order row in a CDC stream or the body of a reply message.

Example JSON Message Envelope

{
  "header": {
    "messageId": "msg_01J9X7A2K9",
    "correlationId": "trace_8f4c12b0",
    "messageGroupId": "order_42",
    "sequenceNumber": 1,
    "messageType": "ORDER_CREATED",
    "schemaVersion": "1.0",
    "createdAt": "2026-11-25T16:10:00Z",
    "expiresAt": "2026-11-25T17:10:00Z"
  },
  "payload": {
    "orderId": "order_42",
    "customerId": "cust_91",
    "items": [
      { "sku": "BOOK-01", "qty": 2 }
    ],
    "totalAmount": 49.90
  }
}

Serialization Formats

Messages are serialized into bytes before crossing the network. Common choices include:

  • JSON: Human-readable and easy to debug, ideal when payload size and throughput are moderate.
  • Protocol Buffers (Protobuf) or Apache Avro: Compact binary formats with explicit schemas and backward/forward compatibility rules, widely used on high-throughput Kafka and gRPC pipelines.

Best Practices for Designing Messages

  • Always include messageId and correlationId: Without messageId, consumers cannot safely handle at-least-once retries; without correlationId, debugging an asynchronous failure across five services becomes guesswork.
  • Keep messages small: Avoid embedding large files or multi-megabyte blobs directly in a broker message. Store the blob in object storage (like S3) and put its URL or key in the payload (the Claim Check pattern).
  • Evolve schemas additively: Add optional fields rather than renaming or deleting existing fields so older consumers keep working during rolling deploys.

FAQs

Why put metadata in the header instead of inside the payload? Brokers, routers, and tracing agents can inspect headers (such as messageGroupId, expiresAt, or correlationId) without deserializing the domain payload. It also keeps domain models clean and lets you standardize observability across every service.
What is the difference between Message ID and Correlation ID? Every single message gets its own unique Message ID (used for deduplication and idempotency). A Correlation ID stays the same across an entire chain of messages spawned by one original user request so you can follow the full workflow in logs and traces.
Why does Message Group ID matter for ordering? Distributed brokers scale by splitting traffic across multiple partitions or consumers. Setting a consistent Message Group ID (or partition key, like orderId) guarantees that all messages for the same entity land on the same partition and are processed in order.