Microservices
Monolith vs microservices, how to draw service boundaries, communication styles, API gateways, service discovery, data ownership, sagas, and the real costs of distribution.
A microservice architecture splits an application into small services, each owning one business capability and its own data, deployed independently and talking over the network. It's an organisational tool as much as a technical one: it lets many teams ship without stepping on each other. It also turns every function call into a distributed-systems problem.
Monolith vs microservices
- One deployable unit and one database
- Simple to develop, test, debug and deploy at first
- In-process calls: fast and transactional
- Scales as a whole; one bad module can take everything down
- Coordination gets painful with many teams
- Many independently deployable services
- Teams own services end to end and release on their own schedule
- Scale and choose technology per service
- Network calls: latency, partial failure, eventual consistency
- Needs serious platform investment: CI/CD, observability, discovery
Drawing boundaries
- Split by business capability (orders, payments, catalogue, shipping), not by technical layer (a "database service" is a smell).
- Use bounded contexts from domain-driven design: each service owns a consistent model of its part of the domain. "Customer" in billing and "customer" in support can be different models.
- A good boundary means most changes touch one service. If every feature needs coordinated changes in five services, you have a distributed monolith — the worst of both worlds.
Architecture overview
Communication
| Style | Examples | Use for | Watch out |
|---|---|---|---|
| Synchronous request/response | REST, gRPC | Queries needing an immediate answer | Chains of calls multiply latency and failure probability |
| Asynchronous events/messages | Kafka, RabbitMQ, SNS/SQS | Notifying others of changes; long workflows | Eventual consistency, harder debugging |
If service A calls B calls C, each at 99.9% availability, the chain is at most 99.7% available. Prefer async events for anything that doesn't need an immediate answer, and keep synchronous call chains short.
Supporting infrastructure
- API gateway: one entry point for clients — routing, authentication, rate limiting, request aggregation. A BFF (backend for frontend) is a gateway tailored to one client type (web vs mobile).
- Service discovery: services find each other's instances via a registry (Consul, Eureka) or platform DNS (Kubernetes Services).
- Service mesh (Istio, Linkerd): sidecar proxies handle mTLS, retries, timeouts, traffic shifting and telemetry without changing app code.
- Observability: distributed tracing is essential to follow a request across services (Observability).
- Config and secrets management, CI/CD per service, contract tests between services.
Data ownership
Each service owns its data; no other service reads its tables directly. Others access it via its API or by consuming its events. This keeps services independently changeable, but:
- Joins across services become API composition or locally replicated read models.
- Transactions across services are impossible with ACID; use sagas.
Sagas
A saga is a sequence of local transactions, each with a compensating action that undoes it if a later step fails.
- Orchestration: a central coordinator tells each service what to do (easier to follow; tools like Temporal, Step Functions).
- Choreography: services react to each other's events (looser coupling, harder to see the overall flow).
Resilience
Every remote call can fail or hang. Apply timeouts, retries with backoff, circuit breakers and bulkheads (Reliability patterns), and design for graceful degradation: if recommendations are down, show the page without them.
- Microservices trade simplicity for team autonomy and independent scaling; start with a modular monolith.
- Split by business capability and bounded context; avoid the distributed monolith.
- Prefer async events; keep synchronous chains short.
- Each service owns its data; cross-service workflows use sagas with compensations.
- Requires gateways, discovery, observability, CI/CD and resilience patterns.
QWhen would you NOT use microservices?medium
For a small team, an early product with unclear domain boundaries, or a system without strong platform tooling. The operational overhead — deployment pipelines, observability, network failures, distributed data — outweighs the benefits until team size or independent scaling needs make the monolith a bottleneck. A modular monolith is usually the right start.
QHow do you keep data consistent across services?hard
Accept eventual consistency and use sagas: each step is a local transaction that publishes an event (reliably, via the transactional outbox), and failures trigger compensating actions. Make consumers idempotent. For reads that need data from several services, compose via APIs or build a local read model from events.