# Microservices

**Also known as:** MSA  
**Category:** System Architecture (Macro)  
**Complexity:** 4/5  
**Fit:** 10+ engineers  
**Guide:** https://layra4.dev/architectures/system/microservices

*Small services that deploy alone, each owning one capability and its data.*

Microservices split a system into many small services. Each one covers one business capability, owns its own database and deploys on its own schedule. One team owns each service from code to production. Amazon moved to this style in the early 2000s, and Netflix runs hundreds of services on AWS.

The gain is autonomy: teams release and scale without waiting for each other. The cost is a distributed system. Calls fail partially, there is no transaction across services, and you need tracing, service discovery and on-call for every service. Most teams should reach this style from a modular monolith, not start here.

> **In one sentence.** Give each service its own data and its own deploy, so teams move without waiting for each other, and accept that every call between them can fail.

## Origin (≈2014)

Named by James Lewis and Martin Fowler, based on the service work at Amazon, Netflix and others. A reaction to heavy SOA: “smart endpoints and dumb pipes” instead of a smart central bus.

## Defining characteristics

### Independently deployable

Each service has its own pipeline and ships without redeploying the others. This, not size, is the defining trait of the style.

- Own build and release
- No fleet-wide release train
- Rollback one service at a time

### Database per service

Each service owns its data store, and no other service reads it directly. Other services get the data through its API or its events.

- No shared tables
- No joins across services
- Free to pick Postgres, Redis or DynamoDB

### Built around capabilities and teams

Services follow business areas (orders, payments, shipping), not technical layers. Conway's law says a system's structure mirrors the teams that build it, so draw services and teams together.

- Cut by domain, not by layer
- One team owns a service end to end
- A team of about 5–9 people

### Smart endpoints, dumb pipes

Logic lives in the services. The transport, HTTP or a message broker, only moves messages. This is the opposite of SOA's central bus that routes and transforms.

- No business rules in middleware
- Simple, standard transport
- Each service is a full application

### Partial failure

A call to another service can succeed, fail, or time out with no answer. With 20 services at 99.9% availability each, a request that needs all of them succeeds only about 98% of the time.

- Timeouts on every call
- Retries only when safe
- Degrade instead of failing

## Rules for not regretting it

### Extract services from a modular monolith

Split out a service only after its module boundary has held for a while. A wrong boundary inside one process costs a refactor; across the network it costs a migration.

### Give each service its own data

Others read it only through the owner's API or events. A shared database ties the services back together into a distributed monolith that must deploy in lockstep.

### Expect every remote call to fail

Set a timeout on every call, retry with backoff only when safe, and add circuit breakers. Show a degraded page, not an error, when a non-critical service is down.

### Use events and sagas across services

There is no transaction across services. Publish events with an outbox, and run multi-step work as a saga with compensating steps. Accept that data is consistent after a short delay.

## Diagrams

Rendered as geometry on the guide page; not reproducible as text.

- `service-topology` — see https://layra4.dev/architectures/system/microservices

## One service = one deployable

```
orders-service/
src/
  domain/                       # its own model and rules
  application/
  api/                          # the network contract: REST or gRPC handlers
  events/                       # publishes and consumes async messages
  infrastructure/
    db.ts                       # a database only this service may touch
Dockerfile                      # ships and scales on its own
package.json                    # its own dependencies and release cadence
```

The unit is a whole deployable: its own database, pipeline and image. Other services reach it only through api/ or events/, never its tables.

## Examples

### Independent services, coordinated by events

```typescript
// order-service — owns order_db, knows nothing about shipping's storage
export const placeOrder = (orders: OrderDb, broker: Broker) =>
  async (cmd: PlaceOrder) => {
    await orders.insert(cmd);                     // local transaction, own DB
    await broker.publish("order.placed", {        // no cross-service call
      orderId: cmd.id, address: cmd.address,
    });
  };

// shipping-service — a separate deployable with its own shipping_db
broker.subscribe("order.placed", async (evt) => { // reacts asynchronously
  await shipping.schedule(evt.orderId, evt.address);
});
```

Orders owns its data and never touches shipping's. Instead of a cross-service transaction, it publishes an event, and shipping reacts in its own time. Each service deploys and scales alone.

## When to use

- Several teams keep blocking each other on one shared release
- Parts need very different scale, such as search versus account settings
- Boundaries are proven, ideally first as modules in a monolith

## When not to use

- A small team or a new product still finding its boundaries
- No CI/CD, tracing or on-call rotation yet to run dozens of services
- Core workflows need strong consistency across many entities

## Benefits

- Teams deploy and scale their services without waiting for others
- A failing service can be isolated instead of taking everything down
- Each service can use the stack and data store that fit its job

## Costs

- Every call can be slow or fail, and there is no cross-service transaction
- Heavy operations: orchestration, discovery, tracing and on-call per service
- Integration tests and local development get much harder

## Pitfalls

- A distributed monolith: services that must deploy together, with all the costs of both styles
- A shared database behind “separate” services, so a schema change breaks three teams
- No timeouts, so one slow service blocks threads everywhere and causes a cascading outage
- Services so small that one user action needs ten network calls

## Similar patterns, compared

Service-based is the coarser middle step most teams should weigh first. N-Tier also splits a system into deployables, but by layer. Multi-agent AI systems reuse the same shape.

### Microservices vs Service-Based

Both deploy services on their own. Microservices split finer and give each service its own data; service-based keeps a few coarse services, often over one database. — [Service-Based](https://layra4.dev/architectures/system/service-based)

**What is genuinely the same**

- Each service has its own build and release
- Calls between services cross the network and can fail
- Services follow business areas, not technical layers

**What actually differs**

- Here often dozens of services; there typically 4 to 12
- Here each service owns its store; there one shared database is normal
- Here a multi-step write is often a saga; there it is one local transaction

**Pick Microservices when:** Many teams must release, scale and choose their data store on their own, and you can run dozens of services.

### Microservices vs N-Tier

Both split a system into deployables. Microservices cut vertically by business capability; N-Tier cuts horizontally by technical layer. — [N-Tier](https://layra4.dev/architectures/application/n-tier)

**What is genuinely the same**

- Several deployables with a network between them
- Independent scaling, with every distributed-systems cost that brings

**What actually differs**

- A service holds every layer of one capability; a tier holds one layer of everything
- Microservices want one database per service; tiers usually share one
- Independent release is the point here; tiers often release together

**Pick Microservices when:** Teams need to change and release one business capability end to end, without touching the others.

### Microservices vs Multi-Agent Orchestration

A multi-agent system is this architecture applied to AI agents, with one big difference: the interface between workers is prose, which no compiler checks. — [Multi-Agent Orchestration](https://layra4.dev/architectures/ai/multi-agent)

**What is genuinely the same**

- Independent units with their own state, coordinated across a boundary
- Partial failure, retries and timeouts all matter

**What actually differs**

- A service contract is a schema a compiler can check; a brief to an agent is prose
- Services split for team and deploy independence; agents split to give each task a clean context

**Pick Microservices when:** What you need to separate is teams and release schedules, not context windows.

## Related patterns

- [Modular Monolith](https://layra4.dev/architectures/system/modular-monolith) — the recommended starting point for extraction
- [Strangler Fig](https://layra4.dev/architectures/architectural/strangler-fig) — how to move capabilities out of the monolith one at a time
- [Bounded Context](https://layra4.dev/architectures/strategic/bounded-context) — the best answer to “how big is a service?”
- [Event-Driven](https://layra4.dev/architectures/system/event-driven-system) — how services share facts without calling each other
- [Saga / Process Manager](https://layra4.dev/architectures/architectural/saga) — multi-step work across services without a distributed transaction

## Further reading

- *Microservices (bliki)* — James Lewis & Martin Fowler — The defining article and its nine characteristics.
- *Building Microservices* — Sam Newman (2021) — The standard practical reference, second edition.
- *microservices.io* — Chris Richardson — A pattern language covering the whole style.
