Sub topic 6
Delay queues, message timers, and retention period
This chapter covers two settings that control when a message becomes available and one that controls how long it survives. You should already know how to send, receive, and delete messages, and how the visibility timeout works. Here you will learn to postpone delivery, to choose between a queue-wide delay and a per-message timer, and to set a retention period that matches your recovery needs.
Three clocks on every message
A message in SQS can be waiting on three separate timers. They are easy to confuse, so keep them apart.
| Setting | Scope | Starts when | Effect | Range |
|---|---|---|---|---|
| Delay (`DelaySeconds` on the queue) | Every new message | The message is sent | Message is invisible to consumers | 0 to 900 seconds |
| Message timer (`DelaySeconds` on a send) | One message | The message is sent | Same as delay, for one message | 0 to 900 seconds |
| Visibility timeout | One receive | A consumer receives the message | Hides the message while it is processed | 0 seconds to 12 hours |
| Retention period | Every message | The message is sent | SQS deletes the message when it expires | 60 seconds to 14 days |
The delay applies once, before the first receive. The visibility timeout applies after every receive. Retention runs from the first send and never restarts.
Delay queues
A delay queue is an ordinary queue whose DelaySeconds attribute is greater than zero. Every message sent to it stays invisible for that many seconds. The default is 0, and the maximum is 900 seconds (15 minutes).
Typical uses are giving a database write time to replicate before a consumer reads it, and holding a "send a reminder" job for a short while.
import boto3
sqs = boto3.client("sqs")
# 30 s delay for every message, 1 day retention
queue_url = sqs.create_queue(
QueueName="reminders",
Attributes={
"DelaySeconds": "30",
"MessageRetentionPeriod": "86400",
},
)["QueueUrl"]
print(queue_url)Attribute values are always strings, even for numbers. Calling create_queue again with the same name but different attributes fails with QueueNameExists. To change a queue that already exists, use set_queue_attributes.
Message timers
A message timer sets the delay for a single message by passing DelaySeconds to send_message. On a standard queue, the per-message value overrides the queue's delay. That works in both directions: a timer of 0 makes a message visible immediately, even on a queue with a 30-second delay.
import boto3
sqs = boto3.client("sqs")
queue_url = sqs.get_queue_url(QueueName="reminders")["QueueUrl"]
# Hidden for 120 s, overriding the queue's 30 s delay
sqs.send_message(
QueueUrl=queue_url,
MessageBody="send follow-up email",
DelaySeconds=120,
)
# Visible immediately, overriding the queue's 30 s delay
sqs.send_message(
QueueUrl=queue_url,
MessageBody="urgent: send now",
DelaySeconds=0,
)
attrs = sqs.get_queue_attributes(
QueueUrl=queue_url,
AttributeNames=[
"ApproximateNumberOfMessages",
"ApproximateNumberOfMessagesDelayed",
],
)["Attributes"]
print(attrs)Right after the two sends, ApproximateNumberOfMessagesDelayed should show the delayed message and ApproximateNumberOfMessages the immediate one. These counts are approximate, so a short lag is normal.
FIFO queues are different
FIFO queues support only the queue-level delay. Sending a message with its own DelaySeconds to a FIFO queue returns an InvalidParameterValue error. The reason is ordering. A timer on one message could let a later message in the same group overtake it.
The two queue types also differ when you change the queue delay on a queue that already holds messages. On a standard queue, messages already in the queue keep their old delay. On a FIFO queue, the new delay applies to them as well.
Retention period
MessageRetentionPeriod is how long SQS keeps a message that has not been deleted. The default is 345,600 seconds (4 days). You can set it from 60 seconds to 1,209,600 seconds (14 days). After that, SQS removes the message without notice. The message does not go to a dead-letter queue.
Points that cause trouble in practice:
- The clock starts at the first send. Receiving a message, returning it through a visibility timeout, or delaying it does not restart the clock. A message with a 15-minute timer has 15 minutes less retention to use.
- A dead-letter queue inherits the original timestamp. When SQS moves a message to a dead-letter queue because of the redrive policy, the message keeps its original enqueue time. If both queues keep messages for 4 days and a message fails on day 3, it has one day left in the dead-letter queue. Give the dead-letter queue a longer retention period than the source queue, typically 14 days.
- A shorter retention drops old messages. If you lower the setting, messages older than the new value are deleted. Treat it as a data-loss change.
- Retention is a safety net, not a schedule. A consumer that falls behind by more than the retention period loses data. Watch message age (covered in the monitoring chapter) and alarm well before the limit.
import boto3
sqs = boto3.client("sqs")
dlq_url = sqs.get_queue_url(QueueName="orders-dlq")["QueueUrl"]
# Keep failed messages for 14 days so there is time to investigate
sqs.set_queue_attributes(
QueueUrl=dlq_url,
Attributes={"MessageRetentionPeriod": "1209600"},
)Choosing values
- Use delay only when the consumer truly needs to wait. A delay adds latency to every message in the queue.
- Prefer a message timer when only some messages need to wait, and the queue is standard.
- Set retention to cover your longest expected outage plus time to fix it. A 4-day default can expire messages over a long weekend.
- Do not use the delay to space out retries. A failed message returns through the visibility timeout. Tune that instead.
Exercise
A standard queue has DelaySeconds set to 60 and MessageRetentionPeriod set to 600. You send a message with DelaySeconds=0. When can a consumer first receive it, and how long does it exist if nobody receives it?
Show answer
A consumer can receive it immediately, because the per-message timer of 0 overrides the queue's 60-second delay on a standard queue. If nobody receives it, SQS deletes it 600 seconds after it was sent.
Exercise
A message is sent to a standard queue with a 4-day retention period and moves to a dead-letter queue, also set to 4 days, after 3 days of failed receives. How long can you inspect it in the dead-letter queue, and what change fixes the problem?
Show answer
About one day, because the message keeps its original enqueue timestamp. Set the dead-letter queue's MessageRetentionPeriod to a larger value, such as 1,209,600 seconds (14 days).
Exercise
Your code calls send_message with DelaySeconds=30 on a queue named jobs.fifo and gets InvalidParameterValue. What are two ways to get a 30-second delay?
Show answer
Set DelaySeconds=30 as a queue attribute with setqueueattributes, because FIFO queues allow only a queue-level delay. Alternatively, use a standard queue and keep the per-message timer, if you can give up FIFO ordering.