# Exercise 1: A Work Queue With a DLQ

```
STEP 1. Create a DLQ `thumbnails-dlq` with 14-day retention.
STEP 2. Create `thumbnails` (Standard): visibility timeout sized for a job whose
        p99 is 40 s, long polling 20 s, 4-day retention, redrive to the DLQ after
        5 receives.
STEP 3. Write a worker loop (pseudo-code or TypeScript with @aws-sdk/client-sqs):
        long-poll up to 10 messages, process each, delete only on success, extend
        visibility for jobs that run long, never delete on failure.
STEP 4. Create a CloudWatch alarm that fires when the DLQ has ≥ 1 visible message.
STEP 5. Explain (comment): what happens to a message whose processing crashes the
        worker 5 times in a row, step by step.
```

Config + code exercise — verify with the checklist.

## Queues (TODO)

```bash
aws sqs create-queue --queue-name thumbnails-dlq --attributes MessageRetentionPeriod=TODO
DLQ_ARN=$(aws sqs get-queue-attributes --queue-url $DLQ_URL --attribute-names QueueArn --query Attributes.QueueArn --output text)

aws sqs create-queue --queue-name thumbnails --attributes '{
  "VisibilityTimeout": "TODO",
  "ReceiveMessageWaitTimeSeconds": "TODO",
  "MessageRetentionPeriod": "TODO",
  "RedrivePolicy": "{\"deadLetterTargetArn\":\"TODO\",\"maxReceiveCount\":\"TODO\"}"
}'
```

## Worker (TODO)

```ts
// import { SQSClient, ReceiveMessageCommand, DeleteMessageCommand, ChangeMessageVisibilityCommand } from "@aws-sdk/client-sqs";
// while (!shuttingDown) {
//   TODO: receive (MaxNumberOfMessages 10, WaitTimeSeconds 20)
//   TODO: for each message: process; on success delete; on failure do nothing
//   TODO: for long jobs: heartbeat ChangeMessageVisibility before the timeout
// }
```

## Alarm (TODO)

```bash
aws cloudwatch put-metric-alarm --alarm-name thumbnails-dlq-not-empty \
  --namespace AWS/SQS --metric-name TODO --dimensions Name=QueueName,Value=thumbnails-dlq \
  --statistic Maximum --period 60 --evaluation-periods 1 --threshold TODO \
  --comparison-operator TODO --alarm-actions $SNS_ONCALL_TOPIC
```

## STEP 5 answer (TODO)

```
#
```

## Requirements Checklist

- [ ] DLQ retention 1,209,600 s (14 days), longer than the source queue's retention.
- [ ] Visibility timeout comfortably above p99 (e.g. 120–240 s for p99 40 s), long polling 20 s, retention 345,600 s.
- [ ] RedrivePolicy with the DLQ ARN and `maxReceiveCount` 5.
- [ ] Worker deletes only after successful processing, extends visibility for long jobs, and stops receiving on shutdown (finishing in-flight work).
- [ ] Alarm on `ApproximateNumberOfMessagesVisible` ≥ 1 (`GreaterThanOrEqualToThreshold`).
- [ ] STEP 5: each crash → message reappears after the visibility timeout → receive count increments → on the 6th receive attempt SQS moves it to the DLQ instead of delivering it.
