Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. Topic vs Partition and ordering guarantees

Kafka · Core Concepts

Topic vs Partition and ordering guarantees

Easykafka-04
topicpartitionorderingpartition key

Question

What is the difference between a topic and a partition? What ordering does Kafka guarantee?

Solution

Topic

A logical category of events: orders, payments, user_events.

Partition

A physical, append-only log that is a shard of the topic. Topics have one or more partitions.

topic: orders
  partition 0: offset 0 → 1 → 2 → 3 → ...
  partition 1: offset 0 → 1 → 2 → ...
  partition 2: offset 0 → 1 → ...

Ordering guarantee (critical interview line)

Kafka guarantees order within a partition, not across the whole topic.

If message A lands in partition 0 and message B in partition 1, consumers may see B before A (or A before B). There is no global topic order.

How to get order for related events

Use a partition key so related records go to the same partition.

Example: key = order_id or customer_id.

All events for order_id=42 → same partition → processed in write order
Events for different orders → may be on different partitions → no cross-order guarantee

Trade-off

  • 1 partition: total order for the topic, but no parallel consumers in a group.
  • Many partitions: high throughput and parallelism, but only per-key / per-partition order.

Interview tip: Always say "ordering is per partition." Then explain keys as the way to keep related events ordered.

PreviousNext