CSE Roadmap

Common tier · about 4 hours · 7 parts

Part 4 of 7 · 3 min read

The event-driven style

Components react to events asynchronously instead of being called in order.

Think of a relay race. A runner hands over the baton and is done. The next runner takes it and runs. Nobody directs the whole race from the middle.

FSA says most applications are request-based: a request goes to an orchestrator, which directs it to processors that read or update a database. Fetching order history is such a request. An event-based model reacts to a situation. An auction bid is an event: the system must respond by comparing it with other bids.

Event-driven architecture

Event-driven architecture is a distributed, asynchronous style in which decoupled event processors receive and process events, usually through a broker.

Example: when "order placed" is published, payment, inventory and notification each react on their own.

There are two topologies. Level 6 page 4 met the same split as choreography and orchestration.

Broker topology. There is no central coordinator. An initiating event goes into the broker. One event processor takes it, does its task and then announces what it did as a new processing event. Others react and announce in turn. Topics (publish and subscribe) are the usual channel.

The book advises that each processor always announces what it did, even if nobody listens yet. It costs little and gives extensibility. If a new need arises to analyse the emails that were sent, a new processor subscribes to the email topic, with no change elsewhere.

The book's order flow:

Step Processor Receives Announces
1 OrderPlacement PlaceOrder (initiating) order-created
2 Notification order-created email-sent (nobody listens)
2 Inventory order-created inventory-updated
2 Payment order-created payment-applied or payment-denied
3 Warehouse inventory-updated (reorders if stock is low)
3 OrderFulfillment payment-applied order-fulfilled
4 Shipping order-fulfilled order-shipped

Steps marked 2 run in parallel. Each processor scales on its own, and the topics act as back-pressure points when one is slow or down (Level 6 page 1).

The broker topology has real drawbacks. No one controls the workflow, and no one knows when the order is complete. Error handling is hard. If Payment crashes, nobody notices, the process stalls, and Inventory still decrements stock as if all were well. Recovery is weak: nobody owns the state of the original request, so nobody can restart it. Data may be left inconsistent.

Mediator topology. A central event mediator knows the steps. The initiating event goes to a queue read by the mediator. The mediator sends commands to dedicated channels (usually queues, point to point) and waits for the processors to report back. Processors do not announce to the rest of the system. Here the messages are commands, things that must happen, and not events, things that have happened. A command must be processed, whereas an event may be ignored.

Mediators come in sizes. Simple flows use Apache Camel or Mule. Flows with many branches and error rules use a BPEL engine. Flows that need a human, such as manual approval of a large trade, need a business process management engine such as jBPM. The book suggests classifying events as simple, hard or complex, and sending all of them through a simple mediator that hands harder ones to a stronger engine. There are usually several mediators, one per domain, to avoid a single point of failure.

Broker topology Mediator topology
Control of workflow None Central
Error handling and restart Weak Good
Coupling of processors Very low Higher
Performance and scalability Highest Good, but the mediator must scale too
Data consistency Weaker Better
Complex dynamic flows Natural Hard to model in advance

The book's summary: the choice is between control and error handling on one side and top performance and scale on the other. Many systems use a mix, with the mediator for the main flow and broker-style events for the unusual cases.

Check yourself:

  • In the broker topology, who knows that an order is complete?
  • Why are mediator messages called commands, not events?