awesome-everything RU
↑ Back to the climb

Queues, Streams, Eventing

Message ordering: code and config reading

Crux Read real producer and consumer snippets, predict the ordering behaviour, and pick the highest-leverage fix a senior would make first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

Ordering bugs live in the producer config and the consumer’s handler, not in the broker. Read the code, predict where order breaks, and choose the fix a senior engineer would reach for first.

Goal

Practise the loop you run on every ordering incident: read the partition key and producer config, find where two messages can race or swap, and reach for the structural fix — key choice, idempotent producer, version guard — before blaming the broker.

Snippet 1 — the partition key

// Events for one account must stay ordered relative to each other.
ProducerRecord<String, Event> record =
    new ProducerRecord<>("account-events", UUID.randomUUID().toString(), event);
producer.send(record);
Quiz

The requirement is per-account ordering. What does this key choice actually do, and what is the fix?

Snippet 2 — the producer config

enable.idempotence=false
max.in.flight.requests.per.connection=5
retries=10
acks=all
Quiz

The key is correct and everything is on one partition. Why can two messages still swap, and what is the one-line fix?

Snippet 3 — the consumer handler

def handle(msg):
    user = db.get(msg.user_id)
    user.name = msg.new_name      # last write wins on whatever arrives last
    db.save(user)
Quiz

Consumers are concurrent on an at-least-once queue. Two updates for one user can arrive out of order or twice. What is the strongest fix?

Snippet 4 — the DLQ replay

# Nightly job: drain the dead-letter queue back into the main topic
for failed in dead_letter_queue.drain_all():
    main_topic.produce(failed.key, failed.value)   # full speed, no throttle
Quiz

The consumer is idempotent and version-guarded. Even so, what does this replay do to live ordering, and how do you run it safely?

Recap

Ordering is read in the producer and consumer, not the broker: a random partition key scatters an entity’s events and must be the consistency boundary; a non-idempotent producer with multiple in-flight requests reorders at the source, fixed by enable.idempotence=true; concurrent consumers on an at-least-once queue need a per-entity version guard, not just a transaction or dedup; and DLQ replay reorders against live traffic, safe to apply under a version guard but only when rate-limited. Fix the structure first, then verify the order holds under load.

Continue the climb ↑Message ordering: build an order-surviving consumer
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.