Book Deep Read · Fundamentals of Software Architecture · Chapter 14

Event-Driven Architecture: Mediator or Broker, and the Speed and Loss of Control That Asynchrony Buys

Fundamentals of Software Architecture · Ch 14 · Mark Richards & Neal Ford · 2020

中文 →

What is this chapter about?

You place an order online and within a second the page says "Order placed." Yet at that moment the payment, the stock update, the confirmation email and the warehouse packing slip have not happened yet. They are being done in the background, each on its own. A software shape built on "acknowledge first, do the work in the background" is what the book calls event-driven architecture.

A strange thing first: nobody is in charge, yet the work gets done

Picture an office with a notice board. The front desk takes an order, pins a note saying "new order," and turns to the next customer. Accounts sees it, collects the money, and pins "payment received." Only then does the warehouse pack the box and pin "packed," and the courier picks it up from there. Nobody is running the whole process; everyone just watches for the notes they care about.

Why the old world was hard

The traditional way is a counter queue: the front desk personally walks to accounts, then to the warehouse, and only comes back to tell you "done" when everything is finished. If one window is slow, you wait; if one window closes, the whole thing stalls. When a crowd shows up, the line runs out the door.

The core mechanism: two kinds of office

The book describes two setups. The first is the notice-board office above: people pin notes and read notes, and nobody manages anybody. It is fast and easy to grow. A new colleague who hands out coupons just starts watching for "new order" notes, and nobody else changes a thing. The downside: nobody knows how far a given order has got. If accounts is off sick that day, the order quietly sits half done and no one notices.

The second is the dispatcher office: one dispatcher holds the checklist and hands out the jobs one at a time. "You collect the money." "Done? Good, you pack it." When something goes wrong, the dispatcher knows straight away and can retry, stop, or call in a person. The price: everyone waits on the dispatcher, and if the dispatcher falls over, everything does.

Either way, one thing can't be dodged. The front desk has already told you "success," so if the payment later fails, there must be a way to come back and tell you, and notes must never go missing along the way.

What it buys you / how to choose

Events give you fast replies, room for huge crowds, and new features without touching old code, which is why online shops, trading systems and sensor networks love them. For a simple flow where speed matters most, use the notice board. For a long flow where every failure must be tracked and redone, use the dispatcher. The honest cost: this is one of the hardest styles in the book to test and debug, because things happen on their own in the background and replaying what went wrong is difficult.

One line to remember

Event-driven = answer first, let independent parts react to events in the background: the notice board is fast but nobody is in control, the dispatcher is in control but slower. Pick by whether you need speed or control.

Want the components of the broker and mediator topologies with the full order flow, the mediator's three-tier delegation, how async errors get repaired, the three places messages get lost, request-reply, and the book's star ratings? → Switch to Deep