cs.oldnet

All sheets / Architecture

Microservices

Decomposition, communication, data and resilience patterns.

Monolith vs microservices

MonolithMicroservices
Deployall at onceindependently per service
Scalingwhole appper service
Dataone DB, easy joins/transactionsDB per service, eventual consistency
Ops costlowhigh (observability, networking, CI per service)
Best whensmall team, early productmany 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

StyleExamplesTrade-off
Sync request/responseREST, gRPCsimple; temporal coupling, cascading failures
Async messagingSQS, Kafka, EventBridgedecoupled, 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.
CodeMeaning
200 / 201 / 204ok / created / no content
400 / 401 / 403 / 404 / 409 / 429bad request / unauthenticated / forbidden / not found / conflict / rate limited
500 / 502 / 503 / 504server 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.