Communication Between Services

Lesson 5 of 10 · 22 min

x
50%

Event-Driven Architecture

Event-driven architecture (EDA) treats state changes as first-class events. When something happens — an order is placed, a payment fails, a user signs up — the service publishes an event describing what occurred. Other services subscribe and update their own state or trigger side effects. No service calls another directly; they react to facts. This creates temporal decoupling: the publisher does not know or care who listens. New consumers can be added without changing the producer. The cost is eventual consistency — you cannot assume all services see the new state immediately. Event sourcing takes EDA further by storing events as the source of truth and rebuilding state by replaying them.

Before
Direct orchestration
// Order service knows every downstream step
async function placeOrder(data) {
  const order = await db.create(data);
  await inventory.reserve(order);
  await payment.charge(order);
  await analytics.track(order);
  return order;
}
After
Event-driven choreography
// Order service publishes a fact and stops
async function placeOrder(data) {
  const order = await db.create(data);
  await eventBus.publish('OrderPlaced', {
    orderId: order.id,
    items: order.items,
    total: order.total,
  });
  return order;
}
// Inventory, Payment, Analytics each subscribe on their own

Key Takeaway

Publish facts, not commands — let services react independently instead of orchestrating every step from one place.

PreviousNext Lesson