
An order service publishes PaymentAuthorized, ShipmentCreated, and OrderCancelled. If those events land in different partitions, a consumer can observe cancellation before payment. If every event is forced through one partition, the sequence is safe but throughput and recovery capacity shrink. Kafka ordering is therefore a business decision expressed through partitioning, not a property that automatically covers an entire topic.
This guide explains Kafka ordering guarantees from producer append to consumer side effect: partition order, keys, retries, asynchronous handlers, hot keys, and the trade-offs behind each design. Start with Kafka partitions, then review Kafka offsets before choosing a commit boundary.
Table of Contents
Open Table of Contents
What Kafka Actually Orders
Kafka appends records to one partition in sequence. The record at offset 41 is before offset 42 in that partition, and a consumer group assigns that partition to one active consumer at a time. Kafka does not provide a total order across partitions, so offset 41 in partition 0 and offset 41 in partition 1 are unrelated positions.
There are three different claims people often collapse into “ordered”: the broker’s append order, the order a consumer returns records from poll, and the order of downstream business effects. The first is Kafka’s strongest guarantee. The second depends on a consumer preserving partition order. The third also depends on worker concurrency, retries, database transactions, and external APIs.
flowchart TD
A[Producer event] --> B{Partition key}
B -->|order-123| P0[Partition 0: offsets 40, 41, 42]
B -->|order-987| P1[Partition 1: offsets 18, 19]
P0 --> C[One consumer-group owner]
P1 --> D[Another consumer-group owner]
C --> E[Ordered reads for order-123]
D --> F[Independent ordered reads for order-987]
If a requirement says “all orders must be globally ordered,” one partition is the straightforward implementation, but it makes one consumer the throughput ceiling for that group. Most systems need order per aggregate—such as orderId, accountId, or customerId—so independent aggregates can use many partitions in parallel.
Keys Define the Ordering Boundary
The key should identify the smallest entity whose events must be serialized. A stable non-null key is hashed to a partition, so all records for that key follow the same log. Use orderId for an order lifecycle, accountId for balance changes, or customerId for a notification sequence. The key is correct only if every producer uses the same serialization and event contract.
| Requirement | Key choice | Resulting trade-off |
|---|---|---|
| Order an order lifecycle | orderId | Hot orders can concentrate on one partition |
| Preserve one account’s balance updates | accountId | One high-volume account is a serial bottleneck |
| Maximize independent analytics throughput | A balanced event key | No order between unrelated aggregates |
| Guarantee topic-wide order | No parallel key space; one partition | Throughput and failover are limited |
ProducerRecord<String, OrderEvent> record = new ProducerRecord<>(
"order-events",
event.orderId(), // Every state transition for one order uses one key.
event
);
producer.send(record, (metadata, error) -> {
if (error != null) {
// The caller must not acknowledge the business action on publish failure.
throw new IllegalStateException("Order event was not published", error);
}
metrics.recordPartition(metadata.partition());
});
Do not key by a convenient field that changes during an entity’s lifecycle. A random UUID balances load but destroys per-order ordering. A broad tenantId may preserve tenant order while turning a large tenant into a hot partition. Define the invariant first, then choose the key that encodes it.
Producer Settings That Protect Order
Producer retries introduce an ordering risk when multiple requests are in flight and an earlier request fails after a later request succeeds. Current Kafka clients use idempotence by default in compatible configurations, but production systems should set the intent explicitly and verify the effective client configuration.
# Preserve sequence numbers when a broker acknowledges a retry.
enable.idempotence=true
acks=all
# Keep enough throughput without allowing an unsafe retry pipeline.
retries=2147483647
max.in.flight.requests.per.connection=5
Idempotence prevents duplicate appends from producer retries within the producer session; it does not make an external side effect exactly once. acks=all waits for the configured in-sync replicas, trading publish latency for stronger durability. If a producer cannot reach Kafka, the application still needs an explicit policy for retry, timeout, and user acknowledgement.
Changing a topic’s partition count is another ordering change. Existing records stay in their old partitions while future keys may hash to new partitions. A key’s historical and future sequence can therefore span partitions after the change. For strict event order across a migration, use a versioned routing plan, drain the old topic, or route only new aggregates to the expanded topology.
Consumer Processing Can Break Order
A consumer can read records in partition order and still apply them out of order. For example, it polls PaymentAuthorized and PaymentCaptured, sends both to an unbounded executor, and the second HTTP call finishes first. Kafka did its job; the application discarded the ordering guarantee at the side-effect boundary.
Use one of these patterns when side effects must remain ordered:
- Process each partition’s records synchronously and commit after the effect succeeds.
- Use a per-key or per-partition queue that allows independent keys to run concurrently but serializes one key.
- Store an event version and reject stale updates in a conditional database write.
- Route failures to a retry or dead-letter path without advancing past an unresolved predecessor.
flowchart TD
A[Poll partition batch] --> B{Ordered handler?}
B -->|Yes| C[Apply record 1, then record 2]
B -->|No| D[Unbounded worker pool]
D --> E[Record 2 can finish before record 1]
C --> F[Commit next offset after durable effects]
E --> G[Use versions, per-key queues, or redesign]
The Kafka offsets boundary should follow the durable effect, not merely the moment a worker accepts a task. If the process crashes after an effect but before its commit, a replay is expected; make that effect idempotent with an event ID or version check.
Designing Around Hot Keys
Hashing balances keys, not record volume. If one seller emits half of a marketplace’s traffic, all of that seller’s keyed records remain on one partition. Adding partitions does not split one ordered key, and adding consumers does not allow two active owners for that partition in one group.
Start by asking whether the business truly needs one sequence. If only order-level events must be ordered, use orderId rather than sellerId. If an aggregate needs a strict balance sequence, keep it serialized and optimize the handler or database. If events are independently aggregatable, use a finer-grained key only after proving that cross-key order is irrelevant.
An explicit sub-key can be valid for a safe workload—for example, customerId + notificationType when each notification stream has independent semantics. It is not a safe shortcut for payments or inventory. Adding a random suffix to flatten a lag chart changes the correctness contract.
Choosing the Right Ordering Scope
The right scope is the narrowest one that protects the invariant:
| Scope | When to use it | Cost |
|---|---|---|
| Topic-wide | A workflow has one global sequence | One partition and one active group owner |
| Partition-wide | Independent keys share a partition | Consumers must preserve each partition’s order |
| Per aggregate | Orders, accounts, or customers need sequence | Hot aggregates remain serial |
| No ordering | Events are independent measurements or logs | Simplest and most scalable, but no sequence promise |
State the boundary in an API or event contract. “Events for one orderId are processed in sequence; different orders may run concurrently” is testable. “Kafka preserves order” is too vague to guide a producer, consumer, or incident response.
Production Example
An order workflow publishes all state transitions with orderId as the key to a 24-partition topic. Inventory, fraud, and analytics each use a separate consumer group. Inventory processes one partition sequentially, while analytics can batch independent records. The inventory database stores the last applied event version and rejects a stale transition.
During a producer retry, idempotence preserves the append sequence. During a consumer restart, the new owner can replay the last event because the database version check makes it harmless. During a hot order incident, the team does not add random key suffixes; it identifies whether the business operation can be split and, if not, accepts the serial boundary while optimizing the handler.
Interview Questions
1. Does Kafka guarantee ordering for a topic?
Kafka guarantees order within each partition, not a total order across a multi-partition topic. I first clarify which entity needs sequence, choose a stable key for that entity, and explain the throughput cost of serializing it. I also distinguish broker append order from downstream side-effect order.
2. How do message keys preserve order?
A non-null key is mapped to a partition, so records with the same key share one append-only log. The guarantee holds only when every producer uses the same key and serialization policy. It still requires the consumer to process that partition in order and protect the side effect from replay.
3. Can more consumers make one key process faster?
Not while preserving strict order for that key. One partition has one active owner in a consumer group, so more consumers help only other partitions. I would first optimize the serial handler or redesign the business operation if the key is a bottleneck.
4. How can producer retries reorder records?
With unsafe in-flight requests or disabled idempotence, a later request can be appended after an earlier request fails and is retried. I enable idempotence, use an acknowledged durability policy, and verify the client’s effective settings. This protects Kafka’s log order but does not make external effects transactional.
5. How do asynchronous consumers preserve order?
They use per-partition or per-key queues, bounded concurrency, and a commit that waits for all preceding work in the chosen boundary. Another option is a conditional write that accepts only the next event version. An unbounded worker pool with offsets committed on submission is not an ordering design.
6. What happens when partitions are added?
Existing data remains in its original partitions, but future keys can map differently. A key’s sequence can therefore span old and new partitions across the change. I treat expansion as a migration, test the ordering contract, and use a versioned or drained rollout when historical continuity matters.
Conclusion
Kafka ordering is a deliberately scoped guarantee: records are ordered within a partition, and keys decide which business events share that sequence. Protect it with idempotent producers, bounded consumer concurrency, durable commit boundaries, and version-aware sinks. The best design preserves only the ordering the business needs, leaving unrelated keys free to scale in parallel.
References
- Apache Kafka: Design and Implementation
- Confluent: Kafka Consumer Design
- Apache Kafka: Producer Configuration
YouTube Videos
- “Configuring Apache Kafka Durability, Availability, and Ordering Guarantees” - Confluent [https://www.youtube.com/watch?v=B8glj1BJkSw]