Delivery and failure
Design for at-least-once delivery
Assume a message can arrive more than once and make the write boundary reject duplicate side effects.
8 minute lesson
Cloudflare Queues provides at-least-once delivery by default. Reliability means a rare duplicate is possible even after normal success.
Give every operation a stable idempotency key. Enforce uniqueness in D1, a Durable Object, or the external provider. An in-memory Set disappears across isolates and deployments. Acknowledging before the side effect risks losing work.
Deliver the same payment-like message twice and prove the durable result is created once.
Use a stable message ID as the idempotency boundary:
insert into processed_messages (id, processed_at)
values (?, current_timestamp)
on conflict (id) do nothing;
Only perform the side effect when the insert claims the ID. Then deliberately retry the same message twice. A queue retry is normal delivery behavior, so duplicate handling belongs in the design rather than in an emergency cleanup script.
Lesson completed