Why asynchronous?
Go async when the work is slow (video encoding), unreliable (third-party APIs), bursty (flash sales), fan-out (one event, many consumers), or simply not needed for the response.
A restaurant ticket rail. The waiter clips the order and goes back to the customers; cooks pick tickets at their own pace. A rush of orders makes the rail longer, not the waiter slower.
Two messaging models
| Point-to-point (work queue) | Publish/subscribe | |
|---|---|---|
| Who receives | One consumer per message | Every subscriber group gets a copy |
| Use for | Distributing tasks across workers | Broadcasting events to many services |
| Examples | SQS, RabbitMQ queues, Celery | SNS, Kafka topics, Google Pub/Sub, RabbitMQ fanout |
Delivery guarantees
| Guarantee | Meaning | How | Risk |
|---|---|---|---|
| At-most-once | Delivered 0 or 1 times | Ack before processing | Lost messages |
| At-least-once | Delivered 1+ times | Ack after processing; redeliver on timeout | Duplicates |
| Exactly-once (effectively) | Effect happens once | At-least-once + idempotent consumer, or transactional processing | Complexity |
"Delivery is at-least-once, and every consumer is idempotent: it records processed message IDs in the same transaction as its side effect, so redeliveries are harmless. That gives us effectively-once processing without a distributed transaction."
The lifecycle of a message
Key mechanisms:
- Ack / nack: a message is deleted only after the consumer confirms success.
- Visibility timeout / lease: a delivered message is hidden for a period; if not acked in time, it's redelivered. Set it longer than the worst-case processing time, or extend it while working.
- Retries with backoff: transient failures retry after 1 s, 4 s, 16 s… with jitter.
- Dead-letter queue (DLQ): after N failed attempts, park the message for inspection instead of retrying forever (a "poison message" can otherwise block a queue indefinitely).