[It's ridiculous to set the setting value to zero!] I receive the same message multiple times with SQS.

While implementing the process of polling SQS, I encountered an event where ** the same message was received ** at the same time.

The explanation code is Python, but it is thought that the event will occur in other languages / SDKs.

What happened

Below is the Python code to get up to 10 messages from the SQS queue.

It is assumed that only one message is stored in the target queue.


import boto3

#Send request to SQS
client = boto3.client('sqs')
messages = client.receive_messages(QueueUrl=my-url, MaxNumberOfMessages=10).get('Messages')

Display the received message.

for i, message in enumerate(messages):
    print(i)
    print('MessageId: ' + message['MessageId'])
    print('MD5: ' + message['MD5OfBody'])

# 0
# MessageId: 7d4f2923-****-4d86-87a6-85f20446086a
# MD5: 38d2b0ed81e0200*********
# 1
# MessageId: 7d4f2923-****-4d86-87a6-85f20446086a
# MD5: 38d2b0ed81e0200*********
# 2
# MessageId: 7d4f2923-****-4d86-87a6-85f20446086a
# MD5: 38d2b0ed81e0200*********

With this feeling, I got ** message ID and 3 items with exactly the same body **. The number of cases to be acquired at the same time may be one or two.

Cause and countermeasure (guess)

Occurs when ** Visibility Timeout ** is set to 0 in the queue settings. This was resolved by changing it to an appropriate value greater than 0.

Amazon SQS Visibility Timeout

SQS is a managed message queuing service that distributes message data behind the scenes to increase availability. If this visibility timeout is disabled, it is likely that all SQS servers have responded to the polling request.

Since it was a simple configuration that did not consider the scale in particular, it was set appropriately as "** Yeah! This option is invalid! **". .. ..

Setting method

From the console screen

Go to [Queue Operation]> [Queue Settings] ↓ Here (image.png )

Polling request

You can send a request with options, but I think it's safer to change the settings for the entire queue.

Python SDK (boto3) example

#Send request to SQS
client = boto3.client('sqs')
messages = client.receive_messages(QueueUrl=my-url, VisibilityTimeout=30)

Recommended Posts

[It's ridiculous to set the setting value to zero!] I receive the same message multiple times with SQS.
How to register the same data multiple times with one input on the Django management screen