Retries Are Not Free: Idempotency in Event-Driven Systems
At-least-once delivery is the contract on every managed messaging service in AWS, and most consumers are written as if it isn't. This article covers where duplicates actually come from in production, why a dedup table with a check-then-write isn't idempotency, and why the write itself has to be the check. Also covers the batch retry trap where one bad message replays nine good ones, the timeout-versus-visibility-timeout relationship that causes concurrent processing with no failure involved,
Every managed messaging service on AWS delivers at least once. SQS, SNS, EventBridge, Kinesis, Lambda's own retry on async invokes. This is documented clearly and still manages to surprise people in production, because the duplicate usually arrives hours later, after the code has moved on and everyone has stopped thinking about it.
The awkward part is that duplicates aren't a service failure. They're the contract. The service is doing what it promised. The gap is that most consumers are written as if delivery happens exactly once, and that assumption only holds until it doesn't.
Where the duplicates actually come from
People picture a duplicate as something exotic. In practice it's mundane:
A visibility timeout expires before the consumer finishes, so SQS hands the message to someone else while the first consumer is still working. A Lambda times out after it already wrote to the database. A network call succeeds on the server side and fails on the client side, so the client retries something that already happened. A batch of ten messages fails on one, and the entire batch gets retried, replaying the nine that succeeded. A DLQ gets redriven after a fix.
None of these are rare. Most of them are expected behavior under load.
Check-then-write is not idempotency
The most common attempt I see is a dedup table. The consumer looks up the message ID, sees it's new, does the work, then records the ID. That's a check-then-write, and under concurrency two consumers can both pass the check before either writes.
Real idempotency means the write itself is the check. A conditional write that fails if the key exists, a state transition guarded on the current state, a unique constraint. The operation has to be safe to run twice, not just guarded by a lookup that ran once.
The second most common mistake is a dedup table with no expiry. It works, quietly, until it's the largest table in the account. Dedup records need a TTL. You only need to remember a message for as long as a duplicate could plausibly arrive, not forever.
Design for the replay, not the happy path
Some operations are naturally idempotent. An upsert is. Setting a record to a specific state is. Anything expressed as "make it look like this" is safe to repeat.
Others aren't. Incrementing a counter, appending to a list, sending an email, charging a card. These need a key from the producer, not one the consumer invents. If the producer doesn't supply an idempotency key, the consumer has nothing stable to deduplicate against, and you end up hashing the payload and hoping.
Where the same logical event can arrive with different payloads, ordering matters too. Standard queues don't guarantee order, so a consumer that assumes "this is newer than what I have" will eventually overwrite good data with stale data. The fix is usually a version or timestamp on the record, compared at write time, not a FIFO queue. FIFO has its own throughput and cost profile and is often reached for when a conditional write would have solved it.
The batch retry trap
This one costs more money than it looks like it does.
Lambda consuming from SQS gets a batch by default. If one message in the batch throws, the whole batch goes back and gets retried. Nine messages that succeeded get processed a second time. If the handler isn't idempotent, that's nine potential duplicates generated by one bad message.
Partial batch responses exist precisely for this. Report which messages succeeded and which failed, and only the failures come back. It's a small change with a large effect on duplicate volume, and it's one of the more common omissions I find in event-driven systems.
The related detail: the Lambda timeout has to be shorter than the queue's visibility timeout. If it isn't, the message becomes visible again while the function is still running, and you get concurrent processing of the same message with no failure involved at all.
Dead letter queues are where good intentions go to die
A DLQ is not a safety net. It's a parking lot, and it's only useful if someone is watching it.
Three things I check whenever I inherit a system with a DLQ:
Is there an alarm on depth? A DLQ without an alarm is a graveyard. Messages go in, nobody notices, and the failure surfaces weeks later as missing data that's expensive to reconstruct.
Is the max receive count set deliberately? The default is often fine for transient failures and wrong for poison messages. A message that will never succeed shouldn't be retried twenty times; it should land in the DLQ quickly and wait for a human.
Does redrive have a precondition? Redriving into a consumer that hasn't been fixed just loops the message back. And redriving a poison message without a plan for it recreates the incident. The fix goes first, the redrive second, and someone should be watching the DLQ depth after.
What I'd actually check
If you're auditing an event-driven system, these are the questions that surface real problems fastest:
· Is every consumer idempotent, and is the idempotency atomic rather than a check-then-write?
· Do dedup records have a TTL?
· Does the producer supply an idempotency key, or is the consumer guessing?
· Is the Lambda timeout shorter than the visibility timeout?
· Are partial batch responses in use?
· Does every DLQ have an alarm, and does it have an owner?
· Is there a documented path from DLQ back to production, including who decides?
The pattern across all of these is the same as most production reliability work. The failure isn't usually a service doing something wrong. It's an assumption in the code that the platform never agreed to, held quietly until the day traffic made it visible.
Duplicates are going to arrive. The only real question is whether they're a non-event or an incident.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article