Course contents

11. Microservices Lesson 1 of 10

Microservices Fundamentals

Split a monolith into services and see the costs: latency that stacks along call chains, bottleneck services, cascading failures, and async decoupling.

16 minIntermediate8 steps
Loading the interactive simulation…
Loading the knowledge check…

Summary

  • Microservices split an application into services that are owned, deployed and scaled independently, each with its own data.
  • Function calls become network calls: latency adds up along the chain and new failure modes appear.
  • In a synchronous chain the weakest service sets capacity, and availabilities multiply.
  • A slow service ties up its callers; a failed one fails every request that needs it.
  • Asynchronous messaging removes dependencies from the request path at the cost of eventual consistency.
Deep diveHow it works, trade-offs, failure modes, misconceptions and the interview angle.

What it is

A microservice architecture builds an application from small services, each responsible for one business capability (orders, inventory, payments), owned by one team, deployed on its own schedule and usually storing its own data. Services talk over the network: synchronously (HTTP, gRPC) or asynchronously (messages, events). The alternative is a monolith: one deployable application whose modules call each other in-process.

Why it exists

A monolith is simple to build, test and run, until the organisation grows. With dozens of engineers in one codebase, every change waits for a shared release, one team's bug can take everything down, and the whole application must be scaled to relieve one hot part. Microservices let teams own their service end to end: release independently, choose what fits their problem, and scale only what needs scaling. The motivation is mainly organisational and operational, not performance.

How it works

Boundaries. Services are cut along business capabilities (often found with domain-driven design), not technical layers. A good boundary means most changes touch one service.

Data ownership. Each service owns its data and exposes it only through its API or events. Sharing tables between services re-couples them: one team's schema change breaks another team's service.

Communication. A synchronous call is simple to reason about but couples the caller's latency and availability to the callee's. An asynchronous message (via a queue or event stream) lets the caller continue without waiting, at the price of eventual consistency.

Latency stacking. If Orders calls its database, then Inventory, then Payments, the request takes the sum of all those calls plus a network round trip for each. Parallel calls reduce this to the slowest branch but not below it.

Capacity and availability coupling. While Orders waits for Payments, it holds a worker, a connection and memory. A slow Payments therefore makes Orders busier without doing more work, and can exhaust it. Availabilities multiply: five services in a chain at 99.9% each give about 99.5% for the whole request.

Supporting infrastructure. Service discovery, an API gateway, per-service CI/CD, centralised logging, metrics and distributed tracing, timeouts, retries and circuit breakers on every call. This is real, ongoing cost.

When to use it

  • Several teams need to work and deploy independently on one product.
  • Parts of the system have clearly different scaling, availability or technology needs.
  • Domain boundaries are understood well enough that services will not need constant cross-service changes.
  • The organisation can afford the operational platform: automation, observability and on-call per service.

When not to

  • A small team or an early product: a well-structured modular monolith gives clear boundaries without network calls, and can be split later along those boundaries.
  • When boundaries are unclear; wrong cuts produce a distributed monolith where every feature needs coordinated releases of several services.
  • When the main goal is performance; extra network hops usually make requests slower.

Trade-offs

Choice You gain You pay
Microservices over a monolith Independent deployment, scaling and ownership Network latency, partial failures, distributed data, operational overhead
Synchronous calls Immediate answers, simple flow Latency and availability coupling
Asynchronous messaging Isolation from slow or failed services Eventual consistency, compensation logic, harder debugging
Database per service Teams evolve schemas independently No cross-service joins or transactions; data duplication
Many fine-grained services Small, focused codebases More hops, more deployables, more coordination

Alternatives

  • Modular monolith: one deployable with strict internal module boundaries; most of the design benefits, few of the operational costs.
  • A few larger services ("macroservices") split only where the benefit is clear.
  • Serverless functions for isolated, event-driven pieces.
  • Service-oriented architecture with shared infrastructure: coarser services, often with a central integration layer.

Failure modes

  • Cascading failure: a slow or failed service exhausts its callers, which fail their callers in turn. Mitigate with timeouts, circuit breakers, bulkheads and fallbacks.
  • Bottleneck service: one under-provisioned service limits every request that passes through it.
  • Retry amplification: retries at several layers multiply load on a struggling service.
  • Distributed monolith: services that must be deployed together and share databases, with all the costs and none of the benefits.
  • Inconsistent data: without distributed transactions, a crash halfway through a multi-service operation leaves partial state; sagas and the outbox pattern handle this.
  • Chatty interfaces: many small calls per request multiply latency and failure points.

In production

Start from business boundaries, give each service its own data, and keep synchronous chains short. Put timeouts, bounded retries and circuit breakers on every call. Move work the user does not need right away onto queues or events, with idempotent consumers and the outbox pattern. Invest in distributed tracing so you can see where a request spent its time. Monitor each service's latency, errors and saturation, and plan capacity per service.

Common misconceptions

  • "Microservices are faster." Requests usually get slower: function calls become network calls.
  • "Microservices are more reliable." Only with deliberate isolation. A synchronous chain fails if any link fails.
  • "Smaller is always better." Very fine services create chatty, fragile systems.
  • "We can share the database for now." Shared tables are the strongest coupling there is and are hard to undo.

Interview angle

Interviewers want to hear why you would split (team autonomy, independent scaling) and why you might not (latency, complexity, distributed data), not microservices by default. Expect questions on boundaries, data ownership, synchronous versus asynchronous communication, handling a slow or failed dependency, consistency across services (sagas, outbox) and observability. Quantifying the cost ("each hop adds a few ms; five services in a chain at 99.9% give 99.5%") makes the trade-off concrete.

Model assumptions

The simulation is a simplified model, not a benchmark. The monolith has 16 workers and a 50 ms mean service time plus one database query. After the split, Orders (32 workers, 15 ms) calls its database, then Inventory (15 ms, which calls its own database), then Payments (16 workers, 20 ms), strictly one after another, holding its worker while it waits. Service times are lognormal (cv 0.3); hops between services take 3–5 ms and shoppers are 20 ms away. Extra Inventory capacity is modelled as more workers rather than more instances. "Degraded" makes a service 4× slower. In the asynchronous step an order succeeds once its payment message is stored. Numbers show behaviour and orders of magnitude only.

The lesson as textEvery step of the simulation, written out.
  1. 1. One application does everything

    Shoppers place about 50 orders per second. A single application, the monolith, checks stock, takes payment and stores the order: about 50 ms of work and one database query, all in one process.

    Calls between those parts are plain function calls: no network, no partial failures. One codebase, one deployment, one database.

    Scene: Shoppers send orders to a single Shop application, which queries one database. Orders take about 100 ms and all succeed.

    Key idea: Inside a monolith, modules talk through function calls: fast, and they fail together or not at all.

  2. 2. Split into services

    As the team grows, the shop is split into three services, each owned by one team and deployed on its own: Orders, Inventory and Payments. Inventory gets its own database; no other service may read it directly.

    Orders now calls its database, then Inventory, then Payments, over the network. The total work is about the same, but every call adds network hops and the median latency goes up, not down.

    Scene: Orders calls the Orders DB, then Inventory (which calls its own database), then Payments, one after another. Orders take noticeably longer than in the monolith, and all succeed.

    Key idea: Microservices buy independent deployment and ownership. They cost network calls: latency adds up along the call chain.

  3. 3. One service becomes the bottleneck

    Traffic grows to 120 orders per second. Inventory, with only three workers, is saturated: its queue fills and it rejects calls. Orders looks busy too, but its workers are mostly waiting on Inventory; Payments is fine.

    Every rejected Inventory call fails the whole order. A chain of services is only as strong as its weakest link, and the monitoring has to show *which* link that is.

    Scene: At 120 orders per second Inventory is overloaded and rejects calls, so some orders fail. Orders is busy waiting on Inventory; Payments has plenty of spare capacity.

    Key idea: In a call chain, the least-capable service sets the capacity of the whole request.

  4. 4. Scale only what needs it

    Because Inventory is its own service, we can give only Inventory more capacity (in practice: more instances behind its own load balancer), without touching Orders or Payments.

    The rejections stop. In the monolith we would have had to scale the whole application to relieve one hot part. Independent scaling is one of the real benefits of the split.

    Scene: Inventory has more capacity. Nothing is rejected any more and all three services have headroom.

    Key idea: Separate services can be scaled, deployed and tuned independently.

  5. 5. A slow service slows everyone

    Payments becomes four times slower. It still answers every call, just later.

    Orders waits for Payments while holding one of its own workers, so Orders gets busier without doing more work, and every order takes longer. Latency in a synchronous chain is the sum of every hop; one slow service shows up in the response time of all the services above it.

    Scene: Payments is degraded and slow. Orders holds more workers while waiting for it, and every order takes longer, though all still succeed.

    Key idea: Synchronous calls couple latency and capacity: the caller pays for the callee’s slowness with its own resources.

  6. 6. One service down, every order fails

    One second in, Payments goes down completely. Orders, Inventory and both databases are healthy, yet every order fails, because Orders cannot finish without the Payments call.

    With three services in a chain, the order works only if all three work. Their availabilities multiply: three services at 99.9% give about 99.7% for the whole request.

    Scene: Payments goes offline. Every order now fails at the Payments step, although all the other services are healthy.

    Key idea: A synchronous chain is as available as the product of its parts. More services in the path means more ways to fail.

  7. 7. Take payment asynchronously

    We change the design: Orders saves the order as *pending payment* and publishes a message; a Payments worker charges the card later. Payments is still down, but orders keep succeeding and the messages wait in the queue until it is back.

    The price: the shopper gets "order received" instead of "payment confirmed", and a failed charge must be handled later (cancel the order, ask for another card). Asynchronous designs trade immediate answers for independence.

    Scene: Orders publishes payment messages to a queue. The Payments worker is offline, so messages pile up in the queue, but every order succeeds.

    Key idea: Asynchronous messaging removes a runtime dependency from the request path, at the cost of eventual consistency and compensation logic.

  8. 8. When microservices pay off

    Payments is back and works through its messages. Microservices pay off when many teams need to deploy independently and parts of the system have different scaling or reliability needs. They cost network latency, partial failures, distributed data and a lot of operational tooling (tracing, service discovery, CI/CD per service).

    A well-structured modular monolith is often the better start. Split along business boundaries, give each service its own data, and prefer asynchronous calls where the user does not need the answer right away.

    Scene: Orders calls its database and Inventory synchronously and hands payments to a queue that the Payments worker consumes. Everything is healthy.

    Key idea: Split for team autonomy and independent scaling, not for fashion. Every network call is a new failure mode.