Lesson 16 of 28Architecture
Monolith vs Microservices
Start with a modular monolith, know when microservices pay off, draw service boundaries, and migrate with the strangler fig.
"Should we use microservices?" is one of the most expensive questions a team can get wrong. Split too early and five engineers spend their week on deploy pipelines and network bugs instead of features. Split too late and fifty engineers queue behind one release train. System design interviews probe this trade-off directly, and a nuanced answer is a strong senior signal.
Three shapes
- A monolith is one codebase, deployed as one unit, usually with one database. A Django or FastAPI app on a few servers is a monolith.
- A modular monolith is still one deployable, but its code is split into modules (orders, billing, catalog) with strict boundaries: each module exposes a small public API and owns its tables. Other modules may not reach inside.
- Microservices split the system into many small services, each deployed independently, each owning its own data, talking over the network.
A request reaches the app. Everything runs in one process, so there is one thing to deploy and monitor.
The gateway routes /orders to its own service. Each service is deployed, scaled and owned separately.
Start with a modular monolith
For most teams under roughly 20 to 30 engineers, a modular monolith is the right default. You get clean boundaries without paying the network tax. The key is to enforce those boundaries in code so they stay real:
Press Run (⌘ Enter) to see what happens.Press Run (⌘ Enter) to see what happens.Tools make the rule stick: import-linter in Python and ESLint's no-restricted-imports (or Nx module boundaries) in TypeScript fail the build when one module imports another's internals. If you later extract billing into a service, only billing/api.py changes from function calls to HTTP calls.
When microservices pay off, and what they cost
| Microservices pay off when | Because |
|---|---|
| Many teams (often 50+ engineers) block each other's releases | Each team deploys its service on its own schedule |
| One hot path needs very different scaling | Scale search to 40 instances while billing stays at 2 |
| Parts need different tech or reliability | A Python ML service next to a Go payments service |
| Fault isolation matters | A crash in recommendations does not take down checkout |
The costs are real and arrive on day one:
- Network. Calls are slow and can fail. You need timeouts, retries, circuit breakers and idempotency.
- Data consistency. No transaction spans two services. You need events, sagas (covered in Event-Driven Architecture) and tolerance for "eventually consistent".
- Operations. N pipelines, N dashboards, distributed tracing (OpenTelemetry), service discovery, versioned APIs, contract tests.
What does this print?
# A request must pass through 5 services in a chain. # Each one is up 99.9% of the time, independently. chain = 0.999 ** 5 print(round(chain * 100, 2))
Drawing boundaries: business capabilities
Split by what the business does, not by technical layer. "Billing", "Catalog", "Shipping" and "Identity" are good services. "Database service" and "validation service" are not, because every feature change would touch all of them.
Domain-Driven Design (DDD, from Eric Evans' 2003 book) gives this a name: a bounded context. In plain words, it is the area where a word has one meaning. "Customer" in billing means a payer with a tax ID and a card. In shipping it means a delivery address and a phone number. Forcing one giant Customer model to serve both couples the teams. Give each context its own model, linked by a shared customer_id.
How services talk
| Style | Examples | Good for | Watch out for |
|---|---|---|---|
| Synchronous request | REST over HTTP, gRPC | Queries that need an answer now ("is this card valid?") | Chains of calls, cascading failures |
| Asynchronous events | Kafka, RabbitMQ, SNS+SQS | Reacting to facts ("order placed") | Eventual consistency, duplicate delivery |
gRPC uses HTTP/2 and Protocol Buffers for fast, strongly typed calls between services. REST with JSON is simpler and better for public APIs. Many systems use both: sync for reads, events for side effects.
Pick the best fit for each case.
Migrating: the strangler fig
A strangler fig grows around a host tree until it can stand alone. The strangler fig pattern (named by Martin Fowler in 2004) migrates a legacy system the same way: put a proxy in front of it, rebuild one capability at a time as a new service, and switch its routes over. You never do a big-bang rewrite, and you can stop or roll back at any point.
Step 1: add the proxy. Every request still goes to the legacy app. Nothing visible changes, but you now control routing.
Put a strangler fig migration in order.
- Tap the steps below in the right order.
Strangler fig router
You are migrating a monolith with the strangler fig pattern. Write route(path, migrated) that returns "new" if the request should go to the new services, or "legacy" otherwise.
migrated is a list of path prefixes like ["/invoices", "/payments"]. A path matches a prefix only on whole segments:
route("/invoices", ["/invoices"]) -> "new"
route("/invoices/42", ["/invoices"]) -> "new"
route("/invoices-old", ["/invoices"]) -> "legacy" (different segment)
route("/invoices?page=2", ["/invoices"]) -> "new" (ignore the query string)
route("/invoices/", ["/invoices"]) -> "new" (ignore a trailing slash)- modular monolith
- One deployable app split into modules with strict public APIs and owned data.
- microservice
- A small service deployed independently, owning its own data, communicating over the network.
- distributed monolith
- Services that must deploy together or share a database: network costs without independence.
- bounded context
- A part of the business where each term has one meaning and its own model.
- strangler fig
- Migrating a legacy system piece by piece behind a routing proxy instead of rewriting it all at once.
- gRPC
- A fast, typed RPC framework over HTTP/2 using Protocol Buffers, common between internal services.