âEvent-Driven Architecture: The Top 10 Classic Problemsâ - Part 3 of 7. 2 AM, an alert fires:
NotificationServicehas stopped sending emails for 40 minutes - one malformed event has been retried thousands of times, jammed right at the head of the line. Later: Ms. TĂ places an order, then cancels it 2 seconds later -InventoryServicereceivesOrderCancelledbefore it receivesOrderCreated. Both problems here are direct consequences of the retry choices made in Part 2: retrying forever produces poison messages; non-blocking retry produces out-of-order events.
- Poison message: a message that never processes successfully, no matter how many times you try. After N failures (or the moment you know the error is permanent) â quarantine it to a Dead Letter Queue (DLQ), with an alert and a human-owned process.
- An unattended DLQ is a lost event with a death certificate - the most common mistake isnât skipping the DLQ, itâs having one nobody watches.
- Out-of-order events: a broker only preserves ordering within a single partition - parallelism and global ordering are natural enemies.
- The fix: a partition key on the business entityâs ID (enough for most cases) plus a version number so the consumer can detect reordering itself.
1. Poison messages - one broken message chokes the whole queue
What is it? A poison message (or poison pill) is a message a consumer never processes successfully, no matter how many times it tries: a malformed payload, data that violates a business rule, or an input that trips a bug in the consumerâs own code. Combined with at-least-onceâs âon error, redeliverâ mechanism (Part 1), it becomes an infinite loop: deliver â fail â return â redeliver â fail⊠The damage is twofold: wasted CPU and useless log spam; and if the consumer processes a partition sequentially, everything behind it is blocked - the head-of-line blocking we met in Part 2, except this time permanently, because the message at the front of the line never moves.
Why does it happen? Because the default retry mechanism canât tell a transient error from a permanent one (the classification from Part 2): it treats âmalformed payloadâ (still wrong on the millionth try) the same as âthe database blippedâ (works fine on retry). For a permanent error, retrying isnât persistence - itâs doing the same thing forever and expecting a different result. The sources of poison messages are effectively unlimited: a producer deploys a bug, a schema changes before every consumer is ready (Part 5), rare edge-case data, or a bug in the consumer itself triggered by one specific input.
How do you fix it? The Dead Letter Queue - a decent resting place for dead messages. The principle: after N failures, quarantine it. The message moves to a separate queue called a Dead Letter Queue (DLQ) - borrowing the name from the post officeâs room for undeliverable mail - along with forensic metadata: what error, which stack trace, how many attempts, which topic/partition/offset it came from. The main line is immediately unblocked.
flowchart TD
A["Message arrives"] --> B{"Processed successfully?"}
B -->|"yes"| C["ack - done"]
B -->|"PERMANENT error (malformed, HTTP 400)"| E["straight to DLQ, no retries wasted"]
B -->|"TRANSIENT error"| D{"attempted >= N times?"}
D -->|"not yet"| F["retry with backoff (Part 2)"] --> B
D -->|"yes"| E
E --> G["Alert the team (DLQ depth > 0)"]
G --> H["A human investigates: fix the bug / fix the data"]
H --> I{"Decision"}
I -->|"can be reprocessed"| J["REDRIVE - replay back onto the main topic"] --> A
I -->|"unsalvageable"| K["Discard deliberately, with a paper trail"]
Notice the âpermanent â straight to DLQâ branch: good error classification saves the entire cost of a wasted retry loop. And notice the right-hand side - the DLQ isnât an ending, itâs a handoff to a human.
Built-in support varies by infrastructure: RabbitMQ has a native dead letter exchange (DLX); AWS SQS has a redrive policy with maxReceiveCount - exceed it, and the message is automatically moved to the configured DLQ; Kafka has no native DLQ - teams roll their own via a convention topic (e.g., orders.DLT), and frameworks like Spring Kafka ship a DeadLetterPublishingRecoverer for it.
An unattended DLQ is a lost event with a death certificate. The most common mistake isnât skipping a DLQ - itâs having one nobody watches. A message that lands in the DLQ and sits there forever has the exact same business outcome as a lost event (Part 1) - the only difference is thereâs a spot to dig it back up. A real DLQ needs: (1) an alert when new messages arrive (DLQ depth is a mandatory metric - Part 6); (2) an owned process: investigate â fix the cause â redrive (replay the message back onto the main topic, now that the code is fixed) or discard it with a paper trail. Redrive is also why the consumer has to be idempotent - the message may have half-processed before it died.
Property - Dead-lettering & redrive. Dead-lettering is the mechanism that automatically quarantines a repeatedly failing message to separate storage, trading âinfinite loopâ for âwaiting on a human judgment call.â Redrive is the reverse operation - replaying a message from the DLQ back onto the original queue once the root cause has been addressed. A good health check for any system: âhow many messages are sitting in your DLQ right now, and whoâs going to notice if that number climbs?â If nobody can answer, there isnât really a DLQ.
2. Out-of-order events - âcancelledâ arrives before âcreatedâ
What is it? Events with a causal relationship (this one has to happen before that one: create â modify â cancel) get received and processed out of order by the consumer. Unlike loss and duplication, out-of-order delivery usually doesnât throw any technical error - every handler runs âsuccessfullyâ - only the final state is wrong: a shipping address gets overwritten with a stale value; a cancelled order still gets its stock reserved.
Why does it happen? Parallelism and ordering are natural enemies. Recall from Part 0: a topic is split into partitions so multiple consumers can process it in parallel. The broker only promises ordering within a single partition - across partitions, itâs a free-for-all. Four common sources of reordering:
- Two events land in different partitions. If the producer doesnât specify a partition key,
OrderCreated #4711might land in a backed-up partition whileOrderCancelled #4711lands in an idle one - the younger event finishes first. - Multiple consumer instances processing in parallel - each at its own speed, with its own GC pauses and network latency.
- Producer retries reshuffle the send order: message 1 hits a network error and has to be resent, while message 2 already made it through.
- Our own non-blocking retry (Part 2): a failed message loops through a retry topic and comes back - automatically landing behind every message that arrived after it.
sequenceDiagram
participant O as OrderService
participant P0 as Partition 0 (backed up, 500 msgs)
participant P3 as Partition 3 (idle)
participant I as InventoryService
O->>P0: t=0ms OrderCreated #4711
O->>P3: t=2000ms OrderCancelled #4711
Note over P3: partition is idle - delivers immediately
P3->>I: t=2100ms OrderCancelled #4711
Note over I: cancelling an order it has never seen -> handler skips it "to be safe"
Note over P0: finally drains 500 backed-up messages...
P0->>I: t=2400ms OrderCreated #4711
Note over I: creates the order + reserves stock for an order that is actually already cancelled
A race between two partitions: the younger event (Cancelled) finishes first because it took the idle lane. InventoryServiceâs final state is completely wrong, even though every individual step âran fine.â This class of bug never shows up in an error log - it shows up as a discrepancy accounting finds days later.
Why not just force the whole system into a single line? You could - one partition, one consumer: perfect total ordering, and throughput pinned to the capacity of a single machine, which erases the entire reason to go distributed in the first place. Total ordering and parallelism are a direct trade-off - fortunately, the business rarely actually needs true global ordering.
How do you fix it? Per-key ordering is enough; version numbers cover the rest.
1. Partition key: put ordering exactly where itâs needed. The key insight: nobody needs order #4711âs events and order #4712âs events to be ordered relative to each other - only events for the same order need to stay in order. So: the producer sets a partition key equal to the entityâs ID (orderId). The broker hashes the key â every event for #4711 always lands in the same partition â gets delivered in send order â and since each partition is read by exactly one consumer within a group, itâs processed sequentially. Parallel across orders, sequential within one order - both worlds win.
// Kafka producer - the key decides the partition, which decides the order
producer.send({
topic: "orders",
key: event.orderId, // #4711 -> hash -> always the same partition
value: JSON.stringify(event),
});
// Pair with (closes reordering source #3 - producer retries reshuffling order):
// enable.idempotence=true -> broker numbers messages and drops reshuffled duplicates
2. Version numbers: let the consumer detect reordering on its own. A partition key handles sources â -âą but doesnât save you from source ⣠(retry topics), and it doesnât help when events arrive from multiple different topics. The second line of defense sits on the consumer: the producer stamps an increasing version number, per entity (version: 1, 2, 3...) on the event; the consumer stores the last version it applied and compares:
// Consumer that guards against reordering using version numbers - also folds in Part 1's dedup
async function apply(event) {
const current = await db.get(`orders/${event.orderId}`); // current version, e.g. 3
if (event.version <= current.version) {
return;
} // stale or duplicate -> skip (idempotent!)
if (event.version > current.version + 1) {
// a gap (e.g. receiving 5 while at 3)
await parkAndWait(event);
return; // park it, wait for #4 to arrive
}
await db.applyWithVersion(event); // exactly the next piece -> apply it
}
The three branches are three sub-strategies: discard stale versions (last-write-wins - fine when the event carries target state); buffer while waiting for a missing piece (when you truly need every event, in order - the cost is memory and latency); or design events so ordering stops mattering - see the commutativity box.
Property - Ordering guarantee & Causality. Total ordering: every consumer sees every event in exactly the same sequence - only achievable by giving up parallelism. Per-key ordering: only events sharing a key stay ordered relative to each other - this is what distributed brokers actually provide (Kafka: per-partition) and itâs usually all the business actually needs. Causality: the relationship âA has to happen before B makes senseâ - the real goal of this whole section is preserving causality, and per-key ordering is the cheapest way to get it.
Property - Commutativity. Two operations are commutative when swapping their order doesnât change the final result: a + b = b + a. If you can design a commutative handler, the ordering problem⊠disappears. Example: âadd 50 loyalty pointsâ and âadd 30 loyalty pointsâ commute; âset address = Xâ and âset address = Yâ donât - the latter needs a version number. Data structures built on this principle have their own name: CRDTs (Conflict-free Replicated Data Types), used in real-time collaboration tools like Figma and Google Docs.
Chained traps with Part 2 and Part 5. Two decisions made elsewhere quietly break the ordering this section works to establish: (1) a non-blocking retry topic (Part 2) pulls a failed message out of its lane - if the business needs per-key ordering, every message sharing that key has to wait too, not jump ahead; (2) increasing a live topicâs partition count (Part 5) re-hashes keys to new destinations - an old event for #4711 sits in partition 2, a new one lands in partition 5, and per-key ordering breaks during the transition. Provisioning partitions generously up front is exactly how you dodge the second trap.
â Previous: Part 2 - Outbox & Retries · Next: Part 4 - Consistency & Sagas â
The series - Event-Driven Architecture: The Top 10 Classic Problems: 0 · Foundations · 1 · Lost & Duplicate Events · 2 · Outbox & Retries · 3 · Poison Messages & Ordering (you are here) · 4 · Consistency & Sagas · 5 · Backpressure & Schema Evolution · 6 · Observability & Recap