When order matters, how to keep it, and what it costs in speed.
Message ordering is about one question: will messages be handled in the same order they were sent? In a distributed system the honest answer is usually "only inside one lane".
In one program with one thread, order is easy: first in, first out. In a distributed system, messages travel over networks with different delays, are split over several queues or partitions, and are picked up by several workers at the same time. Any of these can swap the order.
| Cause | What happens |
|---|---|
| Several consumers in parallel | A fast worker finishes a later message before a slow worker finishes an earlier one |
| Retries | A failed message is tried again later, after newer messages were already handled |
| Several queues or partitions | Each lane has its own order, with none between lanes |
| Network delays | Messages take different routes or are resent, and arrive out of sequence |
| Multiple producers | Two senders have no shared clock, so “first” may not even be defined |
| Level | What is promised | Cost |
|---|---|---|
| No ordering | Messages can arrive in any order | Fastest and simplest. Fine for independent tasks |
| Per key (partition) | Order among messages with the same key | Parallel across keys. A hot key limits speed |
| Per queue | Order of one whole queue, with one consumer | One worker's speed only |
| Total (global) order | One order for every message in the system | Needs a single sequencer or consensus. Slow and a bottleneck |
| Causal order | If one event caused another, they stay in that order | Needs logical clocks. Unrelated events may differ |
Event time is when something happened. Processing time is when your system saw it. A mobile app that was offline may send events from an hour ago, so they arrive late. If you need the true order of events, sort or window by event time, and decide how long to wait for late ones.
A simplified model with a fixed event list, fixed processing times (1 to 3 ticks, 3 extra for a retry) and no random numbers, so the same settings always replay the same story. The tick counts are illustrative, not measurements.
| Term | Meaning in easy words |
|---|---|
| FIFO | First in, first out: the oldest message is handled first |
| Total order | One agreed order for every message in the whole system |
| Partial / per-key order | Order that holds only among related messages |
| Partition key | The value that decides which lane a message goes to |
| Message group | A FIFO-queue label: messages in one group are handled in order |
| Sequence number | A counter on each message, so a receiver can spot gaps and old messages |
| Out-of-order delivery | A message arriving or being handled after a later one |
| Head-of-line blocking | A stuck message holding up everything behind it in a lane |
| Hot key | A key with so much traffic that its lane becomes a bottleneck |
| Event time | When the thing actually happened |
| Processing time | When your system got to see it |
| Choice | We gain | We lose |
|---|---|---|
| No ordering, many workers | Top speed, simple scaling | Correctness for anything order-sensitive |
| Order per key | Correct per entity, parallel across keys | A hot key is a bottleneck. Rebalancing keys can break order briefly |
| One single consumer | Total order, easy to understand | Speed of one worker, and a single point of failure |
| Global order | Everything is sequenced | A sequencer or consensus on every message, which is slow |
| Sequence numbers and drop-stale | Safe even if delivery reorders | More code and state per key |
| Block the lane on retry | Order is kept exactly | One failing message stops all behind it, so you need a dead letter path |