Kafka · Write code
Events for one order arrive out of order
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
Consumers sometimes see 'PAID' for an order before 'CREATED'. Here's our producer, using the Java client. What's wrong with it?
Properties p = new Properties();
p.put("bootstrap.servers", BROKERS);
p.put("acks", "1");
p.put("enable.idempotence", "false");
p.put("retries", "10");
p.put("max.in.flight.requests.per.connection", "5");
p.put("key.serializer", StringSerializer.class.getName());
p.put("value.serializer", StringSerializer.class.getName());
KafkaProducer<String, String> producer = new KafkaProducer<>(p);
public void publish(OrderEvent e) {
String key = UUID.randomUUID().toString(); // a fresh id for every event
producer.send(new ProducerRecord<>("order-events", key, toJson(e)));
}
Then
Follow-ups the interviewer asks next
- How does the producer choose a partition when there's a key, and when there isn't?
- Why can retries change the order inside one partition, and how does the idempotent producer prevent it?
- What can
acks=1lose? - The payment service also publishes 'PAID' for the same order. Can Kafka order its events against the order service's events?
- What happens to per-order ordering if someone adds partitions?
The answer
What a strong answer covers
- Kafka keeps order only inside one partition. A random key per event spreads one order's events over many partitions, so consumers see them in any order. Use the order id as the key: the default partitioner hashes the key (murmur2) modulo the partition count, so one order always lands on one partition. Records without a key are spread in sticky batches.
- Retries with 5 requests in flight and idempotence off: batch 1 fails, batch 2 succeeds, batch 1 is retried and lands after it. The idempotent producer (a producer id plus a sequence number per partition) lets the broker reject repeated or out-of-order batches, so order holds with up to 5 in flight.
- These lines switch off safe defaults. In Kafka 4.x the defaults are
acks=all,enable.idempotence=true,retrieseffectively unlimited withindelivery.timeout.ms(2 minutes) andlinger.ms=5. Idempotence needsacks=all, so delete the overrides. acks=1means only the leader has the record when the send succeeds; if the leader dies before the followers copy it, it's gone.send()is asynchronous and nobody checks the result, so failures after the delivery timeout vanish. Use a callback, log and count failures, and decide what happens to the event (an outbox makes this safe).- Two services writing events for the same order can't be ordered by Kafka. Put a per-order sequence or version in the event, set by one owner, and let consumers ignore older versions.
- Adding partitions changes which partition a key maps to, so ordering breaks across the change.
The red pen
Common traps
Avoid:"Kafka keeps global order"; max.in.flight.requests.per.connection=1 as the only fix (slow, and idempotence already solves it); a random or timestamp key.
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.