At-Least-Once Delivery
A delivery guarantee where messages are delivered one or more times, requiring idempotent handling.
At-least-once delivery means the messaging system guarantees every message will be delivered to the consumer at least one time. It might be delivered more than once if something goes wrong (network issues, consumer crashes before acknowledging, etc.).
This is the most common delivery guarantee in message brokers like Kafka, Google Pub/Sub, and RabbitMQ. It provides a good balance between reliability and performance.
The implication is that your event handlers must be idempotent: processing the same event twice should produce the same result as processing it once. Common strategies include:
- Tracking processed event IDs in the database
- Using idempotency keys in API calls
- Making operations naturally idempotent (e.g., setting a value rather than incrementing)
The alternative, exactly-once delivery, is either impossible or very difficult in distributed systems. At-least-once delivery with idempotent handlers is the practical approach.
References
- Live website updates with Go, SSE, and htmx — When working with virtually any Pub/Sub, you must be aware of the at-least-once delivery guarantee. You may receive the same message twice because of network issues or your server going down at the wrong moment. Embrace that this can happen and design your handlers to be idempotent.
- Durable Background Execution with Go and SQLite — At-least-once atomic execution is theoretically facilitated by Watermill, but there are subtle ways of subverting it. Always acknowledge the message after all the business logic finishes, and ensure that business logic is granular enough that all of it runs anew in case of an interruption.
- Event-Driven Architecture: The Hard Parts — Most message brokers offer at-least-once delivery semantics, meaning you can receive every message at least once but possibly multiple times. This is connected to what happens if your consumer loses network connection in the middle of processing the message.