Modular Monolith First: Rooms With Doors, and the Honest Bill for Microservices
TicketHub starts as one app with five modules that own their tables. At forty engineers, a full split would cost broken joins, half-done transactions, 99.6% availability across four hops and twelve servers. Splitting only the AI assistant costs four.
What you will learn
- Build a modular monolith where each module owns its tables and others call its API
- Read the three bills of microservices: data, network hops and running costs
- Work out the availability of four services in a row (99.6%)
- Use four questions to split one module out instead of five
Before you start
- What a database transaction is
- Choosing an Architecture Style
Every team meets the question sooner or later: one app, or many small services? Here's where we'll end up: start as one app with strong walls inside, and split a piece out only when it can pay its own bill.
One box
Meet TicketHub version one, a support-ticket app with an AI assistant, built by a team of four. Inside there are five parts: tickets, users, notifications, reports, and the AI assistant that drafts replies to customers. Underneath, one database.
A request enters Tickets, asks the AI assistant for a draft reply, reads the database, and the answer comes back. Each hop between parts is an in-process call, one part calling another inside the same app. The whole box is one deployable: the one package you build and put on a server.
Two kinds of call
Why does that matter? A function call inside one app takes well under a millisecond. A network call between two separate apps takes a few milliseconds, and it can fail; the other side might not answer at all. These are relative sizes, but the shape is the point: calls inside one box are cheap and safe, calls across a network cost time and can break.
Rooms with doors
One box can still be a mess, with every part reaching into every other part's data. So give it structure:
- Each part gets its own tables in the database.
- Walls go up between the parts. A part with a wall around it is a module, a self-contained piece with one job.
- Every wall gets a door: the module's API, the few functions it lets others call.
Watch Reports try to read the Tickets tables directly. Blocked. It knocks on the Tickets door instead. That's the rule of a modular monolith: each module owns its tables, and everybody else asks through the door.
What one box gives you
- One thing to build, one thing to test, one thing to deploy.
- Easy debugging: one process, one log, the whole story in one place.
- Transactions just work. Close a ticket and write an audit row, and both are saved together, or neither is.
- The cheapest to run: one server, one database.
The team grows
Two years on, TicketHub has forty engineers in five teams, and it's still one app. Every team pushes into the same box with one deploy button, so each release waits in a queue for the one before it. The AI assistant team changes its prompts five times a day, and every small change waits at the back of that queue. The model calls are slow and heavy, so when the assistant gets busy the whole box slows down, and the ticket list feels it too.
This is the moment people start saying "microservices". In a full split, the walls disappear, the five pieces move apart, and each pulls its own small database along. Each piece is now a service: a small program that does one business job and is deployed on its own, and only it touches its database. Independence looks great. Now read the bill.
Bill one: data
Tickets and users now sit in two databases, so one query can't join the tickets table with the users table any more. You call the other service and put the pieces together yourself.
Transactions break too. Close a ticket in Tickets, then update the agent's stats in Users. The first step saves, the second fails. There's no rollback across both, so you're left half done. One app does both in a single database transaction. Separate services can't, and you have to design for it.
Bill two: the maths of hops
Say each service is up 99.9% of the time, and a request needs four of them in a row: the gateway, Tickets, Users and Notifications. It works only if all four work, so multiply:
| Services in a row | Availability |
|---|---|
| 1 | 0.999 |
| 2 | 0.999 × 0.999 = 0.998 |
| 3 | × 0.999 = 0.997 |
| 4 | × 0.999 = 0.996, so 99.6% |
Each looked great alone; together they're worse than any one. Over a 30-day month, 99.9% allows about 43 minutes of trouble. 99.6% allows nearly three hours.
Time adds up too. Four hops at an assumed 5 ms each is 20 ms before any real work starts.
Bill three: running it
- Five services means five build pipelines, five dashboards, five on-call rotas.
- One request travels through five services, so you chase its id through five logs.
- Servers: with two copies of everything for safety, the monolith needs 2. Five services plus a gateway need 12. That's an assumed example, but the shape is real.
What the bill buys
- Deploy one service alone. Notifications ships a new version and everyone else stays put.
- Scale one service alone. At month end Reports runs three copies; everything else runs one.
- A crash stays local. The AI assistant goes down, and tickets still open and save.
- Each team owns its service, when to ship it and how to run it.
Good things. The question is whether you need them yet, and for which piece.
Split one, not five
Ask four questions about one module at a time:
| Question | AI assistant | Reports |
|---|---|---|
| Has its edge stopped moving? (A boundary that changes weekly costs you twice.) | Yes, stable for months | |
| Is there a real reason to split? | Yes: own scaling, own deploys for prompt changes | No: no special scaling or deploys |
| Can the team run it in production, day and night? | Yes | |
| Are monitoring and tracing ready before the split? | Yes |
So only the AI assistant moves out, with its own database. Its door becomes a network call, and servers go from 2 to 4, not 12. Reports stays inside for now. You can always split later; joining services back together is much harder.
Three shapes
| Big ball of mud | Modular monolith | Microservices | |
|---|---|---|---|
| Deploys | One | One | Many |
| Calls | Fast | Fast | Network hop for each |
| Data | One database, no walls | One database, clear owners | Database per service |
| Team independence | Low | Medium | High |
| Cost to run | Cheap, painful to change | Cheap | High |
The modular monolith sits in the useful middle: organised, and cheap. For how to make the first choice from scratch, see choosing an architecture style.
Recap
- Start with rooms and doors: modules that own their tables and expose a small API.
- Microservices send three bills: lost joins and transactions, multiplied availability and latency, and everything run five times.
- They buy independent deploys, scaling, fault isolation and team ownership.
- Split one module when it passes all four questions, and keep the rest inside.
Try it yourself
Work out the availability of a request that crosses six services at 99.9% each, and how many minutes of trouble that allows in a 30-day month. Then decide which one module in your own app would pass the four questions.
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.
