Course outline

Apache Kafka / Basic: records, partitions and reader progress / Lesson 7

Record headers, correlation and metadata contracts

Design a bounded header contract that distinguishes business identity from request tracing and handles repeated header keys.

About 4 min readContent reviewed
On this page

Lesson goals

  • Distinguish event identity, record key and correlation metadata.
  • Define encoding, duplicate handling and safe logging for headers.

Separate the three identities

A parcel event can carry a business occurrence identifier in its value, a parcel key for partitioning and a correlation identifier linking a request to several operations. These identifiers answer different questions. A retry of the same business fact should retain the occurrence identity even if a new transport attempt has its own tracing context. Define ownership and lifetime for each field before using it to reconcile records. Neither a correlation identifier nor a physical offset automatically proves that an external business effect happened exactly once.

Inspect a synthetic envelope

The following JSON is a readable teaching representation, not a Kafka wire-format record or an executable client example. The value identifies the occurrence, while the key groups parcel history under the selected partitioning scheme. Headers describe request context and content type. Header values are bytes in the Java API, so a real contract must specify their encoding and behavior when a header is absent. Never infer that the broker validates the meaning of these application fields.

json Illustrative event and header fields
{"key":"P17","value":{"event_id":"evt-101","type":"parcel-created"},"headers":[["correlation-id","req-7"],["content-type","application/json"]]}

The representation separates the parcel key, event identity and request correlation without contacting a broker.

Choose a duplicate-key rule

Kafka headers can contain multiple entries with the same key. The Java API exposes both all matching entries and the last matching entry; the application must choose which behavior its contract permits. The fixture below declares a strict reject policy for repeated correlation identifiers. A consumer implementing that policy should reject or quarantine the record with sanitized evidence rather than silently choosing an attacker-controlled or accidental value. Other applications can choose another documented rule.

json A duplicate header requiring a policy
{"headers":[["correlation-id","req-7"],["correlation-id","req-8"]],"policy":"reject-duplicate"}

Two entries share a key; the declared policy rejects this input rather than assuming the collection is a map.

Bound what metadata reveals

Headers may travel through producers, consumers, logs and operational tooling. Use synthetic opaque identifiers instead of names, addresses, credentials or complete payloads. Define an allowlist for diagnostic logging, access controls and retention independently of the topic value. A correlation identifier can still link activities, so treat it according to the organization’s privacy rules. If a header is required for processing, specify an explicit failure path for missing, invalidly encoded or oversized data.

Review an end-to-end trace

In a disposable test, prepare one event and a retry with the same event identity. Compare the parcel key, header values and destination effect separately; preserve the original coordinate and any later coordinate as evidence. Add a second legitimate event for the same parcel to ensure deduplication does not discard it. This validates the application contract only within the tested services and cannot establish guarantees for an unrelated external destination or production workload.

Common errors and fixes

A correlation ID is used as the sole business deduplication key.

Why it happens
Request tracing was confused with occurrence identity.
How to fix it
Persist and use the business event identity for the defined operation.
Prevent it next time
Document the purpose and lifetime of each identifier.

A consumer silently accepts conflicting repeated headers.

Why it happens
The application treated a multivalue collection as a unique-key map.
How to fix it
Apply the documented duplicate policy and retain sanitized failure evidence.
Prevent it next time
Test absent, single and repeated values.

A diagnostic header exposes a customer address.

Why it happens
Operational metadata was treated as private by default.
How to fix it
Remove the address and use an opaque identifier under the logging policy.
Prevent it next time
Review header allowlists and retention before publication.

2 practice exercises

1. Two deliveries have event_id evt-101 and correlation IDs req-7 and req-8. What should be compared?

Show worked solution

Treat them as candidates for the same occurrence, then compare their payloads and destination effects under the business contract. Different correlation IDs can describe separate attempts; they do not alone make two business events. Investigate a conflicting payload before deduplicating.

2. A record has two correlation-id headers. What should the example policy do?

Show worked solution

Reject or quarantine it with sanitized evidence because the example explicitly requires a single value. Do not assume a generic Kafka rule selects the correct one; another application may define a different compatible policy.

Key takeaways

  • Keep business identity, partition key and correlation context distinct.
  • Specify byte encoding, duplicate-key handling and metadata privacy.

Official references