A monolith is one deployable application that contains all features. Microservices split the product into many small services, each owned by a team and deployed on its own. Microservices are fashionable, but they trade simple code for a distributed system. This lesson explains what you gain, what you pay, how to do the arithmetic on reliability and latency, and why many strong designs start with a modular monolith.
Detailed Notes
What Problem Are We Solving?
As a product grows, its code grows, and so does the team. Two things start to hurt: changes collide (everyone edits and releases the same codebase) and scale is uneven (video encoding needs 100 machines, the settings page needs one). The architecture decision is how to draw the boundaries between parts of the system.
Monolith: the whole application is built, deployed and run as one unit. Parts call each other with ordinary function calls inside one process, and usually share one database.
Microservices: the application is split into many small services, each owning one business capability (orders, payments, search) and its own data. Services call each other over the network (REST or gRPC, see the previous lessons) or exchange events through a queue.
Modular monolith: one deployable unit, but internally split into modules with strict boundaries and no shared internals. It is the middle option.
Two calculations frame the decision.
text
Reliability of a chain of synchronous calls
One request touches 5 services, each 99.9% available
0.999^5 = 0.995 = 99.5% available
Downtime: 0.5% of 8,760 hours = about 44 hours per year
A single service at 99.9% = about 8.8 hours per year
Latency of network hops
In-process function call: about 0.0001 ms
Network call in one zone: about 0.5 to 2 ms
A page that needs 12 serial service calls: 12 x 1.5 ms = 18 ms of pure overhead,
before any work is done, plus the slowest call dominating the tail (p99).
Splitting a system turns reliable function calls into network calls that can be slow, fail or time out. That is the price you must get something back for.
Monolith vs MicroservicesComparison
Monolith
one deployable unit
1
One codebase, all modules
users, orders, payments
2
Calls are function calls
in-process, microseconds
3
One database
ACID across modules
4
Deploy and scale as a whole
N copies of everything
Simple, fast to build
Real transactions
Low operations load
Coupled releases
All-or-nothing scaling
Large blast radius
Best for small teams and young products
Microservices
many small services
1
One service per capability
own code and pipeline
2
Calls cross the network
1 to 2 ms, can fail
3
Database per service
sagas, no shared transaction
4
Deploy and scale independently
per team, per load
Independent releases
Targeted scaling
Fault isolation
Distributed failures and latency
No cross-service transactions
Heavy operations
Best for many teams and uneven scale
Key point. Microservices move complexity from the code into the network. Make sure the independence is worth it.
Same product, different boundaries.
Option 1: Monolith
How it works
All modules (users, catalog, orders, payments) live in one codebase, are built into one artifact and run as identical copies behind a load balancer. A request is handled inside one process: the order module calls the payment module as a function. One relational database holds all tables, so a single ACID transaction (all-or-nothing change, see the relational databases lesson) can update orders and inventory together.
You still scale horizontally by running many copies of the whole app.
Pros
Simple to build and reason about. One repository, one build, one deploy, one place to look at logs.
Fast calls and real transactions. No network hops between modules, and cross-module changes commit atomically.
Easy local development and testing. Start one process.
Low operational overhead. One thing to monitor and deploy, ideal for a small team.
Easy refactoring. Moving a function between modules is a code change, not an API migration.
Cons
Coupled deployments. A bug in the reporting module can block the release of a payment fix, and one slow release cycle serves everyone.
Scale is all or nothing. To give the search module more CPU you run more copies of everything.
Blast radius. A memory leak or crash in one module can take down the whole app.
Code tangles over time. Without discipline, modules reach into each other's tables and the system becomes a big ball of mud.
Technology lock-in. One language and framework for everything. Large builds and test suites slow every developer.
When to use it
Choose a monolith when the team is small (say under 20 engineers), the product is young and boundaries are still unclear, or you need to move fast with strong transactions.
Realistic scenario. A 6-person startup builds an online booking product. They deploy several times a day, one engineer can understand the whole code, and bookings, payments and availability must change in one transaction. A monolith on one PostgreSQL database ships in weeks. Splitting it would add network calls, deployment pipelines and a distributed-transaction problem before they have a single customer.
Option 2: Microservices
How it works
Each service has one responsibility, its own codebase, its own deployment pipeline and its own database, so other services cannot read its tables directly. They interact only through APIs or events. An API gateway (a single front door that authenticates and routes requests) exposes the services to clients. A service registry or DNS lets services find each other, and a queue or event stream carries asynchronous work.
Because each order spans several databases, a cross-service business operation (place order, reserve stock, charge card) cannot use one database transaction. The usual replacement is a saga: a sequence of local transactions, with a compensating action (like a refund) if a later step fails.
Pros
Independent deployment. The payments team releases ten times a day without coordinating with search.
Independent scaling. Run 100 video encoder instances and 2 settings instances.
Fault isolation. If recommendations fail, checkout can still work, provided the calls are designed to degrade.
Team autonomy. Each team owns a service end to end, and can choose suitable technology and data store.
Smaller codebases. Easier to understand each part.
Cons
Distributed-systems complexity. Network failures, timeouts, retries, partial failures, version mismatches between services.
Reliability multiplies down. Five chained services at 99.9% give 99.5% overall, as calculated above, unless you add timeouts, fallbacks and circuit breakers.
Higher latency. Every boundary adds milliseconds and a failure mode.
No cross-service transactions. You need sagas, idempotent operations and eventual consistency.
Operational cost. Dozens of pipelines, dashboards, distributed tracing (following one request across services), service discovery, security between services.
Wrong boundaries are costly. If services always change together, you built a distributed monolith: all the complexity, none of the independence.
When to use it
Choose microservices when many teams (a rough threshold is 30 to 50+ engineers) need to release independently, parts of the system have very different scaling or reliability needs, and the boundaries are stable and well understood.
Realistic scenario. A streaming company has 300 engineers. The video encoding pipeline needs GPUs and bursts to thousands of machines, the account service must be extremely reliable, and the recommendation team experiments daily. Separate services let each team deploy and scale on its own schedule, and a failed recommendation service degrades the home screen rather than blocking playback.
The hybrid: modular monolith first, extract services when it hurts
Most successful microservice systems started as a monolith. A modular monolith keeps one deployable unit but enforces boundaries: each module has a public interface, owns its tables, and talks to other modules only through that interface. Later, when a module has a clear reason to leave (it needs separate scaling, or a team needs independent releases), you extract it as a service. Its boundary is already clean.
Often called the strangler approach: put a gateway in front, move one capability at a time to a new service, and leave the rest in the monolith.
Strangling a Monolith into ServicesArchitecture
Key point. Extract one service at a time, each with its own data, behind a gateway.
A gateway routes extracted capabilities to new services. Everything else still hits the monolith.
Interview Discussion
In an interview, do not draw 15 microservices on the whiteboard by reflex. Anchor on the requirements.
Team size and release needs. "Eight engineers, one release train: a monolith."
Scaling shape. "Encoding scales 100x more than the rest, so I'd make it its own service early."
Boundaries and data ownership. "Payments owns its database, others call its API."
Cost of the split. Mention latency, the reliability multiplication, sagas, tracing.
If the interviewer's problem is large (YouTube, Uber), a service per major capability is expected, but say why each boundary exists.
Interview tip. Avoid "microservices because it scales". A monolith scales horizontally too. The real reason to split is organisational independence and uneven scaling or reliability needs.
Interview discussion. "I'd begin with a modular monolith so booking, payment and availability share one transaction. As the team grows past about 30 engineers, I'd extract payments first, since it has distinct reliability and compliance needs, behind a gateway and with its own database."
Trade-off thinking. A monolith trades independence and targeted scaling for simplicity and transactions. Microservices trade simplicity, latency and transactions for independence, fault isolation and targeted scaling. The split is justified by organisation and scaling shape, not by fashion.
Final Takeaway
Choose a monolith (ideally modular) when the team is small, the domain is still changing and you need transactions and speed of development. Choose microservices when many teams must release independently, parts scale or fail differently, and boundaries are stable. Move between them gradually: build a modular monolith, extract the first service for a concrete reason, and show that you can quantify the cost, such as 99.5% availability for a five-service chain.