Event-Driven Microservices: Choreography vs Orchestration
Design scalable distributed reactive systems: Event choreography, Saga orchestration (Temporal/Camunda), transactional outbox pattern, and change stream integration.
01.1. The Fatal Dual-Write Hazard in Event-Driven Systems
When building event-driven microservices, developers frequently write code that updates a database and publishes an event in sequence:
typescript// β THE DUAL-WRITE DISASTER ANTI-PATTERN async function placeOrder(orderData: OrderDto) { // Step 1: Save order to database await db.orders.insert(orderData); // Step 2: Publish event to Kafka await kafkaProducer.send('order-created', orderData); }
Why Dual-Writes Cause Catastrophic Inconsistencies:
- Crash After DB Commit: If the server container crashes (or loses network connectivity to Kafka) immediately after line 4, the order is saved in the database, but the event is never published. The billing and inventory services never receive the event, resulting in an unfulfilled zombie order.
- Reverse Order Failure: If you publish to Kafka first and then save to the database, Kafka succeeds, but a database constraint violation fails line 4. Downstream services charge the user's credit card for an order that does not exist in the database!
- No Distributed 2PC Support: Traditional ACID transactions cannot span both a relational database and an Apache Kafka cluster.
The Transactional Outbox Pattern with Change Data Capture (CDC) π¦
The Transactional Outbox Pattern with Change Data Capture (CDC) π¦
Guaranteed atomic state mutation and event publishing without dual-write bugs, utilizing PostgreSQL WAL replication and Debezium to stream events to Apache Kafka.
Unlock Topic #157: Event-Driven Microservices: Choreography vs Orchestration
You are viewing a preview. The full in-depth engineering deep dive, interactive simulators, architecture flowcharts, and self-assessment quizzes for this topic are available with Pro or Lifetime Access.
Failure modes, high-throughput bottlenecks, and real FAANG implementation decisions.
Interactive system topology diagrams, live parameter simulators, and downloadable SVG charts.
Staff-level multiple-choice quiz questions with instant feedback and answer explanations.
Firebase Google authentication automatically syncs your completed topics and quiz scores.
How clear and staff-actionable was this system breakdown?