Monolith vs microservices
When one codebase stops scaling with your team, how to split it into services, how they talk (sync or async), who owns the data, and why "monolith first" is usually right.
On this page
The problem: one app, fifty engineers
A shop starts as a single program: users, catalogue, cart, payments and search in one codebase, one database, one deploy. For a team of five this is ideal. Then the company grows.
A monolith is simple until the team and traffic grow
- Most apps start as a monolith: one codebase, deployed as one program. Users, catalogue, cart and payments all live together. It’s simple to build, test and run.
- As it grows, pains appear. Fifty engineers editing one codebase step on each other’s toes. Every small change means redeploying everything. And to handle more search traffic, you must scale the entire app.
- Teams collide. Fifty engineers merge into one repository; a broken test in payments blocks a search release.
- Deploys get scary. Every change ships everything, so releases become weekly events with long checklists.
- Scaling is all or nothing. Search needs ten times the CPU on sale day, but you can only scale the whole app.
- One bug can take everything down. A memory leak in image resizing crashes the process that also takes payments.
A monolith is one deployable unit. That is not a flaw in itself: most of these pains are about people and deploys, not about the code being in one place.
The idea: small services that own their data
Each service owns one job, its data, and its deploys
- Microservices split the app into small services, each owning one job and its own data. Each can be deployed by its own team, on its own schedule.
- Each service can scale on its own. On sale day, search gets ten copies while payments stays at two. And a bug in one service has a smaller blast radius: search breaking doesn’t stop checkout.
Microservices split the system into independently deployable services, each owning one business capability (orders, payments, search) and its own data. Other services may only reach that data through the owning service's API or the events it publishes. Each service has a team that can change, deploy and scale it without asking anyone else.
The benefits are real: independent deploys, scaling only what is busy, and a smaller blast radius (how much breaks when one part fails). But you pay for them in network calls, operational load and harder consistency.
Drawing the boundaries
Bad boundaries are the most expensive microservices mistake, because moving them later means migrating data between databases.
- Split by business capability, not by technical layer. "Orders" and "Payments" are good services. "Database service" and "Validation service" are not: every feature would touch all of them.
- Use the language of the domain. In domain-driven design, a bounded context is an area where a word has one meaning. "Product" means a listing with photos in the catalogue, and a SKU with a shelf location in the warehouse. Those are two contexts, so probably two services.
- Look at what changes together. If two parts are always edited in the same pull request, they belong in the same service.
- Follow team shape. Conway's law says systems end up mirroring the communication structure of the organisation that builds them. One team per service (or a few services) works; one service shared by four teams does not.
How services talk: sync or async
Synchronous calls behind an API gateway, and how slowness spreads
A synchronous call (REST or gRPC) means the caller waits for the answer. It is simple and gives an immediate result, but it couples the caller's availability and latency to the callee's.
Worked example: the cost of a call chain
Inside one process, a function call takes nanoseconds. Between services in one datacentre, a call costs roughly 0.5–2 ms at the median and often 10–50 ms at p99, once you add serialisation, the network and the callee's own work.
If checkout makes four calls in sequence, each 2 ms median and 30 ms p99, the median adds about 8 ms, but the chance that at least one call hits its p99 is 1 − 0.99^4 ≈ 4%, so the checkout's own tail gets noticeably worse.
Availability multiplies too. If checkout needs Cart, Inventory and Payments to be up, and each is 99.9% available, checkout is at best 0.999^3 ≈ 99.7%: about 2 hours of downtime a month instead of 43 minutes.
Asynchronous events: publish once, and consumers react in their own time
An asynchronous message (through a queue or event log such as Kafka or RabbitMQ) decouples them: the producer publishes an event like OrderPlaced and moves on; consumers process it when they can. If a consumer is down, its messages wait.
| Style | Good at | Weak at | Use when |
|---|---|---|---|
| Sync request/response (REST, gRPC) | Simple; immediate answer; easy to debug | Availability and latency multiply along the chain; risk of cascading failure | The caller needs the answer now: "is this card valid?" |
| Async commands (a queue) | Smooths load spikes; retries; callee can be down | Caller cannot get an answer in the same request; duplicates need idempotency | Work that can finish later: send an email, transcode a video |
| Async events (pub/sub, Kafka) | Producer does not know its consumers; new consumers added freely | Eventual consistency; harder to trace "who reacted to what" | Several services must react to one fact: order placed, user deleted |
For every sync call, set a timeout, retry only with backoff and only for idempotent operations, and use a circuit breaker (stop calling a failing service for a while) so one slow dependency cannot tie up all your threads. See circuit breakers and retries.
Data per service, and the consistency it costs
Each service owns its tables, and no other service reads them directly. If Orders could query the Payments tables, Payments could never change its schema without breaking Orders: you would have services in name but a monolith in practice.
The cost: no more single database transaction across orders and payments. Two tools replace it:
- Saga: a business transaction split into local steps, one per service, each with a compensating step that undoes it. Reserve stock → charge card → confirm order; if the charge fails, run "release stock". The intermediate states are visible for a moment, so the design must tolerate them.
- Transactional outbox: a service must update its database and publish an event, but doing two separate writes can lose one if it crashes in between. Instead, it writes the event into an
outboxtable in the same local transaction, and a relay process publishes rows from that table to the broker. The event is published at least once, so consumers must be idempotent (safe to process the same message twice).
The plumbing: gateway, discovery, mesh
| Piece | What it does | Examples |
|---|---|---|
| API gateway | One entry point for clients: authentication, rate limiting, routing to services, sometimes combining several calls into one response | Kong, AWS API Gateway, Envoy, NGINX |
| Service discovery | Keeps the list of healthy instances of each service, so callers find them as they scale up and down | Kubernetes Services and DNS, Consul |
| Service mesh | A small proxy (sidecar) next to every service instance handles mutual TLS, retries, timeouts and per-call metrics, without changing app code | Istio, Linkerd |
| Observability | Distributed tracing becomes essential once a request crosses 5 services | OpenTelemetry, Jaeger (see monitoring) |
A mesh is worth it with dozens of services and strict security needs; with five services it is mostly extra moving parts.
The options side by side
| Architecture | Good at | Weak at | Use when |
|---|---|---|---|
| Monolith | Simple to build, test, debug and deploy; fast in-process calls; one transaction for everything | Team contention past ~20–30 engineers; whole-app deploys and scaling | Early products, small teams, unclear domain |
| Modular monolith | Clean internal module boundaries with one deploy; easy to split later | Still one deploy and one scaling unit; discipline needed to keep modules apart | The default for most companies; a stepping stone |
| Microservices | Independent deploys and scaling; team autonomy; smaller blast radius | Network latency and failures; data consistency; much more operations work | Many teams, clear domains, parts with very different load or reliability needs |
The price of a network hop, and the sensible path: split only when there is a reason
- But it isn’t free. Every call between services crosses the network: slower than a function call, and it can fail. You now run many services, many deploys, many dashboards. And data spread across services is harder to keep consistent.
- A common, sensible path: start as a modular monolith, one app with clean internal boundaries, and split out a service only when a real need appears, like one part that needs very different scaling, or a team that needs to ship independently.
When not to use microservices
- A small team. Ten engineers running 15 services spend their time on pipelines, dashboards and on-call, not features.
- An unclear domain. Early on you do not know where the boundaries are. Wrong boundaries in a monolith are a refactor; between services they are a data migration.
- No platform yet. Without automated deploys, centralised logs, tracing and health checks, many services are many blind spots.
- Strong cross-entity consistency. If most operations must atomically change data that would end up in different services, the boundaries are probably wrong.
Start with a modular monolith. Split out a service when a concrete need appears: a part with very different scaling (search, video transcoding), a part with different reliability or security needs (payments), or a team blocked by the shared deploy.
In the interview
A food delivery monolith has 80 engineers in 8 teams. Restaurant search gets 50× more traffic at lunch; payments must pass a security audit; the dispatch team wants to ship several times a day. Propose services, how they communicate, and what you would leave in the monolith.
Compare with our answer
One reasonable split: Search (read-heavy, scaled separately, fed by events from the restaurant catalogue), Payments (isolated for the audit, small surface, strict access), Dispatch (owned by its team, deployed often). Orders stays the core and publishes OrderPlaced, OrderCancelled; Dispatch and Notifications consume them. Checkout calls Payments synchronously (it needs the answer) with a timeout and circuit breaker; everything else reacts to events. Menus and restaurant admin can stay in the monolith until there is a reason.
A strong answer mentions: boundaries by capability and team · splitting for a concrete reason (scaling, security, deploy speed) · each service owns its data · sync only where an answer is needed now · events for side effects with idempotent consumers · leaving the rest in a modular monolith.
A design review shows: 9 services all reading and writing one shared Postgres database; the mobile app calls 5 services directly; placing an order makes a chain of 6 synchronous calls, each hop making up to 3 attempts; there is no tracing. List the problems and fixes.
Compare with our answer
- Shared database: services are coupled through the schema; give each its own data and expose it through APIs or events.
- Clients calling services directly: add an API gateway for auth, rate limits and routing, so internal changes don't break the app.
- Long sync chain: availability and latency multiply (0.999^6 ≈ 99.4%); make side effects asynchronous.
- Retries at every hop: retry storms (3^6 calls in the worst case); retry at one layer with backoff, a budget and circuit breakers.
- No tracing: impossible to find the slow hop; add OpenTelemetry with trace IDs propagated through every call.
A strong answer mentions: data per service · an API gateway · shortening sync chains with events · retry storms and their fixes · distributed tracing.
- Monolith = one deployable unit. Its pains are mostly people and deploys: team contention, all-or-nothing releases and scaling.
- Microservices = independently deployable services, each owning a business capability and its data.
- Boundaries: business capability, bounded contexts, what changes together, team shape (Conway's law).
- Sync (REST/gRPC) when you need the answer now; async (queues, events) for side effects. Availability multiplies: 0.999³ ≈ 99.7%.
- Every sync call: timeout, backoff with jitter, retry at one layer, circuit breaker.
- No shared database. Sagas with compensating steps replace cross-service transactions; the outbox publishes events reliably; consumers are idempotent.
- Plumbing: API gateway, service discovery, optional service mesh (sidecars for mTLS, retries, metrics), tracing.
- Default: modular monolith first; split for a concrete reason. Beware the distributed monolith.
1. A 6-person startup is building its first product and wants microservices "so it scales later". What is the best advice?
A small team pays the full operational cost of microservices with little benefit, and early boundaries are often wrong. Clean modules keep a later split cheap. Splitting per table creates chatty, tightly coupled services.
2. Checkout calls Cart, Inventory and Payments synchronously, and each is 99.9% available. Ignoring other causes, about how available can checkout be?
Availabilities multiply for serial dependencies: 0.999³ ≈ 0.997. That is roughly 2 hours of downtime a month instead of 43 minutes, a reason to keep sync chains short.
3. Twelve services share one database, and changing any table needs four teams to deploy together. What has the team built?
Shared data and lock-step deploys mean the services are not independent. Each service should own its data and expose it through an API or events.
4. An Orders service must save an order and publish OrderPlaced. It sometimes crashes between the two, losing events. What fixes this?
The outbox makes the order and its event commit together; the relay publishes at least once, so consumers must be idempotent. Publishing first just flips the failure: an event for an order that was never saved.
5. Payments slows down. Client, gateway and Orders each make up to three attempts per call. What is the main risk?
Retries at every layer multiply (3 × 3 × 3). Retry at one layer with backoff and a budget, and use circuit breakers. Duplicate charges are a separate risk, handled with idempotency keys.
6. When placing an order, which call should stay synchronous rather than become an event?
The user must know now whether the payment went through, so the caller needs the answer. Email, analytics and warehouse work can happen moments later from an OrderPlaced event.
7. What does a service mesh such as Istio or Linkerd mainly add?
The mesh moves network concerns out of app code into a proxy beside each instance. The public entry point is the API gateway's job; sagas are application logic.
Score 60% or more and the lesson is marked complete.