Microservice Patterns
Microservices Architecture
Build a system as small services that are deployed, scaled and owned independently — and pay for it with network calls, partial failures and operational overhead.
What you will learn
- Explain what microservices buy: independent deployment, scaling and ownership.
- See the cost: network latency, partial failure and more moving parts.
- Recognise why unguarded synchronous calls spread failures.
- Decide when microservices are worth it — and when a monolith is better.
Overview
A food court has separate stalls: the noodle stall can change its menu, hire staff or close for renovation without affecting the pizza stall. A single big restaurant kitchen is simpler to run — but if it closes for renovation, nobody eats.
In a microservices architecture each business capability runs as its own service with its own codebase, deployment pipeline and data, owned by one team. Services talk over the network (HTTP, gRPC, events). The gains are organisational and operational: teams ship independently, services scale independently, and a fault can stay contained. The cost is that every interaction is now a distributed system problem.
Run it
80% of traffic browses the catalog, 20% checks out. A redeploys the monolith; B redeploys only the payment service; C makes the payment provider very slow with no timeouts. Compare the four tiles for browse and checkout.
Monolith vs microservices
| Monolith | Microservices | |
|---|---|---|
| Deploy | Everything together | Per service, many times a day |
| Scale | The whole application | Only the busy service |
| Calls between parts | In-process, nanoseconds, never fail | Network, milliseconds, can fail or hang |
| Data | One database, ACID transactions | Database per service, sagas, eventual consistency |
| Operations | One thing to run and observe | Many pipelines, dashboards, on-call rotations |
| Fits | Small teams, unclear domain, early product | Many teams, clear boundaries, different scaling needs |
What you need before it works
- Resilience on every call: timeouts, circuit breakers and bulkheads — scenario C shows what happens without them.
- Data ownership: database per service, with events or APIs instead of shared tables.
- Observability: distributed tracing, centralized logging and metrics from day one.
- Automation: CI/CD per service, infrastructure as code, service templates.
- Good boundaries: split by business capability (service decomposition), not by technical layer.
# payment-service/.github/workflows/deploy.yml — no other service is rebuilt or restarted
on:
push:
branches: [main]
paths: ["payment-service/**"]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: ./gradlew :payment-service:test :payment-service:bootBuildImage
- run: kubectl rollout restart deployment/payment -n shop # rolling, other services untouchedTrade-offs
| Benefits | Costs | |
|---|---|---|
| Delivery | Teams deploy independently and often. | Cross-service changes need versioned contracts and coordination. |
| Reliability | Failures can be contained to one capability. | Only with resilience patterns — otherwise failures cascade through synchronous calls. |
| Cost | Scale only the parts under load. | More infrastructure, tooling and operational skill. |
Production considerations
- Prefer asynchronous events for things that do not need an immediate answer.
- Keep call chains short; a request touching ten services is a design smell.
- Version APIs and events; never break consumers without notice.
- Give each service one owning team, its own runbook and its own alerts.