dine. a kitchen run by sagas Book a table
Log in with Google (coming soon)

Articles

Tiếng Việt · English

Where broken messages go

October 14, 2026 · about 10 min read

The earlier articles all say that messages in dine are "never lost". But some messages can never be processed: the data inside is wrong, the code has a bug, or that service's database has been in trouble for a long time. Retrying makes no difference. These messages must not disappear, and they must not jam the whole system either. This article is about where they go, and when they have to reach a human.

One broken message can jam the whole queue

Kafka delivers messages in order within each partition, like a line of people at a counter. The receiver marks "processed up to here" before moving on to the next message. If one message keeps failing, and the receiver keeps retrying it forever, every message behind it in the same line has to wait. One faulty order drags hundreds of normal orders into a jam.

A message like this is called a poison message. The standard fix is to take it out of the line and put it somewhere separate to deal with later. That separate place is called a dead-letter queue, DLQ for short.

Does retrying help?

Before retrying, the first question is always: will this error go away by itself?

Temporary errorPermanent error
ExampleLost database connection, a lock is busy, a service is restartingThe data in the message is wrong, the code has a bug for exactly this kind of message
RetryingHelps, it may work in a momentUseless, it gives the same error every time
What to doWait a little, then retryTake it out of the line right away, keep it for a person to look at

The hard part is that you cannot always tell them apart. So dine retries in stages, each stage waiting longer than the one before. A temporary error will clear at some stage. A permanent error goes through every stage and ends up with a person.

The path of a broken message at dine

Every dine service (order as well as the other three) follows the same path:

The path of a broken message: retry 3 times right away; if it still fails, send it to a DLQ topic on Kafka; a worker copies it from the DLQ topic into the dead_letters table in MySQL; another worker retries on a schedule that spreads out, up to 5 times; when retries run out it is marked as needing a person. 1. Quick retry 3 times, 0.5s apart inside the service 2. DLQ topic on Kafka, keeps the original message 3. dead_letters a table in the service's MySQL 4. Scheduled retry 30s, 60s, 90s… up to 5 times Succeeds at any stage processed as usual, done 5. Needs a person no retries left, last error kept Dashed: a retry succeeded. The main line never waits for a broken message: it is set aside right after step 1.
Each stage waits longer than the one before. A temporary error clears at some stage; a permanent error goes all the way and reaches a person.
  1. Quick, short retries. Three times, half a second apart, inside the service itself. Enough to get past momentary glitches.
  2. Take it out of the line. If it still fails, the service sends the original message untouched to a separate DLQ topic on Kafka, along with where it came from and what the error was, then marks the original message as processed. The main line moves on immediately.
  3. Copy it into the database. Another worker reads the DLQ topic and copies each message into the dead_letters table in that service's own MySQL.
  4. Retry on a schedule that spreads out. Every 10 seconds, a worker checks the table for messages due for another attempt. The first after 30 seconds, the next after 60, then 90, up to 5 times. It reruns exactly the original handler, with no separate logic.
  5. Stop when retries run out. The message is marked as needing a person, along with the last error. The system does nothing more with it on its own.

Why not just leave it on Kafka?

Kafka is good at moving messages in order, but not at answering questions like "how many broken payment messages are there today?", "which message is next to be retried?", or "has anyone dealt with this message yet?". A database table answers all of them with one query, and can store the retry schedule, the number of attempts and the notes of whoever handled it.

This table also has to tolerate duplicates, because messages from the DLQ topic can be redelivered too. It uses exactly the technique from the article Receiving a command twice without doing it twice: each message is identified by its original position in Kafka (topic, partition, offset), marked UNIQUE, so copying it twice still leaves only one row.

DLQ topic: one shared, or one per service?

You could gather the broken messages of all four services into one shared DLQ topic to keep things tidy. dine gives each service its own DLQ topic instead, and each service copies its broken messages into the dead_letters table in its own database. The reasons:

The only price is a few more topics, and at dine topics are already created up front with Ansible like every other topic.

Does handling the DLQ need to be fast?

No. The DLQ is an auxiliary part, not the main path. Every message here has already failed on the main path, and its order has usually reached an outcome long before. The 30, 60, 90 second retry schedule above is slow on purpose: it gives a struggling service time to recover, instead of piling more load on it at exactly its weakest moment.

If one day messages pour into the DLQ faster than they can be handled, the answer is not to make the DLQ faster. It is a sign that the system itself has a problem: a bug that was just deployed, a dependency that is down, a kind of data nobody anticipated. That is where the fix belongs. The number of messages in the DLQ should be watched like an alarm bell, not treated as a queue whose speed needs optimizing.

When a broken message is processed late

A message taking this detour might be processed successfully after tens of seconds or a few minutes. By then its order has usually been cancelled for going over 5 seconds. That is fine: if the late message completes its work (charging money, for example), order recognizes it as a late success and sends an undo command. This is exactly where the mechanisms from the earlier articles fit together.

When the machine runs out of options: a person's turn

Running out of automatic retries does not mean giving up. It means this needs someone who understands the situation to take a look. For most broken messages, that is an engineering job: find the bug, fix it, run it again. But some messages directly affect a customer, and money is the clearest case.

Example: a refund command that cannot be processed

A guest books a table and pays by card; everything is normal. A few days later the order is cancelled, and order sends a refund command. But in those few days the guest reported the card lost, and the bank blocked the old card. When the money was charged, the card was valid; when the refund comes, it is not.

The bank returns an error code the payment code has never seen: not "success", and not in the list of known errors either. There is no branch in the code for this case, so the handler can only report an error. However many times it retries, it gets the same code, so the message goes all the way along the path above and stops at "needs a person". A developer opening the logs sees only an unfamiliar error code, with no idea whether the money was refunded, or whether to try again. Meanwhile the guest has been charged and not yet refunded, and the order sits at "undoing".

In a real system, this is where the customer care process begins:

  1. Detect. The list of messages that "need a person" and the orders stuck at "undoing" for a long time are checked daily, or an alert goes to whoever is on call.
  2. Look it up. From the saga ID in the message, find the order, the guest, the amount and the last error. The order detail page shows which steps the order went through.
  3. Contact the guest. Customer care calls or messages the guest: apologizes, explains that the money will be returned, and how and when.
  4. Resolve. If the error was in the system and has been fixed, engineering reruns the message. This is safe because the refund step is already idempotent: once refunded, it will not refund again. If not, staff refund through another channel, for example a direct bank transfer.
  5. Close it. Record how it was handled, then tell the system the job is done so the order ends in the right state. dine already has a "handled by a person" signal for the refund step: the payment service only records it and does not attempt the refund again.

With money, stopping beats guessing. A system that keeps retrying a refund on its own, without being sure the previous attempt really failed, can pay the guest back twice. Stopping, keeping enough information, and letting a person decide is the safest way.

There are two roads that lead to a person, and they are worth telling apart. If the code already knows the blocked-card case (the bank returns a clear error code and the code has a branch for it), it is a business outcome: the undo step reports "failed" and the order stops to wait for a person, as covered in the article Compensation is not rollback. Here, though, the code never anticipated this case, so the message cannot be processed and lands in the DLQ. Both roads end up on customer care's desk, but the second one also leaves engineering with a job: add a branch for the new error code. Next time it shows up, it will take the first road.

dine is a personal project, so there is no dedicated interface for the person handling it yet. The "needs a person" mark, the last error and the "handled" signal are already in the system, enough to build that interface when it is needed.

The price

In short: don't let one broken message jam the whole line, and don't let it disappear either. Retry briefly, then move it somewhere separate, retry less and less often, and when the machine runs out of options, stop and hand it to a person. For anything involving a customer's money, the last step is often a phone call.

Once a system is really in operation, the code can only handle most of what was anticipated; it never covers everything. For the cases you are not sure about, don't make the machine retry forever: retrying endlessly does not make a strange case less strange, it only makes it harder to see. At that point, the best thing the system can do is stop, keep enough information, and call a human.

See it for yourself

Spotted a mistake, or have a question? Write to [email protected].

dine is a personal project about distributed systems, written in Go: orchestrated sagas, transactional outbox, Kafka. More articles.