Part 4 of 8 · 4 min read
The costs and the rules
Network, data and operations.
FSA is direct about the downside. The main cost is the network. A method call inside one process takes a tiny amount of time. A call between services crosses a network, and often a security check at each endpoint. So the number of calls per user request matters, and a design with many small services makes many calls.
Three rules come from this.
Rule 1: each service owns its data. Other styles share one database. Microservices avoid it, because a shared schema is a coupling point as strong as shared code. The effect is that the database no longer gives one source of truth. FSA offers two ways out. One service is named the owner of each fact, and the others ask it. Or the data is copied by replication or caching, and the copy is accepted as slightly old. The same choice appeared in Level 6 (change data capture) and Level 4 (caching).
There is an upside too. Each team may choose its own storage, based on price or the type of data, without asking anyone.
Rule 2: avoid transactions across services. In a monolith, one database transaction can change orders and stock together or not at all. Across two services this is no longer free. FSA's advice is short: do not. If two services keep needing one transaction, the likely cause is that the boundary was drawn in the wrong place, and the services should be merged. Transaction needs are a sign for granularity. FSA also calls transactions across services the worst kind of dynamic connascence (page 2), connascence of value.
There are exceptions, and the common tool is the saga.
Saga
A saga is a way to complete a business operation that spans several services, without one shared transaction. A coordinator calls each step in turn. If a later step fails, it tells the earlier steps to undo their work. The undo requests are called compensating transactions.
Example: book a trip: reserve a flight, then a hotel. If the hotel fails, the coordinator tells the flight service to cancel the reservation.
FSA gives the cost. One way to build a saga is to let each step stay "pending" until the coordinator confirms the whole thing, which adds a lot of network traffic and is hard when new requests depend on a pending state. The other way is to write a "do" and an "undo" for every step, and the book says the undo is often much harder than the do, so the work more than doubles. The advice is to use sagas sparingly. A few across the system are fine. If they are the main feature, something went wrong earlier.
A worked example (made up). A purchase has three steps: take payment, reserve stock, create a shipment. Payment and stock succeed. Shipment fails. The coordinator sends "refund" to payment and "release" to stock. Each of those is a new operation that must itself be able to fail and be retried. Two undo operations were needed to cover one failed step.
Rule 3: share operations, not business code. The style prefers duplication for business logic. But monitoring, logging and circuit breakers are the same everywhere, and doing them once is better. FSA's answer is the sidecar pattern: a small shared component sits next to every service and handles these concerns. One infrastructure team can upgrade it, and every service gets the change. When all sidecars connect, they form a service mesh, a single console for logging and monitoring. Service discovery, a lookup that finds live instances of a service and can start more, is often placed in the mesh.
Sidecar
A sidecar is a helper component that runs beside each service and handles shared operational work, such as logging, monitoring and discovery.
Example: one team updates the monitoring sidecar, and all 40 services report new metrics without a code change in any of them.
BM adds a summary of the operational price. A team coming from a monolith must get much better at deployment, testing and monitoring before the benefits show up. Distributed transactions and the CAP theorem (Level 4) become real problems, not exam topics.
Check yourself: 1. A shared Orders and Stock database is "just easier". Which rule does it break, and what does it cost later? 2. Why does FSA say that frequent transactions across services are a signal about granularity? 3. What does the sidecar share, and what does it deliberately not share?