Monolith vs microservices
| Monolith | Microservices | |
|---|---|---|
| Deploy | all at once | independently per service |
| Scaling | whole app | per service |
| Data | one DB, easy joins/transactions | DB per service, eventual consistency |
| Ops cost | low | high (observability, networking, CI per service) |
| Best when | small team, early product | many teams, clear domains, different scaling needs |
A modular monolith is often the right first step.
Decomposition
- By business capability / DDD bounded context, not by technical layer.
- Strangler fig: route slices of traffic from the monolith to new services incrementally.
- Anti-corruption layer to translate between legacy and new models.
- Signs of a bad split: chatty sync calls, shared DB, lockstep deploys (distributed monolith).
Communication
| Style | Examples | Trade-off |
|---|---|---|
| Sync request/response | REST, gRPC | simple; temporal coupling, cascading failures |
| Async messaging | SQS, Kafka, EventBridge | decoupled, resilient; eventual consistency, harder debugging |
- API gateway: single entry, auth, rate limiting, routing. BFF: gateway per client type.
- Service discovery: DNS, Cloud Map, Consul, K8s services. Service mesh: mTLS, retries, traffic shifting (Istio, App Mesh).
Data management
- Database per service; no other service reads your tables.
- Saga for multi-service transactions: sequence of local transactions + compensating actions.
- Choreography: services react to events. Orchestration: central coordinator (Step Functions).
- Transactional outbox: write event to an outbox table in the same DB transaction; relay publishes it.
- CQRS: separate write model from read-optimized views.
- API composition or read replicas/materialized views for cross-service queries.
Resilience patterns
- Timeouts on every remote call.
- Retries with exponential backoff + jitter, only for idempotent ops.
- Circuit breaker: closed → open (fail fast) → half-open (probe).
- Bulkhead: isolate pools so one slow dependency can't exhaust all threads/connections.
- Rate limiting / load shedding, fallbacks (cached/default response).
- Idempotency keys for safe retries of writes.
Deploy & operate
- Container per service, independent CI/CD pipeline, versioned APIs.
- Contract testing (Pact) to catch breaking API changes before deploy.
- Health checks: liveness vs readiness.
- Distributed tracing (OpenTelemetry) with correlation IDs across hops.
- Centralized logs + RED metrics per service.
API design
- REST: nouns, plural resources, proper status codes, pagination, filtering.
- Versioning: URL (
/v2), header, or additive non-breaking changes. - Backward-compatible changes: add optional fields, never rename/remove without deprecation.
- gRPC for internal low-latency, strongly typed calls; REST/GraphQL at the edge.
| Code | Meaning |
|---|---|
| 200 / 201 / 204 | ok / created / no content |
| 400 / 401 / 403 / 404 / 409 / 429 | bad request / unauthenticated / forbidden / not found / conflict / rate limited |
| 500 / 502 / 503 / 504 | server error / bad gateway / unavailable / gateway timeout |
Migration story (STAR outline)
- S: monolith, slow coupled releases.
- T: enable independent, faster deploys.
- A: strangler fig by domain, async events (SQS/EventBridge/Kafka), DB split with outbox, flags for cutover.
- R: deploy time down, failures isolated, teams ship independently.