title: AWS SQS - Queue not delivering any messages until Visibility Timeout expires for one message
body: <p><strong>EDIT</strong>: <em>Solved this one while I was writing it up :P -- I love those kind of solutions.  I figured I'd post it anyway, maybe someone else will have the same problem and find my solution.  Don't care about points/karma, etc. I just already wrote the whole thing up, so figured I'd post it and the solution.</em></p>
  
  <p>I have an SQS FIFO queue.  It is using a dead letter queue.  Here is how it had been configured:</p>
  
  <p><a href="https://i.stack.imgur.com/E5edM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/E5edM.png" alt="FIFO Queue Configuration"></a></p>
  
  <p>I have a single producer microservice, and I have 10 ECS images that are running as consumers.</p>
  
  <p>It is important that we process the messages close to the time they are delivered in the queue for business reasons.</p>
  
  <p>We're using a fairly recent version of the AWS SDK Golang client package for both producer and consumer code (if important, I can go look up the version, but it is not terribly outdated).</p>
  
  <p>I capture the logs for the producer so I know exactly when messages were put in the queue and what the messages were.</p>
  
  <p>I capture aggregate logs for all the consumers, so I have a full view of all 10 consumers and when messages were received and processed.</p>
  
  <p>Here's what I see under normal conditions looking at the logs:</p>
  
  <ol>
  <li>Message put in the queue at time x</li>
  <li>Message received by one of the 10 consumers at time x</li>
  <li>Message processed by consumer successfully</li>
  <li>Message deleted from queue by consumer at time x + (0-2 seconds)</li>
  <li>Repeat ad infinitum for up to about 700 messages / day at various times per day</li>
  </ol>
  
  <p>But the problem I am seeing now is that some messages are not being processed in a timely manner.  Occasionally we fail processing a message deliberately b/c of the state of the system for that message (e.g. maybe users still logged in, so it should back off and retry...which it does).  The problem is if the consumer fails a message it is causing the queue to stop delivering <em>any</em> other messages to any other consumers.</p>
  
  <p>"Failure to process a message" here just means the message was received, but the consumer declared it a failure, so we just log an error, and do not proceed to delete it from the queue.  Thus, the visibility timeout (here 5m) will expire and it will be re-delivered to another consumer and retried up to 10 times, after which it will go to the dead letter queue.</p>
  
  <p>After delving into the logs and analyzing it, here's what I'm seeing:</p>
  
  <ol>
  <li>Process begins like above (message produced, consumed, deleted).</li>
  <li>New message received at time x by consumer</li>
  <li>Consumer fails -- logs error and just returns (does not delete)</li>
  <li>Same message is received again at time x + 5m (visibility timeout)</li>
  <li>Consumer fails -- logs error and just returns (does not delete)</li>
  <li>Repeat up to 10x -- message goes to dead-letter queue</li>
  <li>New message received but it is now 50 minutes late!</li>
  <li>Now all messages that were put in the queue between steps 2-7 are 50 minutes late (5m visibility timeout * 10 retries)</li>
  </ol>
  
  <p>All the docs I've read tells me the queue should <em>not</em> behave this way, but I've verified it several times in our logs.  Sadly, we don't have a paid AWS support plan, or I'd file a ticket with them.  But just consider the fact that we have 10 separate consumers all reading from the same queue.  They only read from this queue.  We don't have any other queues it is using.  </p>
  
  <p>For de-duplication we are using the automated hash of the message body.  Messages are small JSON documents.</p>
  
  <p>My expectation would be if we have a single bad message that causes a visibility timeout, that the queue would still happily deliver any other messages it has available while there are available consumers.</p>