CSE Roadmap

Common tier · about 4 hours · 7 parts

Part 3 of 7 · 3 min read

Orchestration and choreography

A process across services can be driven by a conductor or by services reacting to each other.

Think of an orchestra and a street dance. In an orchestra, a conductor tells each player when to play. In a street dance, every dancer watches the others and joins in at the right moment.

The Building Microservices book uses this contrast with an example. Creating a customer needs three actions: a record in the loyalty points bank, a welcome pack sent by post, and a welcome email.

With orchestration, a central service drives the process. The customer service calls the loyalty bank, then the email service, then the postal service, using request and response. It can track how far the process has got, and it can show a flowchart directly in code. The book warns of the downside. The customer service becomes a central governing authority, a hub where more and more logic collects, and the other services turn into dumb data holders that are told what to do.

With choreography, the customer service just announces "customer created". The loyalty bank, the email service and the postal service each subscribe and react. If a fourth service later needs to react, it subscribes without anyone else changing. The downside: the business process is no longer written down anywhere. It exists only implicitly, as the sum of reactions. The book advises extra monitoring that matches the explicit flowchart and checks that each service did its part, for example to catch a bug in the loyalty bank.

Choreography

Choreography is a way to run a business process in which each service reacts to events from the others, with no central controller.

Example: the order service publishes "order paid". The warehouse reacts by packing the order. The email service reacts by sending a receipt. Neither is told to by the order service.

Orchestration

Orchestration is a way to run a business process in which one central service tells the others what to do and in which order.

Example: a checkout service calls stock, payment and shipping one after another and decides what to do when one fails.

The Fundamentals of Software Architecture (FSA) book says the same in its own terms. Its event-driven style has two topologies. In the broker topology there is no central controller. The event flow is spread across the event processors in a chain through a lightweight broker. Each processor does its task and then advertises what it did with a new event. This suits simple flows that need high responsiveness. In the mediator topology, a central mediator controls the workflow, which suits cases where the order of steps and error handling need control. Level 7 returns to this style in more depth.

The Building Microservices author prefers choreography for looser coupling and easier change, but admits it needs extra work to monitor and track processes across service boundaries. He found heavily orchestrated systems brittle, with a high cost of change. Good practice is to mix: orchestrate where a clear owner and error handling are needed, and publish events for everything else.

A worked example. Four services react to "order paid":

Service Reaction If it is slow or down
Warehouse Reserves items and starts packing Orders wait in its backlog
Email Sends a receipt Receipts are late
Loyalty Adds 100 points Points appear later
Analytics Adds the sale to a report Report is a bit behind

The order service is unaffected by any of them. Adding a fifth reaction (say fraud scoring) needs no change to the order service. The price is that nobody can see the whole process in one place, so the team adds a monitor that checks "every paid order got a receipt within 5 minutes".

Check yourself:

  • What is the main drawback of choreography?
  • What is the main drawback of orchestration?