Ordering vs parallelism — partition the keyspace
A queue with N competing consumers cannot also be globally ordered; ordering is preserved only within a message group, and you scale ordered work by partitioning the keyspace into many groups.
Throughput is tuned: timeout, prefetch, worker count. But everywhere we've scaled out, we've also scrambled order — messages 1, 2, 3 land on different workers and finish in whatever order they finish. If order matters, we have to pay for it.
Scene 10
Ordering vs parallelism — partition the keyspace
- Watch
- Try it
- Predict
- Capture
Watch all three lanes drain in parallel. Standard finishes fast but the tracer is scrambled. FIFO-single keeps perfect order — at the cost of 7 idle workers. FIFO-keyed stripes work across 4 colored groups: ordered within each color, parallel across them.
Highlighted lines are the ones running in the diagram right now.
def handOutFifo(cell):g = cell.MessageGroupId# the ordering invariant: one worker per group at a timeif g in in_flight_groups:return # head-of-line block for this group onlyw = pick_free_worker()if w is None:return # all workers busy — try next tickin_flight_groups.add(g)w.deliver(cell, on_done=lambda:in_flight_groups.discard(g))
def assignByGroup(cell, groupId):# hash-route to a sub-strip; sticky per groupsubStripIx = stable_hash(groupId) % numSubStripssubStrips[subStripIx].append(cell)def drainSubStrips():# every tick: one head per sub-strip, in parallelfor strip in subStrips:head = strip.peek()if head and head.MessageGroupId not in in_flight_groups:handOutFifo(head)strip.pop()
# Kafka producer side — same idea, different namedef routeToPartition(record, numPartitions):if record.key is None:return round_robin() # like Standard: order-free# MessageGroupId here is spelled `record.key`return murmur2(record.key) % numPartitions# Each partition is consumed by exactly ONE consumer in# the group at a time — that's the ordering scope.# Scale ordered work by adding partitions == adding groups.
Where this sits in Build a Message Queue (RabbitMQ / SQS)
Scene 10 of 14. Globally ordered + competing consumers is impossible. FIFO preserves order WITHIN a message group; many groups = parallelism. Kafka's partition-by-key in queue costume.
Up next. Ordering scope per group — that's partition-by-key with new vocabulary. Now: every scene so far has had exactly one queue. Real systems need fanout (one event, many independent consumer pools). Where does that live?
All 14 scenes in Build a Message Queue (RabbitMQ / SQS) · Every curriculum