Skip to content

Lesson 3 of 9 · 25m

Ordering, Partitioning, and Fanout

Use keys, partitions, topics, and consumer groups to control concurrency without pretending distributed systems preserve global order.

There are two kinds of ordering in queue systems: the ordering you need and the ordering you accidentally assumed. Most applications do not need global ordering. They need per-entity ordering: events for one invoice, one user session, one device, or one document must not leapfrog each other. Once you see that distinction, scale-out design becomes much simpler.

Global order does not scale

A single ordered queue is a serialization point. Every consumer waits for the head of line, one poison message blocks everything behind it, and throughput is capped by the slowest worker. If you truly need one global sequence, you have a log or database sequencing problem, not a normal background-job problem.

Per-key ordering is the practical target:

key=user_42    created -> upgraded -> disabled
key=user_99    created -> invited -> accepted

Those two streams can run in parallel. Events inside each stream cannot overlap.

Partition deliberately

Kafka partitions by key hash. SQS FIFO groups by MessageGroupId. RabbitMQ can use consistent-hash exchanges or separate queues. A database-backed worker can shard by tenant_id or aggregate_id.

def group_key(event):
    return f"{event.tenant_id}:{event.aggregate_id}"

Do not use a random message ID as the partition key when ordering matters; that spreads related work across all partitions and destroys the guarantee. Do not use only tenant ID for a hot tenant either. One large customer can create a single overloaded partition.

Fanout means independent copies, not shared work

Pub/sub fanout creates one copy per subscription. A search-index consumer, email consumer, and audit consumer each see the same event independently. Queue fan-in is different: many workers compete for the same message, and only one should process it.

event topic
  ├── search-indexer consumer group
  ├── notifier consumer group
  └── audit-archive consumer group

job queue
  ├── worker A competes
  ├── worker B competes
  └── worker C competes

If two processes should divide labor, put them in the same consumer group; if each needs a full copy, use separate groups or subscriptions.

Reordering is a normal failure mode

Retries, dead-letter replay, consumer restarts, and uneven latency all reorder events in practice. Workers should compare versions:

update invoice_projection
set status = 'paid', version = 18
where invoice_id = 'inv_10492' and version < 18;

If zero rows update, the event was stale or already applied. That is often correct behavior, not an error.

Field notes

  • FIFO only applies inside the documented ordering scope. "The queue is FIFO" without naming the group or partition is meaningless.
  • Poison messages in partitioned systems block their key, not necessarily the entire fleet. Monitor per-partition lag.
  • Hot keys need design: split subresources, batch updates, or serialize deliberately while the rest of the fleet scales.
  • Fanout multiplies downstream load: one event with five subscriptions is five chances to overload a fragile dependency.

Was this lesson helpful?

Sign in to rate