Microservices or monolith
easy 28 minA monolith is one program, built and released as a whole.
See it run
One order in both styles: how it runs, how it scales, how it fails, how a change goes live, and how to move from one to the other.
Operations
One order, one program
The monolith on its own: function calls, one database, one transaction.
Step 1 of 4
The request arrives
A shopper places an order. The user, order, payment and stock modules are all in one program.
Step 1: The request arrives
def place(order): with database.transaction(): # all or nothing check_user(order.user) take_stock(order.items) insert_order(order) return "confirmed"- Wait for
- One commit
- You run
- 1 program
- Calls between modules
- inside the process
One transaction over several modules is the quiet superpower of a monolith.
In the real world
The same microservices or monolith doing real jobs. Pick one and step through it.
The stock code leaks memory. What do users see in each style?
Step 1 of 3
Monolith: memory grows
- Copy 1 memory
- 88%
- Copy 2 memory
- 91%
Every copy has the leak, because every copy has the stock module.
Step 1: Monolith: memory grows
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Services that share tables
criticalNobody can change the schema alone, so the services must be released together.
Wrong# in the payment servicedb.query("SELECT * FROM orders WHERE id = ?", id)Right# in the payment serviceorder_api.get(id) # or a local copy from eventsSave, then publish
criticalA crash between the two lines leaves an order nobody hears about.
Wrongdb.insert(order)broker.publish("order created", order)Rightwith db.transaction(): insert(order) insert_outbox("order created", order)# a relay publishes the outboxA fallback that invents facts
majorIf stock cannot be asked, answering available sells goods that do not exist.
Wrongexcept BreakerOpen: return Stock(available=True)Rightexcept BreakerOpen: return Stock(known=False) # caller decides: wait, or sell laterSplitting before knowing the domain
majorWrong boundaries become network contracts, and moving them needs several teams.
Wrongservices = [make_service(table) for table in tables]Rightmodules = bounded_contexts(business)# extract one when it has a reason
Interview tips
How to explain it
- Monolith: one unit of release, possibly many copies.
- Microservices: own process, own release, own data.
- The split trades function calls for network calls.
Memory trick: SPLIT
- Single responsibility, Polyglot, Loose coupling.
- Independent release.
- Team ownership.
Reach for services when
- Teams block each other.
- One part needs different hardware or protection.
- You can run the platform.
Stay together when
- The team is small.
- The domain is still moving.
- One transaction covers what you need.