Part 07
Messaging, Event-Driven Architecture and Data Processing
Move work and data between services asynchronously, and process it reliably.
7.1 Asynchronous Foundations
Chapter outcomes and context
Topics covered: 105 Synchronous and asynchronous communication (Core) · 106 Message queue fundamentals (Core) · 107 Producers and consumers (Core) · 108 Consumer groups, partitioning and competing consumers: consumer lag, partition rebalancing (Core) · 109 Publish/subscribe (Core) · 110 Queue-based load leveling (Core)
Learning outcomes
- Decide sync vs. async: synchronous when the caller needs the answer now; asynchronous when work can complete later — buying decoupling, spike absorption, and independent scaling at the price of eventual consistency and harder debugging. The asynchronous request–reply variant (correlation IDs, reply queues) covers "async but an answer is expected".
- Explain queue mechanics: producers, broker, consumers; acknowledgements; redelivery on consumer failure; competing consumers scaling work horizontally.
- Operate consumer groups: a topic's partitions divided among group members (parallelism capped by partition count); watch consumer lag as the primary health metric; understand what partition rebalancing does to in-flight work.
- Distinguish point-to-point (one consumer per message — work distribution) from publish/subscribe (every subscriber gets every event — notification and fan-out).
- Apply queue-based load leveling (primary home): a queue between a spiky producer and a steady consumer absorbs bursts (the waiting room of Project 16 is this pattern at the front door → 97). When not to use: the caller needs a synchronous answer.
7.2 Broker Architectures
Chapter outcomes and context
Topics covered: 111 Kafka architecture: logs, retention, replay (Core) · 112 RabbitMQ and message routing (Advanced)
Learning outcomes
- Explain Kafka's model: an append-only, partitioned, replicated log; messages are retained by policy rather than deleted on consume; consumers track their own offsets, which makes replay a first-class operation (reprocessing in Project 19); the partition key determines placement and per-partition ordering.
- (Advanced) Explain RabbitMQ's model: a smart broker with exchanges (direct, topic, fanout, headers) routing to queues; per-message acknowledgement; messages removed when consumed.
- Choose: Kafka for high-throughput event streams, replay, and multiple independent consumers of the same data; RabbitMQ-style brokers for task queues and rich routing with lower operational weight. Simpler alternative at small scale: a jobs table in the database you already run.
7.3 Event-Driven Architecture and Delivery Guarantees
Chapter outcomes and context
Topics covered: 113 Event-driven architecture (Core) · 114 Message ordering and out-of-order events (Advanced) · 115 Delivery semantics: at-most-once, at-least-once, exactly-once and its limits with external systems (Core) · 116 Idempotent consumers and deduplication (Core) · 117 Dead-letter queues and poison messages (Core)
Learning outcomes
- Design event-driven architecture: services emit facts ("OrderPlaced"), consumers react. Distinguish event notification (thin event, consumer fetches) from event-carried state transfer (fat event, consumer keeps a local copy). Costs: no global view of flow, eventual consistency, contract management (→ 34, 36).
- (Advanced) Reason about ordering: global ordering does not scale; per-key ordering (same key → same partition) is usually what the business needs — and consumers must still handle out-of-order events across keys and after retries (Projects 12, 19).
- Define the delivery semantics: at-most-once (may lose, never duplicates), at-least-once (never loses, may duplicate — the practical default), exactly-once — achievable only within narrow boundaries; with external systems it is really at-least-once plus idempotency.
- Design idempotent consumers (primary home of consumer-side idempotency; API-side → 38): natural idempotency (set-to-value) or deduplication keys stored transactionally with the effect.
- Build the failure path: retries with backoff (→ 152), then a dead-letter queue for poison messages, with alerting and a replay procedure.
7.4 Data Consistency Across Systems
Chapter outcomes and context
Topics covered: 118 Transactional outbox (Advanced) · 119 Change data capture (Advanced) · 120 Event sourcing (Advanced)
Learning outcomes (all Advanced)
- Explain the dual-write problem: writing the database and publishing to a broker cannot be made atomic by hoping — one can succeed while the other fails.
- Apply the transactional outbox: the business change and the event are written in the same database transaction; a relay publishes from the outbox table. At-least-once by design, so consumers must be idempotent (→ 116).
- Apply change data capture: tail the database's log (→ 68; Debezium as the example) to turn row changes into an event stream — cache invalidation, search indexing, analytics feeds (Project 19). Boundary rule: outbox when you control the writes and want intentional events; CDC when you must derive events from an existing database.
- Understand event sourcing: the event sequence is the source of truth; state is derived by replay. Gains: perfect audit, temporal queries. Costs: schema evolution pain, unfamiliar querying — pair with CQRS read models (→ 155). When not to use: most CRUD systems; the simpler alternative is current-state tables plus an audit log (→ 171).
7.5 Jobs, Workflows, Batch and Streams
Chapter outcomes and context
Topics covered: 121 Background job processing: delayed and scheduled jobs (Core) · 122 Workflow orchestration (Advanced) · 123 Batch processing and ETL (Advanced) · 124 Stream processing: windows, late events, replay and reprocessing (Advanced)
Learning outcomes
- Move slow work off the request path with job queues: job states, retries, delayed jobs (run at time T) and scheduled jobs (run every interval), worker pools (Projects 10 and 11).
- (Advanced) Coordinate long-running, multi-step processes with workflow orchestration (durable state machines, compensation hooks — Temporal-style as the example) and contrast with pure event choreography — the trade-off returns with Saga (→ 139; Project 15).
- (Advanced) Design batch and ETL pipelines: scheduled jobs over bounded data, idempotent re-runs, backfills — and the batch-vs-stream decision (Project 18's transcoding; warehouse context → 213).
- (Advanced) Process streams: stateless transforms vs. stateful operations (windowed aggregations, joins); event time vs. processing time; late events and watermarks; replay and reprocessing as the recovery and correction mechanism (→ 111; Project 19).
Done when you can
- Choose sync vs. async per interaction and defend the consistency consequence.
- Design an at-least-once pipeline whose consumers are provably idempotent.
- Explain the outbox pattern in one minute, including why the naive dual write fails.
- Place a workload correctly among job queue, workflow engine, batch, and stream.