System Design HLD-14.02
Microservices
Independently deployable services that own their data: drawing boundaries by domain, the real costs in network, operations and consistency, and when not to.
The big idea
A company has 300 engineers working in one monolith. Every release bundles hundreds of changes; one failing test blocks everybody; teams wait on each other for weeks. The problem is no longer technical, it is organizational: people cannot move independently.
MicroservicesAn architecture where a system is built as many small services, each owning its own data and deployed independently, usually by a single team, communicating over the network. split the system into services that are independently deployable, each owned by one team and each owning its own data. The payments team ships payments five times a day without coordinating with search. Services can scale separately and use the tools that suit them. That independence is the whole point, and it is mostly a solution to organizational scaling.
The price is that every boundary becomes a network call, every transaction across services becomes a saga, and every team now runs production infrastructure. Think of the difference between one large kitchen and a food court. In the food court, each stall runs its own menu, hours and staff, and one stall's problem does not close the others. But a meal combining dishes from three stalls now needs coordination, and someone has to run the shared seating, cleaning and payments.
How it works
Step 1: Independent deployability and data ownership
Two rules make microservices work:
- Independently deployable: a team can release its service at any time without releasing anything else. If services must always be deployed together, you have a distributed monolith: all the costs of distribution and none of the independence.
- Data owned by one service: each service has its own database (or schema), and no other service reads or writes it directly. Others use its API or its events. Sharing a database couples services through the schema just as tightly as shared code.
Step 2: Drawing boundaries
Boundaries should follow business capabilities and domains, such as orders, payments, catalog and shipping, not technical layers like "the database service" or "the validation service" ( Domain-driven design for architects ). A good boundary keeps most changes inside one service: a feature that always needs coordinated changes in three services is a sign the boundary is in the wrong place. Services that are too small (nanoservices) multiply network hops and coordination without adding independence.
Step 3: The costs
import random
# A request that synchronously calls N services, each 99.9% available, needs all of them.
for n in (1, 5, 10, 20):
a = 0.999 ** n
print(f"{n:>2} services in the path: {a:.2%} available (~{(1 - a) * 30 * 24 * 60:,.0f} min/month down)")
# Fan-out: each call is usually 20 ms but 1% of calls take 500 ms. The request waits for all.
rnd = random.Random(3)
def call():
return 500 if rnd.random() < 0.01 else 20
for n in (1, 10, 50):
slow = sum(max(call() for _ in range(n)) >= 500 for _ in range(10_000)) / 10_000
print(f"fan-out to {n:>2} services: {slow:.0%} of requests hit at least one slow call")Output
1 services in the path: 99.90% available (~43 min/month down)
5 services in the path: 99.50% available (~216 min/month down)
10 services in the path: 99.00% available (~430 min/month down)
20 services in the path: 98.02% available (~856 min/month down)
fan-out to 1 services: 1% of requests hit at least one slow call
fan-out to 10 services: 10% of requests hit at least one slow call
fan-out to 50 services: 39% of requests hit at least one slow call The first half shows availability compounding: a request that synchronously needs 10 services, each at 99.9 percent, is available only 99 percent of the time. The second shows tail latency compounding: if 1 percent of calls are slow, a request that fans out to 50 services hits at least one slow call 39 percent of the time. Beyond that:
- Network failure modes everywhere: timeouts, retries, circuit breakers ( Timeouts, retries, backoff and jitter , Circuit breakers and bulkheads ).
- Distributed data: no joins across services, no cross-service transactions; use sagas, the outbox pattern and eventual consistency ( Sagas: choreography and orchestration , Transactional outbox and change data capture ).
- Operational load: every service needs deployment pipelines, monitoring, on-call, security patches and capacity planning; tracing becomes essential ( Distributed tracing ).
- API contracts: changes must stay backward compatible, because consumers upgrade on their own schedule.
Step 4: When not to use microservices
Small teams, early products whose domain is still changing, and systems without strong platform tooling usually move faster with a modular monolith ( Monolith and modular monolith ). A common path is to start modular and extract a service only when there is a concrete reason: a team that needs to deploy independently, a component with very different scaling or reliability needs, or a different technology requirement.
Going deeper
Synchronous chains versus events
Long chains of synchronous calls multiply latency and failure. Prefer asynchronous events for things that do not need an immediate answer ( Event-driven architecture and event sourcing ), keep synchronous call depth shallow, and have each service keep a local copy of the data it often needs, updated from other services' events.
Platform teams
Organizations that succeed with microservices usually have a platform: standard service templates, CI/CD, service discovery ( Service discovery ), observability and security baked in, so each new service does not reinvent them. Without it, every team pays the full operational cost separately.
Trade-offs
| Option | Wins when | Loses when |
|---|---|---|
| Modular monolith | Small and medium teams, evolving domains, strong consistency needs | Independent deploys and per-component scaling |
| Microservices | Many teams needing independent deploys, ownership and scaling | Network failures, distributed data, operational overhead |
| Few larger services (domain-oriented) | Balancing team independence against hop count and overhead | Some coordination inside each service's team |
| Distributed monolith | Nothing | Costs of distribution with none of the independence |
Common mistakes
- Splitting into microservices to fix code quality problems that boundaries inside a monolith would fix.
- Sharing a database between services.
- Services that must be deployed together, forming a distributed monolith.
- Deep synchronous call chains with no timeouts or circuit breakers.
- Too many tiny services owned by too few people.
In the wild
Uber
Introducing domain-oriented microservice architecture (opens in a new tab)Uber's 2020 post describes the downsides it hit with around 2,200 critical microservices: investigating one problem could require working through about 50 services across 12 teams, simple features needed changes across many services and teams, and some services had to be deployed together, forming networked monoliths. Its response, Domain-Oriented Microservice Architecture, groups related services into domains with a single gateway as the entry point, arranges domains in layers that limit which dependencies are allowed, and adds extension points instead of teams editing each other's services. The post reports classifying the 2,200 services into 70 domains and, for one platform, cutting the time to integrate a new feature from three days to three hours.
Check yourself
-
What is the main benefit microservices are meant to provide?
Show answer
B. Independent deployment and ownership so many teams can move in parallel They mostly solve organizational scaling; technically they add costs.
-
Two services read and write the same database tables. What is the problem?
Show answer
B. They are coupled through the schema and cannot change or deploy independently Data ownership by one service is what keeps services independently changeable.
-
A request synchronously depends on 10 services, each 99.9 percent available. Roughly how available is it?
Show answer
C. 99 percent 0.999 to the 10th power is about 0.99; availability multiplies along a synchronous chain.
-
A 4-person startup with a fast-changing product asks whether to start with microservices. Best advice?
Show answer
B. Start with a modular monolith and extract services when there is a concrete reason Microservice costs only pay off with organizational scale and a stable domain.
Recap
- Microservices are independently deployable services, each owning its data and owned by one team.
- They solve organizational scaling: parallel teams, independent releases and scaling.
- Costs: network failures, compounding availability and latency, distributed data and operational load.
- Draw boundaries along business domains; avoid shared databases and distributed monoliths.
- Start with a modular monolith unless independence is needed now; build platform support when you split.