Skip to content

Lesson 2 of 9 · 23m

Envelopes, Visibility Timeouts, and Acknowledgements

Design message envelopes and lease handling so crashes, slow workers, and retries are explicit rather than accidental.

Most queue incidents are not mysterious broker failures. They are lease mismatches: a worker is still thinking, the queue thinks it died, another worker picks up the same message, and now two consumers are mutating the same order. You need to know exactly when a message is invisible, when it becomes visible again, and what acknowledgement really commits.

Separate envelope from payload

The payload is the business fact. The envelope is everything the worker needs to handle it safely:

{
  "id": "01J9ZK5V0B4T4P7S5H7M0HCEEW",
  "type": "invoice.finalized",
  "occurred_at": "2026-11-30T18:02:11Z",
  "tenant_id": "tn_8hf31",
  "aggregate_id": "inv_10492",
  "payload": {"invoice_id": "inv_10492", "currency": "usd"},
  "trace_id": "b3f8b3a60c884ef9",
  "schema": 2
}

Useful fields are stable message ID, type, schema version, tenant, aggregate or entity ID, timestamps, trace context, and enough payload to perform the job. Do not put mutable large objects in the message when a reference plus a fresh read is safer.

Visibility timeout is a lease, not a deadline oracle

In SQS-like systems, a received message becomes invisible for a fixed period:

aws sqs receive-message   --queue-url "$QUEUE_URL"   --visibility-timeout 30   --wait-time-seconds 20   --max-number-of-messages 5

If processing sometimes takes longer than 30 seconds, either raise the timeout or extend the lease while work continues. If your worker never heartbeats long jobs, duplicates are self-inflicted.

def process(message):
    stop = heartbeat(message, every_seconds=10)
    try:
        handle(message.body)
        message.delete()
    finally:
        stop.set()

The exact API differs by client library, but the shape is universal: receive, maintain lease, perform effect, acknowledge, stop heartbeating.

Acknowledgement has a transaction boundary

Acking means "this consumer will never need this message again." It should happen only after the durable effect is committed. In database-backed work, commit the business update and acknowledgement together when possible. With an external broker, use an idempotency record so redelivery after a crash is safe.

Negative acknowledgement is equally important. nack, requeue, release, or change_visibility decides whether failure retries immediately or after delay. Accidentally requeueing a poison message hot-loops your fleet.

Payload size and freshness

Queues are not object stores. A 10 MB message slows every consumer and encourages stale snapshots. Store large artifacts in object storage and send a reference. For mutable entities, include the ID and expected version, then read current state in the worker.

Field notes

  • A timeout slightly longer than average processing time is not enough. Measure p95 and p99 duration, then leave room for downstream degradation.
  • Heartbeats that run in the same blocked thread are decorative. The lease extender must actually run while work is running.
  • Deleting a message before side effects is at-most-once processing whether or not the broker advertises it.
  • Include a schema version before you need it. The first incompatible payload change is much easier when old workers can reject cleanly.

Was this lesson helpful?

Sign in to rate