Pick the fix
Events for one order must be processed in sequence. How do you produce them?
Hint 1 of 1
Order holds within what?
Choose an answer, then open Show answer below to check it.
Run on Kafka 4
Text version
P0: offsets 0 A, 1 A, 2 C, 3 A, 4 B; P1: offsets 0 B, 1 D, 2 D, 3 B; P2: offsets 0 C, 1 E, 2 E, 3 F, 4 C.
Start: C1 and C2. C1: P0, P1; C2: P2.
- C3 joins (consumer C3 subscribes): C1: P0; C2: P1; C3: P2. Rebalance: one partition each.
- C4 joins (consumer C4 subscribes): C1: P0; C2: P1; C3: P2; C4: idle. C4 is idle: 3 partitions, 4 consumers.
- C2 leaves (C2 closes): C1: P0; C3: P2; C4: P1. P1 moves to C4, which resumes at the committed offset.
Show answer
Answer: A Use the order id as the message key
Correct. All of that order's events go to one partition, which keeps order.
Kafka only guarantees order within a partition, and the key's hash picks the partition. Keying by order id sends all of one order's events to the same partition, in order.
Why not the others?
- B Use a random key. Spreads one order's events across partitions.
- C Use one partition for the whole topic. Orders are kept, but parallelism is gone.
- D Add a timestamp and sort in the consumer. Consumers on different partitions still race.