Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

FDE · Reliability

Make the Flaky Endpoint Survive.

A demo delivers one event on a good day. A real integration delivers thousands against an endpoint that fails intermittently — and worse, sometimes processes an event but times out before it can say so. Naively, you lose events. Add retries and you stop losing them, but you start duplicating the ones that timed out. Add idempotency and the duplicates vanish. Add a dead-letter queue and the events that exhaust their retries become recoverable instead of lost. Send a stream through a flaky endpoint, toggle the three patterns, and watch the exactly-once delivery rate climb — the reliability trade-offs from the integration handbook, made playable.

flaky endpoint · retries (fewer losses, new duplicates) · idempotency (kills duplicates) · dead-letter (recovers loss)
transient failure

A temporary error — a timeout, a 503, a network blip — that would succeed if simply tried again.

ambiguous timeout

The server processed the event but the client never got the ack, so the client thinks it failed. Retrying causes a duplicate.

idempotency

A re-delivered event is processed at most once, by deduping on a key. It’s what makes retries safe.

dead-letter queue

Where an event goes after exhausting its retries — held for inspection and replay instead of dropped.

delivery.js — retry, dedupe, dead-letter
Ready
The endpoint succeeds ~60% of the time, times out ~15% (it processed the event but you didn’t hear back), and hard-fails ~25%. Toggle the three patterns, then Send 20 and watch the outcomes. Start with everything off to feel the pain.
—
delivered once
—
duplicated
—
lost

How it works

The lab is a tiny delivery simulator, and building it exposes exactly why the three patterns exist and why their order matters. Model each attempt as one of three outcomes: a clean success (the server processes it and you get the ack), an ambiguous timeout (the server processes it but you never hear back), or a hard failure (nothing happened). Now watch the patterns interact. With no retries, every hard failure is a lost event, and every timeout leaves the client believing the delivery failed. Whether a timed-out event is really lost depends on where the timeout hit: before the server did the work, it is gone; after, it landed and nobody knows. (In this lab the timeout always comes after the work, so those events land once.) Either way your confirmed delivery rate is capped by the endpoint’s reliability. Turn on retries and losses plummet, because a hard failure just gets tried again until it sticks — but a nasty thing appears: an ambiguous timeout already processed the event, and retrying it makes the server process it again, so retries manufacture duplicates. This is the counter-intuitive lesson — retries alone trade one failure mode (loss) for another (duplication). The fix is idempotency: dedupe on a client-supplied key so a re-delivered event is processed at most once, which collapses those duplicates back to exactly-once and is precisely why idempotency is the prerequisite that makes retries safe. Finally, some events are just unlucky and exhaust every retry; without a dead-letter queue they’re silently dropped, but with one they’re parked for inspection and replay — turning an invisible data-loss bug into a visible, recoverable backlog. Stack all three and the exactly-once rate approaches 100% even against a genuinely flaky endpoint. The takeaway an FDE carries to every integration: retries need idempotency, and everything needs a dead-letter path.

1

Naive delivery loses events

With no retries, every hard failure is a lost event, and every timeout looks like one to the client — though if the server finished the work before the timeout hit, the event actually landed. Your confirmed delivery rate is capped by the endpoint’s reliability, which you don’t control.

2

Retries cut loss — but add duplicates

Retrying a hard failure eventually succeeds, so losses plummet. But an ambiguous timeout already processed the event; retrying it processes it again. Retries trade loss for duplication.

3

Idempotency kills the duplicates

Dedupe on a key so a re-delivered event is processed at most once. The duplicates collapse to exactly-once. This is why idempotency is the prerequisite that makes retries safe.

✓

Dead-letter recovers the unlucky

A few events exhaust every retry. Without a dead-letter queue they vanish silently; with one they’re parked for replay. Stack all three and exactly-once approaches 100% — even on a flaky endpoint.

Retries alone
less loss, more duplicates
Idempotency
makes retries safe
Dead-letter
loss → recoverable
All three
≈ exactly-once

The code

# Deliver one event through a flaky endpoint processed = 0 for attempt in range(max_attempts if retries else 1): r = try_deliver(event) # success | timeout | fail if r == "success": processed += 1; break # got the ack — stop if r == "timeout": processed += 1 # server DID process it… # …but we saw a failure, so we retry → maybe process again effective = min(1, processed) if idempotency else processed if effective == 0: dead_letter(event) if dlq else drop(event) elif effective == 1: delivered_exactly_once += 1 else: duplicated += 1 # retries without idempotency

The picture: one timeout, made safe

client endpoint key store 1: send event, key k42 k42 new: process, save ack lost: timeout ✕ 2: retry, same key k42 k42 seen: skip work 200 + saved result two deliveries, one effect
The ambiguous timeout from the lab, with idempotency switched on. The first delivery did its work; only the ack went missing. The retry carries the same key, so the endpoint recognises it and returns the result it saved the first time. Without the key store, step 2 would process the event again: the duplicate the lab paints amber. With retries exhausted on hard failures, the event would go to the dead-letter queue instead.

Worked example: 1,000 events through the lab’s endpoint

The simulator gives every attempt a 60% chance of success, 15% of an ambiguous timeout and 25% of a hard failure, and allows 5 attempts when retries are on. A timeout counts as a failure to the client, so it retries, but the server has already done the work once. These are the exact expected outcomes per 1,000 events, enumerated over every possible attempt sequence.

Patterns onOnceDuplicatedLostIn DLQ
None75002500
Retries802.1196.91.00
Retries + idempotency999.001.00
All three999.0001.0

Row 1: only the 25% hard failures are lost. A single timeout still lands once, because the server did process it; the client just cannot tell, so it looks lost from where the client sits. Row 2: an event is lost only if all 5 attempts hard-fail, 0.25⁵ = 1 in 1,024, but every timeout before the final success adds a processing, so 196.9 events land two to five times. Without idempotency the server does 1,237 pieces of work for 1,000 events. Row 3: deduping collapses every one of those to exactly once. Row 4: the last 1 in 1,024 is parked, not dropped. The cost of all this is load: with retries on, the client makes 1.65 attempts per event on average.

Trade-offs: what each pattern costs

Here Time is what the pattern adds to latency, and Space is what it has to store.

CaseTimeSpaceWhy
Retries (5 attempts)Worst case is every backoff wait in a row: 1 + 2 + 4 + 8 = 15 s with doubling from 1 sNothing stored, but 1.65 attempts per event on the lab’s endpointCuts loss from 25% to 0.1% here, and creates duplicates unless the receiver dedupes.
Idempotency keysOne lookup and one write per requestOne record per key for as long as a retry can arrive: say 1 million events a day, kept 7 days, at 100 bytes each, is 700 MBThe retention window must outlast the longest retry or replay, or a late duplicate gets through.
Dead-letter queueNone on the happy pathEvery exhausted event, until someone replays itTurns silent loss into a backlog. Worth nothing without an alert on its depth and a replay tool.
Ack fast, process asyncThe sender gets its 2xx at onceAn internal queue between receipt and workStops slow processing from looking like failure and triggering the sender’s retries.

None of the four is free, and none is optional on a path that moves money or writes records. The order still matters: retries first only if idempotency is already there.

Common mistakes in delivery code

  • Minting a new key on every attempt

    If the retry loop generates a fresh UUID each time, no two deliveries ever share a key and dedupe never fires. The key belongs to the event, not the attempt: fix it before the first send, or derive it from the event’s own id.

  • Checking the key, then acting on it

    Two deliveries arrive together, both look up k42, both find nothing, both process. Make the claim atomic: an insert that fails on a unique constraint, in the same transaction as the side effect. Then exactly one of them wins.

  • Retrying errors that will never succeed

    A 400 for a malformed payload fails identically on all 5 attempts, spends the endpoint’s capacity and delays the event’s arrival in the dead-letter queue. Retry timeouts, 429s and 5xx. Send other 4xx straight to the DLQ.

  • Retrying in a tight loop

    Immediate retries from thousands of clients hit a struggling endpoint all at once, just as it tries to recover. Back off exponentially and add random jitter, so the retries spread out instead of arriving in waves.

  • Replaying the DLQ around the dedupe

    A dead-lettered event may already have been processed: its last attempt could have been an ambiguous timeout. Replay it through the same idempotent path as live traffic, never through a side door that skips the key check.

Where this shows up in production

  • Payment APIs

    Stripe accepts an Idempotency-Key header on POST requests, so a client that times out on a charge can retry without charging twice. The same pattern guards any call whose side effect costs money.

  • Webhooks

    Senders deliver at least once and re-deliver on a non-2xx response, as the FAQ below says. Receivers dedupe on the event id the sender includes, which is exactly the key store in the diagram.

  • Message queues

    Amazon SQS standard queues deliver at least once, and a redrive policy moves a message to a dead-letter queue after a set number of receives. Kafka consumers that commit offsets after processing are at least once too, so the consumer must be idempotent.

  • Interviews

    “How do you avoid charging a customer twice?” The expected answer: exactly-once delivery over an unreliable network is impossible, so combine at-least-once delivery with an idempotent receiver to get exactly-once effect, and give failures a dead-letter path.

Quick check

1. You add retries to a flaky integration and losses drop — but a new problem appears. What is it?

2. What makes retries safe?

3. What does a dead-letter queue give you?

FAQ

Why do retries alone cause duplicate deliveries?

Because of ambiguous timeouts: the server processes an event but the ack is lost, so the client thinks it failed and retries. The server, with no memory of the first delivery, processes it again — a duplicate. Retries fix hard failures but manufacture duplicates from timeouts. The fix is idempotency: dedupe on a client-supplied key so the second delivery is ignored.

What is idempotency and why does it matter for integrations?

An operation is idempotent if doing it many times has the same effect as doing it once. For integrations it means a re-delivered webhook or retried request is processed at most once, usually by deduping on an idempotency key: the first time you see the key you process and record the result; later times return the recorded result. It matters because retries re-deliver events, and without idempotency those re-deliveries double-charge or double-write.

What is a dead-letter queue?

A dead-letter queue (DLQ) is where a message goes after failing processing too many times. Instead of being retried forever (blocking the pipeline) or dropped silently (losing data), it’s moved to a separate queue to inspect, fix, and replay. For an FDE, a DLQ turns silent, invisible data loss into a visible, recoverable backlog you can monitor and alert on. Any reliable ingestion path should have one.

How do you make a webhook integration reliable?

Combine patterns: verify the signature and reject stale events (replay window); acknowledge fast (return 2xx) and process asynchronously so slowness doesn’t trigger retries; make processing idempotent by deduping on the event id (senders retry on non-2xx and re-deliver); and route failures to a dead-letter queue for replay. Together these turn an at-least-once channel into effectively exactly-once processing.

Keep going

Finished this one? 0 / 61 Labs done
NextThe Deployment Target Chooser Next on the “The engagement, played” ladder · rung 5 of 6

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Labs