STACK
Ch 13 · Service Architectures
Chapter 13 · How services fit together

Service Architectures — monolith, micro, mesh, DDD, CQRS, event sourcing

13.1 · Five shapes

ShapeWhen
MonolithSmall team; one domain; speed beats scale.
Modular monolithSame deployable; strict internal boundaries. Modern default for most products.
MicroservicesMultiple teams owning distinct domains; independent deploy cadence required.
Service meshMany services; uniform retries, mTLS, traffic shaping.
Cells / micro-frontendsSplit user populations into isolated stacks for blast-radius control.

13.2 · Monolith done right

  • One deployable, one DB, one repo.
  • Mature framework (Rails, Django, Spring, Phoenix, Laravel, .NET).
  • Boundaries via packages, not network hops.
  • Background jobs in-process with a queue worker.
  • Highest speed-to-revenue for < 10 engineers.

13.3 · Modular monolith — the underrated middle

  • One deployable; strict internal modules with explicit APIs between them.
  • Extract a service later, cheaply, because boundaries were clean.
  • Tools: Java modules / Kotlin Gradle subprojects / Rust workspaces / TS workspaces + Nx tags.

13.4 · Microservices — only when you need them

ProCon
Independent deploysNetwork + serialisation tax on every call
Tech polyglotOperational footprint multiplies
Per-service scalingDistributed debugging is 10× harder
Smaller blast radiusNeed mesh, OTel, contracts to stay sane

13.5 · BFF — Backend For Frontend

One backend tailored per client surface (web, iOS, Android, partner). Composes downstream services; pairs with GraphQL or tRPC.

13.6 · Service mesh — Istio, Linkerd, Consul, Cilium

  • Sidecar proxy (Envoy) per pod — or sidecar-less (Cilium eBPF, Istio Ambient).
  • Uniformly: mTLS, retries, timeouts, circuit breaking, traffic shifting, telemetry.
  • Cost: ops complexity. Add when service count > ~10.

13.7 · Hexagonal / Clean / Onion

Same idea, three names: domain at the centre, ports (interfaces) around it, adapters (DB, HTTP, queue) on the outside. Lets you swap infrastructure without touching domain logic.

13.8 · DDD — Domain-Driven Design

  • Strategic: bounded contexts, context maps, ubiquitous language. "Order" in shipping ≠ "Order" in billing.
  • Tactical: entity, value object, aggregate, repository, domain event, factory.
  • Pattern relationships: partnership, customer–supplier, conformist, anti-corruption layer.
  • Use heavily when the domain is the hard part (insurance, lending, healthcare).

13.9 · CQRS & Event Sourcing

  • CQRS: separate models (and possibly stores) for commands vs queries.
  • Event sourcing: store events, derive state. Full audit history; rebuild any read model.
  • Pairs with: Kafka, EventStoreDB, Marten, Axon.
  • Cost: complexity. Use only when audit + temporal queries justify it.

13.10 · Strangler fig — the migration pattern

  1. Route new traffic through a façade / proxy.
  2. Move features one at a time; old and new run in parallel.
  3. When the legacy has no traffic left, retire it.

13.11 · Integration patterns (EIP — Hohpe / Woolf)

PatternIdea
Message channelProducers / consumers connected by typed channels.
Content-based routerInspect message → route to handler.
AggregatorCollect related messages; emit one.
Scatter-gatherFan out, collect all, fan in.
Process managerStateful coordinator across messages.
Message translatorAdapter between schemas.
Anti-corruption layerWrap a legacy system so its model can't leak.

13.12 · Self-check

Q1 · A 6-engineer team starting a new SaaS picks…
15 microservices
A modular monolith
A service mesh from day 1
CQRS + event sourcing