Skip to content

Lesson 5 of 9 · 24m

Queues, Streams, and Backpressure

Batching, visibility timeouts, shard ordering, concurrency controls, and the mechanics that decide whether asynchronous pipelines degrade gracefully or melt down.

Asynchronous systems rarely fail fast. They fail quietly: a queue grows for six hours while a dependency is down, then somebody notices customers waiting. The craft is controlling batching, retries, concurrency, and ordering so pressure accumulates where you can see it.

SQS visibility timeout is a lease

Messages become invisible while processed. If the lease expires before processing finishes, another invocation can receive the same work:

WorkerQueue:
  Type: AWS::SQS::Queue
  Properties:
    VisibilityTimeout: 180

WorkerFunction:
  Type: AWS::Serverless::Function
  Properties:
    Timeout: 30
    Events:
      Queue:
        Type: SQS
        Properties:
          Queue: !GetAtt WorkerQueue.Arn
          BatchSize: 10

A 30-second function timeout with a 30-second visibility timeout is not safe; the function may need the entire timeout for a batch.

Partial batch failure matters

By default, one failure can retry the entire batch. For SQS, return only failed message IDs:

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

Configure FunctionResponseTypes: ["ReportBatchItemFailures"]. Without that, one bad message forces good messages through repeated side effects.

Streams are ordered by shard

Kinesis and DynamoDB Streams preserve ordering within a shard. That enables correct projections but couples throughput to shard count and lets one failing record block progress. Choose partition keys deliberately: aggregate ID such as order ID when ordering matters, high-cardinality keys when parallelism matters more.

Backpressure is intentional congestion

The usual tools are queue depth, reserved concurrency, smaller batch sizes, batch windows, worker-side rate limits, and dead-letter queues. The point is not to eliminate pressure; it is to hold pressure somewhere durable instead of inside a fragile dependency.

A useful health sketch:

healthy:  visible messages low, oldest message age low
degraded: visible messages rising, oldest age rising
danger:   DLQ depth increasing, stream iterator age rising

ApproximateAgeOfOldestMessage is usually more actionable than raw queue count. Ten thousand tiny messages may be fine; a 20-minute-old message often means users are waiting.

Field notes

  • Larger batches are not automatically better; they increase blast radius, memory use, and retry cost.
  • Alarm on DLQ depth and oldest-message age before launch.
  • Redrive DLQs gradually after a fix; replay itself can create a second outage.
  • FIFO queues buy ordering and deduplication at the cost of throughput and key design. Do not choose them merely because ordering sounds safer.

Was this lesson helpful?

Sign in to rate