The modular monolith
One app, with locked walls inside. Learn what a module is, why each one must own its data, and how to make the walls real so they don't erode.
The lesson in text
Click a paragraph to hear it.01412 crossings
StayHub has decided: no twelve microservices. One app, with strong walls inside. So Lina runs a scan of the code, to see who depends on whom today.
The result: 412 places where one team's code reaches straight into another team's code. And the Search team runs 40 database queries directly on Booking's tables.
This week, Booking wants to split one column of its bookings table in two. A small, sensible change. And it would break all 40 of Search's queries.
In this lesson, you'll learn how to build walls inside one app: what a module is, why it must own its data, and how to keep the walls from quietly falling down.
02Locked offices, one building
Think of the back offices of a big hotel. Accounting, Housekeeping, Reservations: each department has its own office, its own files, and a locked door.
Each office also has a small service window. If Housekeeping needs to know a bill, it asks at Accounting's window. It never walks in and edits the ledger itself.
And it's all one building. Walking to another office takes seconds. That's the difference with microservices, where every question means sending a courier across town.
That's a modular monolith: one app, deployed as one unit, with locked walls inside. Each part is a module: it owns its code and its data, and others come in only through its front door.
| In the hotel | Release | Parts talk by | |
|---|---|---|---|
| Plain monolith | One building, no inside walls: anyone opens any file | One deploy | Function calls |
| Modular monolith | One building, locked offices with a service window | One deploy | Function calls through a module's front door |
| Microservices | Separate buildings across town | One deploy per service | Network calls: a courier for every question |
Notice what it is not. It isn't a new technology, and it doesn't need new servers. It's the same app StayHub already runs, the same single release, with one difference: every part has a clear owner, and the doors between parts are respected.
03A module's front door
In the code, a module is a folder, like search, booking or payments. Inside, most of the code is private. Only a few functions are offered to the outside. That small list is the module's public API, its service window.
Asking through it is still a normal call inside the same program. No network, nothing slow. You get the walls of the food hall without the runners.
To design the window, Lina looks at the 40 queries and asks: what question is each one really asking? 31 of them ask the same thing: which rooms are free in these hotels, on these nights? The other 9 count bookings, to rank hotels.
Free rooms aren't really Booking's job, or Search's. They get a new owner: an Inventory module, with a window of just two functions. One says how many rooms are free. The other holds rooms while a guest pays.
src/modules/inventory/index.tsthe front door: all other modules may useavailability(hotelIds, nights)how many rooms are freeholdRooms(booking)keep rooms while the guest payssrc/modules/inventory/internal/…private: the build fails if another module imports itinventory tablesprivate: only Inventory's database login can open themNotice how the walls are drawn: around what the business does, like availability, payments or search. Not around kinds of code, like one folder for every screen and another for every database query. Those cut every feature into pieces no team can own.
04Data has one owner
A wall in the code isn't enough if everyone can still open the same files. So each module also owns its tables. Only Inventory writes the availability data. Nobody else reads it directly.
Then how does Inventory know a room was just booked? Booking announces it: a booking was confirmed. Inventory hears that, and updates its own count. You'll see much more of these announcements, called events, in the next lesson.
And the 9 ranking queries? Search keeps its own small table of booking counts, fed by the same announcements. It can be a few seconds behind, and for ranking hotels, that's fine.
So there are only two honest ways to get another module's data: ask at its window, or keep your own copy, fed by its announcements. Never by reaching into its tables.
05The silent wrong number
Here's why that matters. Every night, a Reports job worked out each hotel's revenue. It did it by reading the Payments team's charges table directly, joined with Booking's tables.
Then Payments improved its own code and started saving new charges in a new table. Their tests were green. The old table still existed, it just stopped getting new charges.
So the Reports job didn't crash. It ran perfectly, and showed revenue 60% too low, three nights in a row. Partner payouts are based on it: about 140 hotels were underpaid.
That's the worst kind of break: a wrong number, with no error. The fix: Reports keeps its own revenue table, fed by an announcement from Payments each time a charge succeeds. And it gets no access to Payments' tables at all.
06Make the walls real
Walls that rely on good intentions fall down. Someone is in a hurry, adds one import just this once, and a year later the walls are gone. That's how StayHub got 412 crossings.
So the build checks them. A boundary check runs with the tests: if code reaches into another module's private folder, the build fails, and the change can't ship. In the database, each module gets its own login that can only open its own tables.
But failing all 412 crossings on day one would block every change. So Lina freezes today's list as a baseline: the old ones are tolerated, no new one is allowed, and each sprint the teams remove a few. The number can only go down.
- Day one412 crossings frozen as the baseline. The check is on: any new crossing fails the build.
- Each sprintTeams remove a few crossings and lower the baseline. It never goes up.
- LaterEach module gets its own database login that can open only its own tables.
This isn't only for small companies. Shopify runs its core as one big modular app, and built its own open-source tool, Packwerk, to check the boundaries between its parts.
07Walls before services
Why walls first, and not services straight away? Because you'll draw some walls in the wrong place. Inside one app, moving a wall is a tidy-up: move some files, change some calls, ship it in one deploy.
Between two services, the same move is a project: two APIs to change, data to move between databases, and releases to coordinate. So you learn where the walls belong while they're still cheap to move.
And when a module does need to leave one day, a clean front door and its own data make it easy: the courier simply takes the place of the walk down the corridor. The price you pay meanwhile: one shared release, and one program, so a crash in one module can still hurt the others.
08Your job as lead
Partners want a revenue dashboard by Friday. Claude Code writes one database query that joins Payments' charges with Booking's and Inventory's tables. "Fast, and it works today," it says. "A separate table would take three more days."
That's the exact kind of query that underpaid 140 hotels. Maybe the dashboard ships three days late. Maybe a smaller version ships on Friday. That's your call, but make it a decision, not a silent coupling.
When Claude Code builds or changes a module, ask: which module owns this data? Does this code go through another module's front door, or reach into its tables? Does the boundary check fail on a bad import? And did it add an exception just to make the check pass?
Let's recap. A modular monolith is one building with locked offices: one deploy, modules with a small front door and their own data. Get data by asking or by keeping your own copy, never by reaching in. And let the build guard the walls. Next: what happens when one booking has to tell four other parts at once.
Check yourself
0/10 answered1What makes an app a modular monolith rather than a plain monolith?
2In the hotel offices picture, what is a module's public API?
3Booking wants to change a column in its own table, and Search reads that table with 40 queries. What makes changes like this safe from now on?
4Payments moved new charges to a new table. The nightly revenue report, which read the old table directly, kept running. What did users see?
5Which is a good way to draw modules?
6Search needs booking counts to rank hotels, and Booking owns the bookings data. What should Search do?
7There are 412 crossings today. Turning on a strict boundary check would block every change. What do you do?
8Claude Code makes the boundary check pass by adding an exception for its new import. What do you do?
9Why draw walls inside one app before splitting into services?
10Claude Code proposes one database query joining Payments', Booking's and Inventory's tables for a Friday dashboard. What's your best move?
Save your progress — create a free account
Free. No card. Your finished lessons follow you to any device, and you can ask questions in the community.
Create my free accountEvery lesson is free while we build. Lock in €9/month for later.
The first 100 members keep the founding price for as long as they stay. Reserving is free: no card, nothing is charged.
See the founding offer