Monolith vs microservices
One shop with four parts, built as one app, as one app in clear modules, and as four services. Put them through the same five rounds and see what each wins and what each pays. None of them wins them all.
What you'll take away
- Tell a monolith from microservices by how each one scales, ships and fails
- Explain what the network costs: calls that fail, and a saga instead of one transaction
- Keep one sick service from dragging others down: timeouts, fallbacks, a breaker
- Start with a modular monolith, and know when splitting out a service pays off
The 1-minute version
Tap a point and the picture below plays that step.
Press a round, then switch between the ways. None wins every round. The numbers are a small model shop's
A team of 5 has just started a new product that changes every week. Where should they start?
The picture: the same shop on the same ground. A monolith is a block of identical buildings, each holding all four parts (catalog, cart, orders, payments), with one database; microservices are four lots, one per part, each with its own copies and its own database. The street between them is the network; amber is a deploy, red is down, the light is an order, the small lights are shoppers crowding the catalog. A small model, nothing measured.
Compare them all: Monolith vs microservices
The same shop three ways, through the same five rounds: a busy catalog, a fix to payments, a memory leak in the catalog, placing one order, and counting what there is to run. See what each way wins and loses in each round.
Use it when A small team, a new product still changing fast: the simplest to build, change and debug
Skip it when Many teams in one codebase waiting on each other to ship, or one part needing very different capacity
A model shop of four parts, nothing measured. No way wins every round; small teams usually start with a monolith or a modular one. Press any tile to see it large above, with when it fits.
Round by round
| Round | Monolith | Microservices |
|---|---|---|
| One part gets busy | Copy the whole app | Copy that service |
| Ship a fix | Ship everything | Ship one service |
| A bug eats memory | The whole app falls | One service falls |
| Place an order | Function calls, one transaction | Network calls, a saga |
| To run | One app, one database | Many of each, plus tracing |
It's about teams as much as code
Microservices pay off when many teams need to ship on their own and parts really need different capacity. For a small team, or while the boundaries are still moving, a monolith with clear modules is faster to build and far easier to run. Draw the boundaries well and splitting later is a move, not a rewrite.
Every network call can fail
Give each call a timeout, retry a bounded number of times, stop calling a service that keeps failing (a circuit breaker) and have something to show when it's gone. Without these, one stuck service drags its callers down with it.
In real code
// The same question, "what does this item cost?", asked two ways: a function call inside one process (a monolith)
// and an HTTP call to another service (microservices). The second needs a timeout and a fallback, or one slow or dead
// service makes its callers hang too. Both servers really run on 127.0.0.1.
// Run: node calls.ts (Node 23.6+; on Node 22: node --experimental-strip-types calls.ts)
import assert from "node:assert/strict";
import { createServer } from "node:http";
import type { AddressInfo } from "node:net";
const prices: Record<string, number> = { tea: 120, cup: 250 };
// Monolith: the catalog is a module; asking it is a function call. Nothing in between can fail.
const catalog = { price: (item: string) => prices[item] };
assert.equal(catalog.price("tea"), 120);
// Microservices: the catalog is a server of its own.
const service = createServer((req, res) => {
const item = new URL(req.url ?? "/", "http://x").searchParams.get("item") ?? "";
if (req.url?.includes("slow")) return; // a service that never answers (stuck, overloaded)
res.setHeader("content-type", "application/json").end(JSON.stringify({ item, price: prices[item] }));
});
await new Promise<void>((r) => service.listen(0, "127.0.0.1", r));
const base = `http://127.0.0.1:${(service.address() as AddressInfo).port}`;
async function priceOverNetwork(item: string, path = "/price"): Promise<{ price: number | null; from: string }> {
try {
const r = await fetch(`${base}${path}?item=${item}`, { signal: AbortSignal.timeout(300) }); // never wait for ever
return { price: ((await r.json()) as { price: number }).price, from: "catalog service" };
} catch {
return { price: null, from: "fallback (show the cart without prices, try again later)" };
}
}
const ok = await priceOverNetwork("tea");
const slow = await priceOverNetwork("tea", "/slow");
service.closeAllConnections();
service.close();
const down = await priceOverNetwork("tea");
assert.deepEqual(ok, { price: 120, from: "catalog service" });
assert.equal(slow.price, null);
assert.equal(down.price, null);
console.log(`function call: 120 · HTTP: 120 · catalog stuck: gave up after 300 ms, fallback · catalog gone: fallback`);
node calls.ts (Node 23.6+ · on Node 22 add --experimental-strip-types)It printed: function call: 120 · HTTP: 120 · catalog stuck: gave up after 300 ms, fallback · catalog gone: fallbackThe same price lookup as a function call (monolith) and as HTTP to a catalog service. The network call needs a timeout and a fallback: when the service is stuck it gives up after 300 ms, and when it's gone the cart still shows. Both servers really run on 127.0.0.1.
Common questions
What's the difference between a monolith and microservices?
A monolith is one app with one database, its parts calling each other as functions in one process. Microservices make each part a service with its own database, talking over the network. You can scale and ship them apart, and you pay for it in a lot more to run.
Can't a monolith scale?
It can: run more copies of the whole app behind a load balancer, which is simple. You just pay for the idle parts too. In the picture only the catalog is busy, yet the whole app goes to 8 copies.
Should a small team start a new product with microservices?
Usually not. While the boundaries move every week, separate services mean changing APIs across them every time an idea changes, on top of running a distributed system. Start with a monolith in clear modules and split when there's a real reason.
What is a modular monolith?
One deployable app, divided inside into modules with clear walls: each has its own data and interface, and none reaches into another's. You keep a monolith's simplicity and stay ready to split into services when the time comes.
Why should each service own its database?
If services share tables, every schema change needs every team, and they can't really ship apart: a distributed monolith. Each service owns its data, and the others ask through an API or events.
One service goes down. Why do the others fall with it?
Calls with no timeout leave the callers hanging; requests pile up until they fall over too, and then whatever calls them. Give every call a timeout and something to show when the other side is gone, and stop calling a service that keeps failing (a circuit breaker).
How do you do a transaction across services?
Not the old way, since each service has its own database. Use a saga: each step is a small transaction in its own service, and when one fails, compensating steps undo the earlier ones (put the stock back). The data agrees a little later.
What extra is there to run with microservices?
In the picture the four-part shop becomes 4 services and 4 databases: 8 things to deploy, monitor and back up. Then you need tracing to see where time goes, service discovery, and people ready to run a distributed system.
Next lesson
Reverse proxy, API gateway and BFFLevel: Intermediate
With many services now, see what stands in front of them: a reverse proxy, an API gateway, a BFF.