Two ways to organise a system
Every product is made of parts: orders, payments, email, search. The question is whether those parts live inside one program or in many separate programs that talk over the network.
- A monolith is one program. All the parts are built together, deployed together and run together.
- Microservices split the parts into separate small programs. Each has its own code, its own database and its own deploy.
Neither is the modern one or the old one. They trade one kind of difficulty for another.
The same request, both ways
A customer presses Buy. Three things must happen: create the order, charge the card, send a receipt.
A customer buys something. Follow the request.
Monolith
A customer buys something. Follow the request.
Inside one program, one part uses another by calling a function. That call takes almost no time, it cannot be lost, and all the changes can be saved in a single database transaction: either everything is saved or nothing is.
Good at
- Simple to build, test, deploy and debug: there is one of everything
- Calls between parts are fast and cannot fail halfway
- One database, so data stays consistent without extra work
Where it bites
- Everyone changes and deploys the same program, so large teams get in each other's way
- The whole program scales together, even if only one part is busy
- One bad bug or memory leak can take everything down
Microservices
A customer buys something. Follow the request.
Now each arrow between services is a trip across the network. It takes milliseconds instead of nanoseconds, and it can time out. In the animation the card was charged and the receipt failed. There is no shared transaction to undo, so the code has to handle being half done. This is the central difficulty of microservices, and it shows up everywhere.
What you get in return is independence. The payments team can deploy ten times a day without asking anyone. Search can run on fifty machines while email runs on two.
Good at
- Teams build and deploy without waiting for each other
- Each service scales by itself, on hardware that suits it
- A failure can be contained to one service, if callers are written to cope
Where it bites
- Every call between services can be slow or fail, and the code must handle it
- Data is spread over many databases, so keeping it consistent is your job
- Much more to run: many deploys, many dashboards, and tracing a request across services
What actually decides it
Microservices are mainly a solution to a people problem, not a technical one. Three questions matter.
| Question | Points to a monolith | Points to services |
|---|---|---|
| How big is the team? | A handful of people who all talk to each other | Many teams that cannot all coordinate |
| How often do you deploy? | A few times a week is fine | Teams are queueing behind each other |
| Do parts scale differently? | Load is fairly even | One part needs far more, or very different, hardware |
Be honest
Answer for the system you have today, not the one you hope to have.
How many engineers work on this system?
Notice where the first question sends most teams. A well-organised monolith carries a company a long way, and it can be split later. Going the other way, merging services back together, is much harder.
Key takeaways
- A monolith is one program. Microservices are many small ones talking over the network.
- Inside a monolith, calls are fast and safe, and one transaction keeps data consistent.
- Between services, every call can fail, and work can be left half done.
- Microservices mostly solve a team problem: many people needing to deploy independently.
- Start with a well-organised monolith and split out a part when there is a clear reason.