Section305

Design example

WhereIllustrative SaaS scenario
FormatDesign example

Event-driven microservices that stay consistent

A proposed design for independent consumers, durable event publication and safe retries.

This is an illustrative architecture discussion, not an account of a completed client engagement. The choices and goals below belong to the example.

Design goals at a glance

  • Allow consumers to release independently within compatible event contracts
  • Keep slow asynchronous consumers off the user’s request path
  • Verify recovery from duplicate delivery and partial failure
All design goals

Software architecture / Hands-on engineering / Engineering leadership

Problem

Consider services connected by synchronous calls, where a slow downstream dependency delays unrelated work. The example explores moving suitable reactions to events while preserving synchronous responses where users need them.

Constraints

  • No lost or double-applied business events
  • Teams migrating at different speeds
  • Existing PostgreSQL data as the source of truth

Architecture & approach

Services write state and an outbox record in one transaction. A relay publishes the outbox to Kafka, and consumers are idempotent, so retries are safe. Redis handles hot read paths. Synchronous calls remain only where a user is waiting.

Fig. 305-2Outbox to Kafka with idempotent consumers Critical path
  • Service to PostgreSQL
  • PostgreSQL to Outbox relay
  • Outbox relay to Kafka topics
  • Kafka topics to Billing
  • Kafka topics to Notifications
  • Kafka topics to Search index

Design tradeoffs

How do we publish events without losing any? 305-3.1

  • AlternativeWrite to the database, then publishA crash between the two steps loses the event.
  • AlternativeDistributed transaction across DB and brokerCorrect, but slow and operationally fragile.
  • Example choiceTransactional outbox plus idempotent consumersOne local transaction. Replays are safe by design.

Design goals

  1. Allow consumers to release independently within compatible event contracts
  2. Keep slow asynchronous consumers off the user’s request path
  3. Verify recovery from duplicate delivery and partial failure

Technology

Kafka, PostgreSQL, Redis, Microservices, REST APIs, Docker

Lessons & tradeoffs

  • Idempotency helps make retries safe, but its guarantees depend on where state is recorded and which side effects are covered.
  • Use synchronous calls where an immediate response is required. For other work, compare a background job with an event stream before choosing.