Scrivio Interview library
DesignAdvanced6 min read · updated 13 November 2026

A logistics platform publishes shipment events keyed by shipment id on 64 partitions, about 30,000 events per second. Invoicing now needs all events for a customer in order, a warehouse dashboard needs events per warehouse, and both teams ask the shipment team to change the key.

Design how different consumers get different ordering keys, and say what ordering each one can really rely on.

We keep the source topic keyed by shipment id, the entity the producer owns, because changing it for one consumer breaks the others and moves every key. Teams that need another order derive re-keyed topics with a stream processor, shipments-by-customer and shipments-by-warehouse, each owned by the team that needs it; in Kafka Streams, selectKey followed by to() writes the new topic. After re-keying, a customer's events are ordered by when the re-keying task appended them, and events of one shipment stay in order, but different shipments can interleave differently from their original times. True customer-wide order needs a customer-level sequence at the source, a reorder buffer, or a customer-keyed source.

Why this question is asked

When several teams consume one topic, sooner or later each wants it keyed its own way. Changing the source key for one of them is the obvious request and usually the wrong answer. Interviewers ask this design question to see whether we can give each consumer what it needs, and, just as important, be precise about what ordering each one actually gets. In this answer, we will see why the source key stays, how to derive re-keyed topics with Kafka Streams, what ordering survives re-keying and what does not, how to get true customer-wide order if it is really required, and what all of this costs.

1Why the Source Key Stays on the Shipment

The shipment team produces events about shipments: created, picked, loaded, delivered. The natural key is the shipment id, because the most important ordering guarantee is that one shipment's events are read in sequence. Every current consumer relies on that.

If we changed the key to customer id for invoicing, the warehouse dashboard would still be unhappy, and every key would move to a different partition, so consumers would see a period where old and new records for the same shipment sit on different partitions. Large customers would also create hot partitions in a topic carrying 30,000 events a second. The producer should key by what it owns, and consumers that need another view should derive it.

2Deriving Re-Keyed Topics With Kafka Streams

A stream processor reads the source topic and writes each event to a new topic with a different key. In Kafka Streams, selectKey replaces the key, and to() writes the result. Each derived topic is owned by the team that needs it, which also owns the small application that produces it.

Java
StreamsBuilder builder = new StreamsBuilder();
KStream<String, ShipmentEvent> shipments = builder.stream("shipments"); // key: shipmentId
shipments.selectKey((shipmentId, e) -> e.getCustomerId()) // key: customerId
.to("shipments-by-customer"); // owned by invoicing
shipments.selectKey((shipmentId, e) -> e.getWarehouseId()) // key: warehouseId
.to("shipments-by-warehouse"); // owned by warehouse team, if needed
// If we aggregate after a key change, Streams inserts a repartition topic itself:
shipments.selectKey((id, e) -> e.getCustomerId())
.groupByKey() // -> internal ...-repartition topic
.count(); // shipments per customer
Step throughSource topic and derived views
  1. shipments (key shipmentId, 64 partitions)owned by shipment team; per-shipment order
  2. Re-keying apps (Kafka Streams)selectKey + to(), one per consuming team
  3. shipments-by-customerowned by invoicing; order = append order of re-keying
  4. Warehouse dashboardreads source directly; aggregates, any order is fine

4 layers, from the top down. Step through them one at a time.

Notice the last example. When a key change is followed by an aggregation or a join, Kafka Streams writes the records through an internal repartition topic automatically, so that all records with the new key reach the same task. With a plain to() we write the re-keyed topic ourselves, and that topic is effectively the repartition step.

3What Ordering Each Consumer Can Really Rely On

This is where precision matters. The re-keying application has many tasks, one per source partition. Shipment S1 for customer C9 lives on source partition 3, shipment S2 for the same customer on partition 17, processed by different tasks, perhaps on different machines. Each task appends to shipments-by-customer as it goes. So events for C9 on the derived topic are in the order the re-keying tasks appended them, which depends on processing speed and lag of each task, not on when the events originally happened.

What does survive is per-shipment order. All events of S1 come from one source partition, are processed by one task in order, and go to one derived partition, the one for C9, in that same order. So invoicing can rely on each shipment's events being in sequence, and on all of a customer's events arriving at one consumer, but not on events of different shipments being in their original time order.

Ordering after re-keying
Pair of eventsOrder preserved?
Two events of the same shipmentYes
Two shipments of one customer, same source partitionYes, in source order
Two shipments of one customer, different source partitionsNo: depends on task timing
Events of different customersNo order (different partitions)

4If Invoicing Needs True Customer-Wide Order

First, it is worth asking whether invoicing really needs it. Most invoicing groups charges per customer per period and is happy with each shipment's events in order. If it truly needs a total order across shipments, there are three options. The source can assign a customer-level sequence number to each event, so invoicing can detect and fix ordering by sequence. Invoicing can run a reorder buffer: hold events per customer for a bounded wait, say 30 seconds, and release them sorted by event time, accepting that a later straggler beyond the wait is handled as a correction. Or the source can be keyed by customer, which gives a true total order but accepts skew from very large customers and breaks per-shipment parallelism. Each option costs something, so we pick it only if the requirement is real.

5The Costs of Derived Topics

Every derived topic stores another copy of the data, at 30,000 events a second a significant amount, adds a processing hop of latency, usually tens to hundreds of milliseconds, and adds a copy of the data that must be secured and governed. So we derive only where a consumer needs a different order. The warehouse dashboard probably does not: it counts and aggregates events per warehouse, and counts are the same in any order. It can read the source topic and aggregate by warehouse, and if it uses Kafka Streams, the aggregation's internal repartition topic handles the grouping without a published topic.

6Follow-up questions

Who should own the re-keying application?

The consuming team that needs the new order. It understands the requirement, it bears the cost, and it can change the derived topic without negotiating with the shipment team. The shipment team owns only the source contract, which keeps its responsibilities small.

Does the derived topic need the same partition count as the source?

No. It should be sized for its own consumer's parallelism and throughput. Only if it will be joined with another topic keyed by customer do the two need the same partition count and key format to be co-partitioned.

7Conclusion

In this answer, we kept the shipments topic keyed by shipment id, because the producer should key by the entity it owns and changing it would break other consumers. Teams that need another order derive re-keyed topics with Kafka Streams selectKey and to(), owned by them, while aggregations rely on automatic repartition topics. After re-keying, per-shipment order survives but events of different shipments can interleave differently from their original times. True customer-wide order needs a source sequence, a bounded reorder buffer or a customer-keyed source. Derived topics cost storage, latency and governance, so we create them only where needed.

What to say in the interviewAbout 90 seconds aloud

Open with

I would keep the source topic keyed by shipment id, because the producer should key by the entity it owns; changing it for one consumer breaks the others and moves every key.

Then, if they want more

Instead, I would derive re-keyed topics with a stream processor, shipments-by-customer for invoicing and, only if needed, shipments-by-warehouse, each owned by the team that needs it. In Kafka Streams that is selectKey followed by to(), and if a key change is followed by an aggregation, Streams writes through an internal repartition topic automatically. Then I would be precise about ordering.

After re-keying, a customer's events are in the order the re-keying tasks appended them. Events of one shipment stay in order because they come from one source partition and one task. But events from different shipments, on different source partitions, can interleave differently from their original times.

If invoicing truly needs customer-wide order across shipments, it needs a customer-level sequence assigned at the source, or a reorder buffer by event time with a bounded wait, or the source keyed by customer, which accepts skew from large customers.

On cost, each derived topic adds storage, a processing hop and another copy to secure, so I would derive only where a consumer needs a different order, and let the dashboard, which only aggregates, read the source and tolerate any order.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design how different consumers get different ordering keys, and say what ordering each...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/47ab2bfff2/