📡 Producer & Consumer

Producer & Consumer: Producer — Writing Messages A Kafka Producer works like a package routing system at a courier depot: the sender (producer) attaches a destination label (mess

Producer — Writing Messages

A Kafka Producer works like a package routing system at a courier depot: the sender (producer) attaches a destination label (message key), drops the parcel at the intake window (broker), and the depot's routing algorithm decides which conveyor belt (partition) it goes to — the same label always lands on the same belt, ensuring all parcels for one customer arrive in order. But why does the producer even have a key at all — can't you just publish to a topic and be done? Because Kafka's ordering guarantee is per-partition, not per-topic: without a key, events round-robin across partitions and can arrive out of sequence. In Java terms, the message key is structurally identical to the hash key in a HashMap: `hash(key) % numPartitions` determines the bucket, and the same key always maps to the same bucket. For QA, this is where silent bugs are born: if your test publishes an order-created event and a payment-captured event for the same order ID without the same key, they may land on different partitions and be consumed in reverse order — making the payment arrive before the order exists, a race condition that only reproduces under load and always looks like a timing fluke in CI logs.

Java Producer — Send a message

Micro Lab: Code practice

Replace the TODO line with the critical line from the expected solution. This is not a real runtime; the goal is to reinforce writing the correct structure in a controlled way.

Key → Partition Routing

The message KEY determines which partition it goes to: hash(key) % numPartitions. Same key always goes to same partition → guarantees ordering for related messages. Example: key="user-123" — all orders for user 123 go to the same partition, so they're processed in order. No key → round-robin across partitions.

What Happens From producer.send() to the Callback?

A ProducerRecord is built…

A `ProducerRecord` is built: topic="orders", key="user-123", value=JSON — the key is the single field that will decide which partition the message goes to.

producer.send() runs ASYNCHRONOUSLY…

`producer.send()` runs ASYNCHRONOUSLY — the main thread continues immediately, the send is queued in a background buffer.

Because of acks=all…

Because of `acks=all`, the broker does NOT confirm the message until ALL in-sync replicas — not just the leader — have written it — this is the slowest but safest option.