{"id":7044,"date":"2026-11-16T02:23:33","date_gmt":"2026-11-16T05:23:33","guid":{"rendered":"https:\/\/tucumandevelopers.com\/index.php\/2026\/11\/16\/handling-poison-messages-in-aws-event-driven-pipelines\/"},"modified":"2026-11-16T02:23:33","modified_gmt":"2026-11-16T05:23:33","slug":"handling-poison-messages-in-aws-event-driven-pipelines","status":"publish","type":"post","link":"https:\/\/tucumandevelopers.com\/index.php\/2026\/11\/16\/handling-poison-messages-in-aws-event-driven-pipelines\/","title":{"rendered":"Handling Poison Messages in AWS Event-Driven Pipelines"},"content":{"rendered":"<div>\n<div><\/div>\n<p>By offloading the message to the <code>dlq<\/code> after three attempts, you ensure the primary consumer remains healthy and available for subsequent, valid messages.<\/p>\n<h2> <a name=\"step-2-implementing-idempotent-consumers\" href=\"#step-2-implementing-idempotent-consumers\"> <\/a> Step 2: Implementing Idempotent Consumers <\/h2>\n<p>Moving a message to a DLQ is only half the battle. If your consumer fails partially through a batch process, you risk processing the same message twice. You must design your consumers to be idempotent.<\/p>\n<p>When using Lambda with SQS, partial batch failures can occur. If your Lambda processes a batch of 10 messages and the 8th message fails, you don&#8217;t want to re-process the first 7. You can use the <code>ReportBatchItemFailures<\/code> feature to tell SQS exactly which messages failed. <\/p>\n<div>\n<pre><code><span>def<\/span> <span>handler<\/span><span>(<\/span><span>event<\/span><span>,<\/span> <span>context<\/span><span>)<\/span><span>:<\/span> <span>batch_item_failures<\/span> <span>=<\/span> <span>[<\/span><span>]<\/span> <span>for<\/span> <span>record<\/span> <span>in<\/span> <span>event<\/span><span>[<\/span><span>'Records'<\/span><span>]<\/span><span>:<\/span> <span>try<\/span><span>:<\/span> <span># Process record logic<\/span> <span>process_message<\/span><span>(<\/span><span>record<\/span><span>)<\/span> <span>except<\/span> <span>Exception<\/span> <span>as<\/span> <span>e<\/span><span>:<\/span> <span># Add failed message ID to the list<\/span> <span>batch_item_failures<\/span><span>.<\/span><span>append<\/span><span>(<\/span><span>{<\/span><span>\"itemIdentifier\"<\/span><span>:<\/span> <span>record<\/span><span>[<\/span><span>'messageId'<\/span><span>]<\/span><span>}<\/span><span>)<\/span> <span>return<\/span> <span>{<\/span><span>\"batchItemFailures\"<\/span><span>:<\/span> <span>batch_item_failures<\/span><span>}<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>By returning the <code>itemIdentifier<\/code> of the failed messages, SQS will only keep those specific messages in the queue, while the successful ones are deleted. This prevents duplicate processing and ensures only the &#8220;poison&#8221; entries eventually hit the DLQ.<\/p>\n<h2> <a name=\"step-3-handling-poison-messages-in-streaming-services\" href=\"#step-3-handling-poison-messages-in-streaming-services\"> <\/a> Step 3: Handling Poison Messages in Streaming Services <\/h2>\n<p>When moving from queue-based to stream-based processing with Amazon Kinesis Data Streams or Amazon MSK, the &#8220;poison message&#8221; concept changes. You cannot simply &#8220;delete&#8221; a message from a stream. Instead, you must use a &#8220;discard and continue&#8221; or a &#8220;side-output&#8221; pattern.<\/p>\n<p>For Amazon Managed Service for Apache Flink, you can use a <code>SideOutput<\/code> to route malformed records to a separate destination (like an S3 bucket or a different Kinesis stream) without stopping the main application pipeline. <\/p>\n<div>\n<pre><code><span>\/\/ Conceptual Java snippet for Flink<\/span> <span>OutputTag<\/span><span>&lt;<\/span><span>String<\/span><span>&gt;<\/span> <span>deadLetterTag<\/span> <span>=<\/span> <span>new<\/span> <span>OutputTag<\/span><span>&lt;<\/span><span>String<\/span><span>&gt;<\/span><span>(<\/span><span>\"dead-letters\"<\/span><span>)<\/span><span>{<\/span><span>}<\/span><span>;<\/span> <span>SingleOutputStreamOperator<\/span><span>&lt;<\/span><span>MyData<\/span><span>&gt;<\/span> <span>processedStream<\/span> <span>=<\/span> <span>inputStream<\/span><span>.<\/span><span>process<\/span><span>(<\/span><span>new<\/span> <span>ProcessFunction<\/span><span>&lt;<\/span><span>String<\/span><span>,<\/span> <span>MyData<\/span><span>&gt;<\/span><span>(<\/span><span>)<\/span> <span>{<\/span> <span>@Override<\/span> <span>public<\/span> <span>void<\/span> <span>processElement<\/span><span>(<\/span><span>String<\/span> <span>value<\/span><span>,<\/span> <span>Context<\/span> <span>ctx<\/span><span>,<\/span> <span>Collector<\/span><span>&lt;<\/span><span>MyData<\/span><span>&gt;<\/span> <span>out<\/span><span>)<\/span> <span>{<\/span> <span>try<\/span> <span>{<\/span> <span>out<\/span><span>.<\/span><span>collect<\/span><span>(<\/span><span>parse<\/span><span>(<\/span><span>value<\/span><span>)<\/span><span>)<\/span><span>;<\/span> <span>}<\/span> <span>catch<\/span> <span>(<\/span><span>Exception<\/span> <span>e<\/span><span>)<\/span> <span>{<\/span> <span>ctx<\/span><span>.<\/span><span>output<\/span><span>(<\/span><span>deadLetterTag<\/span><span>,<\/span> <span>value<\/span><span>)<\/span><span>;<\/span> <span>}<\/span> <span>}<\/span> <span>}<\/span><span>)<\/span><span>;<\/span> <span>processedStream<\/span><span>.<\/span><span>getSideOutput<\/span><span>(<\/span><span>deadLetterTag<\/span><span>)<\/span><span>.<\/span><span>addSink<\/span><span>(<\/span><span>new<\/span> <span>S3Sink<\/span><span>(<\/span><span>)<\/span><span>)<\/span><span>;<\/span> <\/code><\/pre>\n<div>\n<\/p><\/div>\n<\/p><\/div>\n<p>This ensures that the main stream processor does not block on bad data while providing an audit trail for the poisoned records.<\/p>\n<p><a href=\"https:\/\/media2.dev.to\/dynamic\/image\/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto\/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1666148670142-2f01b117e6e0%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fit%3Dmax%26fm%3Djpg%26ixid%3DM3w5NzU0MjJ8MHwxfHNlYXJjaHwyfHxkYXRhJTIwZmxvdyUyMGRpYWdyYW18ZW58MHwwfHx8MTc5NDc4MDEzNnww%26ixlib%3Drb-4.1.0%26q%3D80%26w%3D1080\"><\/a><br \/> <em>Photo by <a href=\"https:\/\/unsplash.com\/@kenny161616?utm_source=articles_pipeline&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener noreferrer\">kenny cheng<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=articles_pipeline&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener noreferrer\">Unsplash<\/a><\/em><\/p>\n<h2> <a name=\"common-pitfalls\" href=\"#common-pitfalls\"> <\/a> Common Pitfalls <\/h2>\n<ul>\n<li> <strong>Infinite Loops:<\/strong> If your DLQ consumer also fails to process the poison message and triggers a retry, you can end up with a recursive loop where messages move between queues. Ensure your DLQ consumer has robust exception handling that specifically catches schema errors and moves them to a &#8220;parking lot&#8221; storage (like S3) instead of attempting to re-process.<\/li>\n<li> <strong>Visibility Timeout Mismatch:<\/strong> If your Lambda execution time exceeds the SQS <code>visibility_timeout<\/code>, SQS will assume the function failed and put the message back in the queue even if the function is still working. Ensure <code>visibility_timeout<\/code> is at least 6 times your Lambda <code>timeout<\/code> setting.<\/li>\n<li> <strong>Ignoring Batch Metadata:<\/strong> When processing batches, ensure you are using the correct metadata from the event object. Relying on global state or shared variables within a Lambda execution context during batch processing is a common source of non-deterministic behavior.<\/li>\n<\/ul>\n<h2> <a name=\"conclusion\" href=\"#conclusion\"> <\/a> Conclusion <\/h2>\n<p>Poison messages are a diagnostic signal that your system needs better validation or error handling. By using SQS DLQs, implementing partial batch failure reporting, and utilizing side-outputs in streaming applications, you transform these failures from production outages into manageable data quality tasks.<\/p>\n<p><strong>Try it:<\/strong> Create an SQS queue with a DLQ attached. Send a dummy message that triggers an exception in your code. Verify that the message moves to the DLQ after the <code>maxReceiveCount<\/code> is reached, and then inspect the message body using the AWS CLI:<br \/> <code>aws sqs receive-message --queue-url &lt;dlq-url&gt; --max-number-of-messages 1<\/code><\/p>\n<p><strong>Clean up:<\/strong> Delete the SQS queues and any associated Lambda functions to avoid recurring costs.<br \/> <code>aws sqs delete-queue --queue-url &lt;primary-queue-url&gt;<\/code><br \/> <code>aws sqs delete-queue --queue-url &lt;dlq-url&gt;<\/code><\/p>\n<p><em>Cover photo by <a href=\"https:\/\/unsplash.com\/@trhammerhead?utm_source=articles_pipeline&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener noreferrer\">Bryson Hammer<\/a> on <a href=\"https:\/\/unsplash.com\/?utm_source=articles_pipeline&amp;utm_medium=referral\" target=\"_blank\" rel=\"noopener noreferrer\">Unsplash<\/a>.<\/em><\/p>\n<hr>\n<p><em>This article was drafted with AI assistance and is intended for technical review and fact-checking before publication.<\/em><\/p>\n<\/p><\/div>\n<\/div>\n<\/div>\n<\/div>\n<p>Fuente: <a href=\"https:\/\/dev.to\/aniketsoni\/handling-poison-messages-in-aws-event-driven-pipelines-jnd\">Art\u00edculo original<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>By offloading the message to the dlq after three attempts, you ensure the primary consumer remains healthy and available for subsequent, valid messages. Step 2: Implementing Idempotent Consumers Moving a message to a DLQ is only half the battle. If your consumer fails partially through a batch process, you risk processing the same message twice. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7043,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"webixso_pending_account_ids":""},"categories":[41],"tags":[],"class_list":["post-7044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devto"],"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/7044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/comments?post=7044"}],"version-history":[{"count":0,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/posts\/7044\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media\/7043"}],"wp:attachment":[{"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/media?parent=7044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/categories?post=7044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tucumandevelopers.com\/index.php\/wp-json\/wp\/v2\/tags?post=7044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}