Part 5 of the Serverless series.

Two of the oldest AWS services are still two of the most useful. Amazon SQS (Simple Queue Service) holds messages until a consumer is ready for them. Amazon SNS (Simple Notification Service) pushes each message to every subscriber at once.

SQS: a buffer between producer and consumer

A producer sends a message to a queue; a consumer receives it, processes it, and deletes it. If the consumer is slow, busy or down, messages simply wait, for up to 14 days.

That one property solves a lot of problems:

  • Load levelling: a burst of 10,000 orders doesn’t need 10,000 concurrent database writes. The queue absorbs the spike and the consumer works through it.
  • Resilience: a failing downstream system doesn’t lose work; messages are retried.
  • Decoupling: the producer doesn’t wait for, or even know about, the consumer.

Visibility timeout

When a consumer receives a message, it becomes invisible to other consumers for the visibility timeout. If it isn’t deleted in that time (because the consumer crashed or threw), it reappears and is retried. For Lambda consumers, set the visibility timeout to at least six times the function’s timeout.

Dead-letter queues

After a set number of failed receives (maxReceiveCount), SQS moves a message to a dead-letter queue (DLQ) instead of retrying forever. Always configure one, alarm when it isn’t empty, and use DLQ redrive to send messages back once you’ve fixed the bug.

Standard vs FIFO

  • Standard: nearly unlimited throughput, at-least-once delivery, best-effort ordering.
  • FIFO: strict ordering within a message group and exactly-once processing within a deduplication window, with lower throughput. Use it when order matters, such as events for one account or one game.

Lambda + SQS

Lambda polls the queue for you and invokes your function with batches of messages. By default, if one message in a batch fails, the whole batch is retried. Turn on partial batch responses and return only the failures:

def handler(event, context):
    failures = []
    for record in event["Records"]:
        try:
            process(record["body"])
        except Exception:
            failures.append({"itemIdentifier": record["messageId"]})
    return {"batchItemFailures": failures}

In SAM, add FunctionResponseTypes: [ReportBatchItemFailures] to the SQS event. Use maximum concurrency on the event source to cap how many functions process the queue at once, protecting whatever is downstream.

SNS: fan-out

An SNS topic delivers each published message to all its subscriptions: SQS queues, Lambda functions, HTTPS endpoints, email, SMS and mobile push. Subscription filter policies let each subscriber receive only the messages it cares about.

The classic pattern is SNS → multiple SQS queues: one event, several independent consumers, each with its own queue, retries and DLQ, none able to slow the others down.

Hands-on

  1. Create an SQS queue with a DLQ (maxReceiveCount: 3).
  2. Attach a Lambda function that throws for any message containing "fail".
  3. Send a handful of messages, including a failing one, and watch the good ones disappear while the bad one lands in the DLQ after three attempts.
  4. Fix the function and redrive the DLQ from the console.

Gotchas

  • Make consumers idempotent. At-least-once means duplicates will happen eventually.
  • Message size is limited (1 MiB for SQS, 256 KB for SNS). Store large payloads in S3 and send a pointer: the claim check pattern.
  • SNS → SQS needs a queue policy allowing the topic to send messages; SAM and CDK handle it, hand-written templates often forget.

Next: Step Functions: workflows as state machines.

← Series overview