Prompt
How do I build an event-driven architecture with at-least-once delivery?
Latest observation
Building an event-driven architecture with at-least-once delivery means designing your system so that every event is delivered one or more times, and consumers are safe to process duplicates.
Core idea
At-least-once delivery usually comes from this rule:
- Do not acknowledge an event until it has been durably processed
- If processing fails or the ack is lost, the message gets retried
That means duplicates are expected, so your system must be idempotent.
1) Use a durable message broker
Choose a broker that supports persistence and retries, for example:
- Kafka
- RabbitMQ
- AWS SQS
- Google Pub/Sub
- Azure Service Bus
The broker should:
- persist messages to disk
- redeliver unacked messages
- support consumer offsets / acknowledgments / leases
2) Make producers reliable
When publishing an event:
- write the event to the broker durably
- include a unique
event_id - ideally use a schema/version for payload compatibility
Example event fields:
{
"event_id": "uuid-123",
"event_type": "OrderCreated",
"timestamp": "2026-10-24T12:00:00Z",
"aggregate_id": "order-789",
"version": 3,
"payload": { ... }
}
Important
If your producer also writes to a database, avoid the “dual write” problem.
Use one of these patterns:
- Transactional outbox
- CDC (change data capture)
- Write event in same transaction as state change, then relay it asynchronously
3) Design consumers to be idempotent
Since duplicates happen, the consumer must safely handle repeated events.
Common approaches:
A. Deduplication table
Store processed event_ids in a database.
Flow:
- Receive event
- Check whether
event_idwas already processed - If yes, skip
- If no, process and store
event_id
B. Idempotent updates
Design operations so repeating them has no extra effect.
Examples:
- set order status to
SHIPPEDinstead of incrementing a counter - upsert records by key
- use version checks / optimistic concurrency
C. Natural keys + unique constraints
Use database constraints so duplicates fail harmlessly.
Example:
- unique constraint on
(event_id) - unique constraint on
(order_id, version)
4) Ack only after successful processing
The consumer should:
- receive event
- process it
- commit DB changes
- acknowledge message
If you ack before the DB commit, a crash can lose data. If you commit before ack, a crash can cause duplicate delivery — which is okay if idempotent.
5) Handle poison messages and retries
Some messages will always fail due to bad data or code bugs.
Implement:
- retry with backoff
- max retry count
- dead-letter queue (DLQ) / poison queue
Pattern:
- temporary failure → retry
- permanent failure → send to DLQ for manual inspection
6) Preserve ordering only where needed
At-least-once delivery and strict ordering are different concerns.
If ordering matters:
- partition events by a key, such as
aggregate_id - process one partition/key serially
- use event version numbers to detect out-of-order updates
If ordering does not matter:
- scale consumers horizontally more easily
7) Make side effects safe
If event handling triggers external effects, duplicates can be dangerous:
- sending emails
- charging credit cards
- provisioning resources
For these, use:
- idempotency keys with downstream APIs
- a local ledger or state machine
- “exactly once effect” semantics at the application layer, even if transport is at-least-once
Example:
- payment API call includes
idempotency_key = event_id - email service stores sent-message IDs to prevent duplicates
8) Observe and monitor
Track:
- consumer lag
- retry counts
- DLQ volume
- duplicate rate
- processing latency
- success/failure per event type
This helps you spot:
- stuck consumers
- poison messages
- duplicate storms
- backlog growth
Reference processing flow
A robust consumer flow looks like this:
- Receive event from broker
- Start DB transaction
- Check deduplication store
- If already processed, commit/skip and ack
- Apply business logic
- Persist state changes
- Record event as processed
- Commit transaction
- Ack the broker message
Simple design rule
If you want at-least-once delivery, build for this assumption:
“Any event may be delivered multiple times, and any consumer may crash at any point.”
That leads to:
- durable broker
- idempotent consumers
- transactional persistence
- retries + DLQ
- deduplication where needed
Minimal pattern summary
- Broker: durable and retry-capable
- Producer: unique event IDs + transactional outbox if needed
- Consumer: process transactionally, then ack
- Data model: idempotent or deduplicated
- Failures: retry, backoff, DLQ
- External effects: idempotency keys
If you want, I can also give you:
- a Kafka-based reference architecture,
- a Node.js / Python consumer example, or
- a transactional outbox implementation.