Delivery Guarantees and Event-Driven Design

Understand at-most-once, at-least-once and effectively-once delivery, idempotent consumers, ordering, and the outbox pattern for reliable events.

Advanced · about 4 hours · difficulty 4/5

Learn first: Message Queues and Async Processing

Async systems fail in subtle ways: messages get duplicated, reordered or lost between a database write and a publish. Getting the guarantees right is what separates a toy design from a production one.

Delivery semantics

  • At-most-once: send and forget. Messages may be lost, never duplicated. Fine for metrics.
  • At-least-once: retry until acked. Nothing is lost, but duplicates happen. The default for most brokers.
  • Exactly-once: true end-to-end exactly-once is not achievable across arbitrary systems. In practice you build effectively-once with at-least-once delivery plus idempotent consumers.

Idempotent consumers

Give every event a unique ID. Consumers record processed IDs (in a table with a unique constraint, or in the same transaction as the side effect) and skip repeats. "Credit wallet for event 123" must have the same result whether it runs once or three times.

Ordering

Most systems only guarantee order per key or partition. Design consumers to tolerate out-of-order events: carry a version or timestamp and ignore stale updates.

The dual-write problem and the outbox pattern

Writing to the database and then publishing to Kafka can fail in between, leaving the two out of sync. The transactional outbox fixes this: write the business row and an outbox row in one DB transaction, then a relay (or CDC tool like Debezium) publishes outbox rows to the broker.

Event-driven architecture

  • Events state facts (OrderPlaced). Commands request actions (SendEmail).
  • Choreography: services react to each other's events; loose coupling, harder to see the whole flow.
  • Orchestration: a coordinator tells services what to do; easier to follow and debug.
  • Event sourcing and CQRS store events as the source of truth and build read models from them. Powerful, but add them only when you need the audit trail or separate read models.

Pitfalls

  • Assuming the broker delivers exactly once.
  • Publishing events outside the database transaction.

You are ready to move on when you can explain why a payment-success event might be processed twice and design the consumer so the user is credited only once.

Key concepts

  • At-most-once vs at-least-once
  • Effectively-once processing
  • Idempotent consumers
  • Per-key ordering
  • Transactional outbox
  • Choreography vs orchestration
  • Event sourcing and CQRS

Mini project

Implement the outbox pattern: an order service writes orders and outbox rows in one transaction, a relay publishes them to a queue, and a consumer deduplicates by event ID. Inject crashes at each step and show no event is lost or double-applied.

Check yourself

Why does writing to the database and then publishing to a broker in two separate steps eventually cause inconsistency, and how does the outbox pattern fix it?

Free resources