The end of poison messages!
Tags: opensource, product, tooling, github
Published: 2022-07-20 18:31:17

Have you ever got a call in the middle of the night saying “Infrastructure looks ok, but some service is not consuming data / messages get redelivered. Please figure it out”

Redelivered messages are also called “Poison messages”.

Poison messages = Messages that cause a consumer to repeatedly require a delivery (possibly due to a consumer failure) such that the message is never processed completely and acknowledged so that it can be stopped being sent again to the same consumer.

**Example**: Some message on an arbitrary queue pushed/pulled to or by a consumer. That consumer, for some reason, doesn’t succeed in handling it. It can be due to a bug, an unknown schema, resources issue, etc…

In RabbitMQ, for example, quorum queues keep track of the number of unsuccessful delivery attempts and expose it in the “x-delivery-count” header that is included with any redelivered message. It is possible to set a delivery limit for a queue using a policy argument, delivery-limit. When a message has been returned more times than the limit the message will be dropped or dead-lettered (if a DLX is configured).

It is known to any developer that uses a queue / messaging bus that poison messages should be taken care of, and it’s the developer’s responsibility to do so.

In RabbitMQ, the most common, simple solution would be to enable DLX (dead-lettered queue), but it doesn’t end there.

Recovering a poison message is just the first part, and the developer must also understand what causes this behavior and mitigate the issue.

---

**Solutions**
While there is the classic solution of committing/acknowledging a message as soon as possible, it’s not the best option for use cases requiring ensuring messages are acknowledged only when finished being handled.

Other approaches –

**How to handle unacknowledged messages in RabbitMQ**

1. Turn on DLX
2. Configure the DLX
3. Place a routing key
4. Build a dedicated consumer pointed to the DLX
5. Consume the unacknowledged messages
6. Fix the code/events


![FAQ: When and how to use the RabbitMQ Dead Letter Exchange - CloudAMQP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pvhm2z719kmls4e7xgj0.png)

**How to handle unacknowledged messages in Apache Kafka**

There is no out-of-the-box redelivery/recovery of such messages.

1. Ensure there are logs within the code, tracking exceptions, and export to pagerduty/datadog/new relic/etc…

2. If the retention of a message is too small, it will be gone before the developer gets the chance to debug it. In most cases, the message will not be unique and can the loss, but in other, like transactions / atomic requests, it is. To mitigate this, a wrapper that provides this functionality should be made. A great example of such that provides that ability, and more is Wix’s [GreyHound](https://github.com/wix/greyhound). Definitely worth taking a look.

3. There are other use cases that utilize cache DB of some kind to persist the message while being processed before being getting committed.

![architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z6jzc4kdzm8uotaquimt.png)

---

**Unacknowledged messages in [Memphis.dev](https://memphis.dev)** 

When we started to refine our approach, understand the needs, validate the experience, and craft the value it will bring to our users – three key objectives led our process – 

1. The entry point of a user to the issue.
2. Quickly understand the problematic consumer and the root cause of the issue.
3. Fix code. The developer must debug the fix with a similar message to ensure it works.

![Alert-Fix-Root](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1l2278hzvuk9cljrsy3u.jpeg)

1 - **Define the trigger**

In Memphis broker, at the SDK level, we use a parameter called “maxMsgDeliveries.”
Once this threshold is reached, the broker will not repeatedly send the “failed-to-ack” message to the same consumer group.

![Poison message](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmafrrfwcf7hpacakzdi.jpeg)

2 - **Notification**

- Memphis broker senses the event of crossing the “maxMsgDeliveries = 2” per station, per consumer group.
- Persist the time_sent, and payload of the redelivered message to the Memphis file-based store for 3 hours.
- Mark the message as poisoned by a specific consumer.
- Create an alert.

![notification](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wi7bi7hwgcr7s51m3q1z.jpeg)

![notification-2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cce1z64komeonutpm9wx.png)

3 - **Identify the consumer group which didn't acknowledge the message**

Instead of going around through logs and multiple consumer groups, we wanted to narrow the finding to the minimum, so a simple click on the “real-time tracing” will lead to a graph screen showing the CGs which passed the redelivery threshold.

![Group](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kpvtgw2r9xmh8fxeqyic.png)

4 - **Fix and Debug**

After the developer understand what went wrong and creates a fix, before pushing the code which will lead to probably more adjustments when new messages will arrive, we have created the “Resend” mechanism which will push the unacknowledged message as many times as needed (Until ACK) to the consumer group that was not able to acknowledge the message in the first place.

![Resend](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/thbadh83g7n0058vhl10.jpeg)

The unacknowledged message will be retrieved from Memphis internal DB, ingested into a dedicated station per station, per CG, and only upon request. Next, it will be pushed to the unacknowledged CG – WITHOUT ANY CODE CHANGE, using the same already-configured emit.

That’s it. No need to create a persistency process, cache DB, DLQ, or massy logic. You can be sure no message is lost.

---
[Join 4500+ others and sign up for our data engineering newsletter](https://memphis.dev/newsletter)

---
Follow Us to get the latest updates!
[Github](https://github.com/memphisdev/memphis) • [Docs](https://docs.memphis.dev/memphis/getting-started/readme) • [Discord](https://discord.com/invite/DfWFT7fzUu)

---
This article is written by [Avraham Neeman](https://twitter.com/AvrahamNeeman)