Sub topic 6
Delay queues, message timers, and retention period
This chapter assumes you can create an SQS queue and send, receive, and delete messages with boto3. It also assumes you know what a visibility timeout is. Here you will control two things: when a message first becomes visible, and how long SQS keeps it.
Delaying delivery
Sometimes a message should not be processed right away. Examples are a reminder email that goes out in five minutes, or a short wait so a database write can settle before a consumer reads it. SQS gives you two ways to delay a message.
- Delay queue. You set the
DelaySecondsattribute on the queue. Every new message stays invisible for that many seconds after it is sent. - Message timer. You set
DelaySecondson a singlesend_messagecall. That value overrides the queue's setting for that message.
The delay can be 0 to 900 seconds (15 minutes). The default is 0.
Do not confuse a delay with a visibility timeout. A delay hides a message when it first arrives. A visibility timeout hides a message after a consumer has received it.
Setting a delay
The example below creates a queue with a 30-second default delay and then sends two messages. The first uses the queue default. The second uses a message timer of 120 seconds.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.create_queue(
QueueName="reminders",
Attributes={"DelaySeconds": "30"},
)["QueueUrl"]
# Uses the queue default: visible after 30 seconds
sqs.send_message(QueueUrl=queue_url, MessageBody="default delay")
# Message timer: visible after 120 seconds
sqs.send_message(
QueueUrl=queue_url,
MessageBody="two minute delay",
DelaySeconds=120,
)Message timers are not available on FIFO queues. A FIFO queue accepts a queue-level delay only, because a per-message delay would break ordering within a message group.
Retention period
The retention period is how long SQS keeps a message that has not been deleted. Set it with the MessageRetentionPeriod attribute, in seconds.
| Setting | Value |
|---|---|
| Default | 345,600 seconds (4 days) |
| Minimum | 60 seconds |
| Maximum | 1,209,600 seconds (14 days) |
When the period ends, SQS deletes the message, whether or not any consumer received it. Retention is counted from the time the message was first sent. Receiving a message does not reset it.
This matters for dead-letter queues. A message keeps its original send time when it moves to a dead-letter queue, so it arrives with less retention time left. Give the dead-letter queue a longer retention period than the source queue, for example 14 days against 4, so you have time to investigate.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="reminders")["QueueUrl"]
sqs.set_queue_attributes(
QueueUrl=queue_url,
Attributes={"MessageRetentionPeriod": "1209600"}, # 14 days
)Common errors
- Expecting a long delay: the maximum is 900 seconds. For longer waits, use a scheduler service or store a due time and check it in the consumer.
- Passing
DelaySecondsper message to a FIFO queue. SQS rejects the request. - Finding messages missing after several days because the retention period was shorter than the time the consumers were down.
- Reading a delay as a processing timeout. A delay does not give a consumer more time. Use the visibility timeout for that.
Exercise
A message is sent to a standard queue with a 60-second queue delay and a per-message DelaySeconds of 10. The queue's retention period is 4 days. When does the message become visible, and when does SQS delete it if no one receives it?
Show answer
It becomes visible after 10 seconds, because the message timer overrides the queue delay. SQS deletes it 4 days after it was sent, since retention is counted from the send time.