Video in progress

The events of one order must be processed in strict order, but there are occasional transient errors. How do you design the retry?

If you need ordering at the key level, plain non-blocking retry doesn't work. The options: blocking retry (the whole partition waits), or "stopping the key", which means queueing a failed key's later events too until it recovers, or making the consumer tolerant of versions.

📖 Answer

I see three ways.

  1. Blocking retry. DefaultErrorHandler + exponential backoff, many attempts. The order is fully preserved, but one order's problem also stalls thousands of other orders in that partition. If the errors are short (a DB failover, a few seconds), it's the simplest and the right choice.

  2. "Parking" at the key level. The failed orderId is written to a "blocked" list (a DB table), and the event goes to a retry topic. On every following event the main consumer checks: if its key is blocked, it also goes to the retry/parking topic, in order. After the retry succeeds, the key is released and the parked events are replayed one after another. Other orders don't stop. The cost is complexity and extra state, and it's hard to test.

  3. Not depending on order. The event carries a version or sequence, and the consumer applies only version = current + 1, drops older ones, and puts ones that jumped ahead on hold. Or the event carries the full state (state transfer), and then "the newest version wins" is enough.

UPDATE orders SET status = ?, version = ?
WHERE id = ? AND version = ? - 1;   -- if 0 rows: it arrived out of order

I usually use 1 + 3 together: a short blocking retry for transient errors, the consumer protected by versions, and for a long outage a DLT and an alert. Option 2 only if the business truly requires it.

One catch: retrying an "unfixable" error (validation, a missing entity) is pointless; send it straight to the DLT, otherwise the partition stalls for nothing.