Skip to content

Microservices: what you buy and what you pay

James Lewis and Martin Fowler define the microservice style as an approach to building one application as a suite of small services. Each service runs in its own process and communicates through lightweight mechanisms, often an HTTP API. The services are built around business capabilities, and each can be deployed independently by fully automated deployment machinery. Each also owns its data: there is no shared database that all of them read.

Mental model. In a modular monolith, a boundary is a rule in the code that a tool checks. In microservices, the boundary is a process and a network. You cannot cross it by accident, and every crossing costs something.

The checkout path from the first module shows what that does. The checkout service needs a login check, the stock, a payment, some recommendations, and an order message for the email worker. Read each box below as a service of its own, with its own data.

CustomerHealthy<1%CheckoutserviceHealthy2%Login serviceHealthy1%InventorydatabaseHealthy1%PaymentproviderHealthy7%RecommendationsserviceHealthy5%Order queueHealthy<1%Email workerHealthy24%
CustomerHealthy<1%CheckoutserviceHealthy2%Login serviceHealthy1%InventorydatabaseHealthy1%PaymentproviderHealthy7%RecommendationsserviceHealthy5%Order queueHealthy<1%Email workerHealthy24%
Text version of the diagram
  • Customer sends requests to Checkout service.
  • Checkout service sends requests to Login service, Inventory database, Payment provider, Recommendations service, Order queue.
  • Login service does not call another component.
  • Inventory database does not call another component.
  • Payment provider does not call another component.
  • Recommendations service does not call another component.
  • Order queue sends requests to Email worker.
  • Email worker does not call another component.

In a monolith the login check, the stock lookup and the recommendations are calls between modules, inside one process. As services, they are three network calls, and the payment provider and the queue are the same remote calls they were before. Counting the checkout service, one request now depends on five services.

The benefit is independence, and it has three parts.

Deploying on its own. The monolith lesson counted a 40-minute pipeline that allows at most 12 deploys in a working day, for 8 teams that want 16. Give each team a service and a pipeline of 10 minutes, and each team’s 2 deploys a day take 20 minutes of its own pipeline. Nobody queues, and nobody’s rollback undoes somebody else’s change, as long as each service’s API stays compatible with its callers.

Scaling on its own. Only the service under load gets more instances, on the hardware that suits it, instead of another copy of everything.

Teams that match the code. A service organised around a business capability can be owned by one team from code to production. This is the alignment that Conway’s law describes, and that Team Topologies uses as a design aim, and it is why many companies adopted the style: it is as much about people as about processes.

There is a fourth thing that people expect and that is not automatic: fault isolation. A service that crashes does not stop the others, but only if the others are written to survive its absence. Without that, a failure spreads along the call chain, as the next sections show.

A call between two modules costs a function call. A call between two services costs serialisation, a network round trip, a timeout that someone must choose, and a chance that the other side is slow, down, or up but unable to answer. Fowler and Lewis put it plainly: any service call could fail, and the client has to respond as gracefully as it can, which is a disadvantage compared with a monolithic design.

Say the three calls that were in-process cost 1 ms each as remote calls, a made-up round figure, and the checkout makes them one after another: 3 ms added to every request. That is the smaller cost. The larger one is that the calls can each be slow. If one call in a hundred is slow, the 99th percentile, then a request that makes 5 calls has 1 − 0.99^5, or 4.9%, chance of meeting at least one slow call. This is the effect that Dean and Barroso describe in The Tail at Scale: with many parts on the path, the rare slow call becomes common for the user.

And the calls can fail. Put the 5 services in series, each available 99.9% of the time and failing independently. The request works only if all five do, so the availabilities multiply.

Availability calculator

Your numbers

%

The share of time one copy of one component works. 99.9% means it is down about 8.8 hours a year. A provider's published figure is a promise, so measure what you actually get.

components

How many components in a row must all work for a request to succeed: the hard dependencies of the request path, each counted once.

copies

How many copies of each component can serve a request, so that one failure does not stop it. 1 means no redundancy.

Changing the numbers needs JavaScript. The result is for the starting numbers.

Result

System availability99.5%

That allows 1.82 d of downtime a year.

How it is calculated

Each step shows its formula, the numbers that go in, and the figure that comes out.

  1. One component, with its copies

    A_c = 1 − (1 − a)^r

    A group of copies is down only when every copy is down at the same time, so its unavailability is one copy's unavailability to the power r.

    a availability of one copy
    99.9%
    r copies of each component
    1

    A_c availability of one component, with its copies99.9%

  2. The whole chain

    A = A_c^n

    A request needs every component in the chain, so the availabilities multiply. The chain is up only when all of them are.

    A_c availability of one component, with its copies
    99.9%
    n components in the chain
    5

    A system availability99.5%

  3. Downtime in a year

    D = (1 − A) × T

    The time the chain is down is the unavailable share of the year (365 days).

    A system availability
    99.5%
    T time in a year
    365 d

    D downtime in a year1.82 d

What each level of availability allows
AvailabilityDown per yearDown per month (30 days)Down per day
99%3.65 d7.2 h14.4 min
99.5% · your system1.82 d3.59 h7.19 min
99.9%8.76 h43.2 min1.44 min
99.95%4.38 h21.6 min43.2 s
99.99%52.6 min4.32 min8.64 s
99.999%5.26 min25.9 s864 ms
Model notes

Assumptions

  • Components fail independently of one another. Failures with a shared cause (a bad deploy, one power supply, one data centre) break this, so real copies are less independent than this, and the real availability is lower than the figure here.
  • A copy takes over instantly and completely when another fails. Real failover takes time, and requests fail while it happens, so copies buy fewer nines than the arithmetic says.

Simplifications

  • Availability here is the share of time that the system is up. Many teams measure the share of requests that succeed instead, which is about the same only when traffic is even through the day.
  • The calculator does not claim more than twelve nines. Beyond about six, a figure says more about the model's assumptions than about any real system, and the arithmetic cannot tell a copy's failure from a shared one.
  • A year is 365 days and a month is 30 days.

Parameter values are illustrative. They are not benchmarks of any real product.

0.999 to the power 5 is 0.995, so the request is available 99.5% of the time. Over a 30-day month, one service at 99.9% may be down 43.2 minutes. The chain of five may be down for about 3.6 hours, five times as long, though every service in it looks excellent. Setting Components a request needs to 8 gives 99.2%. This is arithmetic under an assumption of independence: a shared database or a shared deploy would make it worse.

The remedies are the ones from the reliability module. A timeout on every call, a circuit breaker around each dependency, a bulkhead so that one slow dependency cannot use up the threads that the others need, and a decision, call by call, about which dependencies the request cannot do without. If the recommendations are optional and the page is shown without them when they fail, the hard path is 4 services: 0.999 to the power 4 is 99.6%. Handing the work over, as the synchronous or asynchronous lesson shows, takes a service off the path altogether, at a price that the next lesson names.

Each service owns its data, and that is what makes the independence real. It also means that no transaction spans services. Fowler’s article notes that microservice systems favour transactionless coordination between services, accept eventual consistency and deal with problems by compensating operations.

Take an order with three steps in three services: reserve the stock, take the payment, create the shipment. Each call succeeds with probability 0.999. All three succeed with probability 0.999 to the power 3, or 99.7%. The case that hurts is the one where the first step has committed and a later one fails: 0.999 × (1 − 0.999²), which is about 0.2%. At 100,000 orders a day that is about 200 orders a day that are half done: stock reserved, no payment taken. With one database that cannot happen, because the first step is undone when the second fails. With three, something must notice and repair it: a step that releases the stock, or a process that finds orders stuck in the middle. The next module, on distributed patterns, takes this up with the saga.

Fowler’s Microservice Prerequisites names three capabilities to have before the style is considered: rapid provisioning, basic monitoring and rapid deployment. Each service adds its own pipeline, dashboards, alerts and on-call duty, so 20 services are 20 of each. One request is now spread across processes: finding out why it was slow needs a trace that follows it, where a monolith has a stack trace. And a deployment tool must deal with 20 things that depend on each other’s API versions: rolling one service while its callers still run an older client is a compatibility problem that a single deployable did not have.

Fowler’s Microservice Premium sums this up as a cost: microservices slow a team down, and the cost pays back only if the system is complex enough that managing it as one unit is slower still. The factors he lists are large teams, multi-tenancy, many ways for users to interact, parts that evolve independently, and scaling, and the biggest is simple size: a monolith that is too big to modify and deploy. Note what is not on the list: a system of three developers and one product.

There is one more cost, which is about the boundaries. Fowler points out that refactoring between services is much harder than inside a monolith, and that even experienced architects find it hard to get the boundaries right at the start. A wrong boundary gives two services that must always change and be released together, which keeps the monolith’s coupling and adds the network.

Change Components a request needs in the calculator above and read the system availability. How many services in a row, at 99.9% each, bring the request below 99%? It takes 11: 0.999 to the power 11 is 98.9%, and at 10 it is 99.0%. Then change Availability of one copy to 99.99%. Even 50 components in a row, the most the calculator takes, give 99.5%, the same as the 5 services at 99.9%. A service that fails ten times less often buys ten times as many services in the chain. That is why the call graph of a request matters as much as the quality of each service in it.

Check your understanding: microservices

This quiz needs JavaScript to check your answers.

Question 1 of 3

A checkout request waits, in turn, for four services. Each of them is available 99.95% of the time, and they fail independently of each other.

What availability does the checkout request have?

Enter a number and choose its unit.

percent
Question 2 of 3

An order takes three steps in three services that each own their database: reserve the stock, take the payment, create the shipment. The payment step fails after the stock was reserved.

What does the system need now, that one database transaction would have given for free?

Choose one answer.

Question 3 of 3

Two services are always changed in the same pull request and released on the same day, because neither works with the other's older version.

What does this show?

Choose one answer.

  • Decision. The application is a suite of small services, each in its own process, owning its data and deployable on its own, communicating over the network.
  • Why? Independent deployability lets a team change, release, scale and roll back its service without coordinating with the others, and services organised by business capability line up with the teams that own them.
  • Benefits. Independent releases and rollbacks. Only the services that need capacity are scaled. Team boundaries and code boundaries can match.
  • Costs. The microservice premium. No transaction across services, so consistency is eventual and failures are repaired with compensations. Remote calls are slower than in-process calls and can fail. Prerequisites in provisioning, monitoring and deployment.
  • Failure modes. A slow service exhausts its callers along the chain and a degraded dependency becomes a cascading failure. Badly drawn boundaries give services that must be deployed together. A multi-step change that fails halfway leaves services disagreeing.
  • Scaling limitations. Operational load grows with the number of services. A chain of calls has the product of the availabilities and the sum of the latencies. The number of services a team can own is bounded by its cognitive load.
  • Alternative. A modular monolith, when you are not yet using independent deployment or independent scaling and the cost of calls, split data and operations outweighs the benefit.

What happens if one dependency slows down?

Section titled “What happens if one dependency slows down?”

The recommendations service, optional on the page, starts to take 2 s instead of 30 ms, and the checkout service waits for it. At 100 calls a second, Little’s Law gives 100 × 2, which is 200 calls waiting at the same time. If the checkout service has 200 worker threads, every thread is stuck inside a recommendations call within two seconds, and the checkout stops answering everyone, including requests that never needed recommendations. A slow optional dependency has taken down the page that makes the money. This is the cascade that the failure engineering module describes.

A timeout of 200 ms changes it: 100 × 0.2 is 20 calls held, and a circuit breaker that stops the calls altogether once the failures are clear brings it near zero. A page that is shown without suggestions is a degraded answer, not an outage. None of this happens by itself, and none of it was needed when recommendations were a function call.

  • Microservices are small services, each in its own process, built around a business capability, owning its data and deployable on its own.
  • You buy independent deployment (no queue behind a shared pipeline), independent scaling, and teams that match the code. Fault isolation is only there if the callers are written for it.
  • The network costs calls that are slower and can fail: 5 services at 99.9% in series are available 99.5% of the time, about 3.6 hours down in a 30-day month against 43.2 minutes for one, and with 5 calls at 1% slow each, 4.9% of requests meet a slow one.
  • Split data means no cross-service transaction: about 0.2% of three-step orders, 200 a day at 100,000, are left half done unless something repairs them.
  • Each service adds a pipeline, monitoring and on-call, and Fowler lists rapid provisioning, basic monitoring and rapid deployment as prerequisites.

Next: event-driven architecture, reacting to facts.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.