# Solution 02 — Keys and partitions

*Attempt the exercise first — this file is the post-mortem.*

## What the verified run showed

Step 3 (CLI placement):

```
Partition:0 : customer-alice : order-1
Partition:0 : customer-alice : order-3
Partition:0 : customer-carol : order-4     ← collision with alice
Partition:0 : customer-alice : order-5
Partition:2 : customer-bob   : order-2
```

Step 4 (unkeyed burst, 12 records):

```
all 12 on Partition:2   (one sticky partition — see below)
```

The Java lesson (`scripts/run.sh lab.ProducerLesson`):

```
predicting with murmur2(key) % 6, the client's own math:
  amy → 0 · bill → 3 · carol → 2 · dmitri → 3
amy → partition 0 @ offset 147   bill → partition 3 @ offset 353 …
↳ Same key → same partition, every round. Different keys may collide.
```

## The guarantee, stated so it cannot be misread

> Records with the same key hash to the same partition **while the
> partition count and the partitioner stay fixed**. A partition is a single
> ordered log. Therefore: per-key ordering — and nothing else. No
> cross-key, cross-partition, or cross-topic ordering exists.

Two ways to break even that narrow guarantee: change the partition count
(hashes re-map) or change the partitioner (the custom `vip-*` router in
lesson section 04 routes by rule, not hash).

## The understanding checks, answered

1. See the box above — same-key ⇒ same-partition ⇒ ordered; that's all.
2. Alice and carol colliding on Partition:0 threatens **neither's**
   ordering. A partition orders all records within it; alice's records
   stay ordered among themselves, carol's among hers. Collisions cost
   *isolation* (they share headroom), not order.
3. Prediction needs exactly two facts: the **partition count** and the
   **hash algorithm + partitioner** (`murmur2` + the default partitioner,
   computed client-side — the lesson prints both, because the *broker* is
   never consulted for placement of a keyed record).
4. The unkeyed burst on one partition becomes a problem when one partition
   is also one consumer's entire parallelism slot: 12 records is a
   curiosity, 12 GB is a skew incident. The fix is keys (even synthetic
   ones) — the same lever as any hot-key problem.

## The unkeyed surprise, named

Since Kafka 3.3, the default partitioner assigns an unkeyed batch to a
single **sticky** partition and rotates it only occasionally (KIP-794) —
better throughput, and a small burst that lands entirely on one partition
is *expected*. Verified here on 3.9.0 with a 12-record pipe: all on
Partition:2.

## What this buys you in production

"Ordering bugs" filed against Kafka are almost always one of: no key
(round-robin/sticky spread), key changed mid-stream, or partition count
changed (hash re-mapped). You can now tell which from a
`--property print.partition=true` read in ten seconds.
