Tiếng Việt · English
Where broken messages go
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 error | Permanent error | |
|---|---|---|
| Example | Lost database connection, a lock is busy, a service is restarting | The data in the message is wrong, the code has a bug for exactly this kind of message |
| Retrying | Helps, it may work in a moment | Useless, it gives the same error every time |
| What to do | Wait a little, then retry | Take 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:
- Quick, short retries. Three times, half a second apart, inside the service itself. Enough to get past momentary glitches.
- 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.
- Copy it into the database. Another worker reads the DLQ topic and copies each message into the
dead_letterstable in that service's own MySQL. - 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.
- 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:
- Each service owns its broken messages. Only the payment service understands payment's broken messages: what they contain, how to rerun them, who is responsible for fixing them. Put them all in one place and everyone can see them, but nobody really owns them.
- No mixing of each other's data. Payment messages carry money information, kitchen messages carry stock information. Each kind has its own structure and its own sensitivity. In a shared topic, the kitchen's retry worker would also have to read through payment's messages, even though it can do nothing with them.
- One service in trouble does not flood the others. If the kitchen fails in bulk, only the kitchen's DLQ topic fills up; payment's broken messages are still seen and handled as usual.
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:
- 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.
- 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.
- Contact the guest. Customer care calls or messages the guest: apologizes, explains that the money will be returned, and how and when.
- 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.
- 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
- More to watch. One more topic, one more table, two more background workers in every service. A
dead_letterstable that slowly fills up with nobody looking at it is as good as not having one. - Ordering is no longer kept for broken messages. A message taken out of the line is processed after messages that arrived later than it. At dine this is acceptable, because each order is independent and the late cases already have a way to be handled.
- There is still a last case. If Kafka itself will not accept the message into the DLQ topic, all the service can do is write the full message into its log. No system is perfectly safe; you only add layers to make that rarer.
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
- Refund failed, waiting for staff The order stops at "undoing" and does not retry by itself. This is the bank-refusal case, which also ends up needing a person.
- Payment ledger Payments waiting for manual handling have their status clearly marked.
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.