Video in progress
What parts does a Kafka record consist of?
Each record has a key, value, headers and timestamp; on the broker it is placed by partition and offset. Key and value are byte arrays, and headers are key-value pairs for extra metadata.
📖 Answer
ProducerRecord<String, String> record =
new ProducerRecord<>("orders", null, "order-17", payloadJson);
record.headers().add("traceId", traceId.getBytes(StandardCharsets.UTF_8));
record.headers().add("eventType", "OrderCreated".getBytes(StandardCharsets.UTF_8));
producer.send(record);
When are headers useful? Trace ID, event type, schema version, where the message came from. You can route or filter a message without opening the payload. Spring Kafka uses headers too: for example, it writes the JSON type information into the __TypeId__ header.
The timestamp comes in two kinds: CreateTime (the time set by the producer, the default) and LogAppendTime (the time the broker wrote it). You choose with the topic setting message.timestamp.type.
A record with a null value has a meaning too: in a compacted topic it's a "tombstone", which means "delete this key".
Sign up to mark studied questions and save your progress.