Sub topic 3
Living with Eventual Consistency
Assume you know basic programming, HTTP request/response APIs, database transactions, and distributed systems. In an event-driven system, a published change reaches consumers later. Eventual consistency means a read may temporarily show old data, but replicas or services converge after processing completes.
Design user flows around this delay. Show “processing” status, query the source of truth when necessary, and avoid promising that every screen updates immediately.
Retries and Dead-Letter Queues
A consumer may fail because of a timeout or temporary outage. Retry with increasing delays, called backoff. After a limit, move the event to a dead-letter queue (DLQ) for investigation instead of retrying forever.
The Transactional Outbox
A database update and event publication can fail independently. The transactional outbox solves this by writing the business change and an unpublished event in one database transaction. A relay later publishes the outbox row.
BEGIN;
UPDATE orders SET status = 'placed' WHERE id = 'o-1';
INSERT INTO outbox (id, event_type, payload)
VALUES ('evt-1', 'order.placed', '{"orderId":"o-1"}');
COMMIT;The relay may publish the same row twice, so consumers still need idempotency.
Sagas
A saga coordinates a business process across services without one distributed transaction. Each step commits locally. If a later step fails, a compensating action reverses the earlier effect, such as refunding a payment after shipping cannot be arranged.
Event Sourcing and CQRS
These are related but separate ideas. Event sourcing stores events as the primary record of change and rebuilds state from them. CQRS separates models or paths for commands that change state and queries that read state. You can use either pattern without adopting the other.
Observability and Trade-offs
Trace IDs should travel with events. Monitor processing delay, retry counts, DLQ size, and consumer errors. Test duplicate delivery, reordered events, replay, and partial failure.
Event-driven architecture adds brokers, asynchronous debugging, schema management, and eventual consistency. Prefer a direct transaction or synchronous API when one service owns the data, immediate feedback matters, or the workflow is simple.
Exercise
An event updates a search index several seconds after an order is placed. What should the UI show?
Show answer
Show a processing or recently updated state, and read from the authoritative order service when current data is required.