System Design: Foundations
Abstraction and decomposition
How to split a system into parts you can understand one at a time, and how to hide detail behind boundaries without hiding the things that matter.
Think about how you give someone directions to the airport. You do not describe every junction. You say “take the motorway, follow the signs, long-stay car park is the second exit”. The driver does not need to know how the motorway was built, and you do not need to know which gear they will use.
Now think about how you would build the airport. Nobody holds the whole thing in their head. There are terminals, baggage, air traffic control, fuel and security, and each team works on its own piece while agreeing on how the pieces meet. (The one place where everyone ignores the abstraction is the car park, where everyone becomes an expert in exactly where the second exit is.)
Those are the two moves of this lesson, and they work as a pair.
Abstraction is deciding which details to hide so a part can be used without being understood. Decomposition is splitting a system into parts so each one can be understood, built and changed on its own.
Why they exist
People can hold only a handful of things in mind at once. A system with two hundred moving parts is fine as long as nobody needs to think about more than seven of them at a time. Abstraction and decomposition are how we arrange that.
They also serve a second goal that is easy to forget: limiting the cost of change. Back in lesson one I described architecture as the decisions that are expensive to change. A good split makes most decisions cheap, because a change stays inside one part. A bad split makes a small change ripple through five.
So the question to ask of any boundary is not “does this look tidy?” but “when this thing changes, how much else has to change with it?”
Abstraction: a promise, not a disguise
An abstraction has two sides:
- The interface: what the part promises to do, and what it needs from you.
- The implementation: how it keeps that promise, which you are allowed not to know.
You use abstractions all day. requests.get(url) hides sockets, TLS and redirects. A PostgreSQL connection hides disk layout and query planning. A Kubernetes Deployment hides which node a pod landed on.
The useful way to think about it is that an abstraction is a promise. “Give me a URL, I will give you a response or an error.” If the promise is small and stable, callers can depend on it, and the inside can be rewritten freely.
Abstractions leak
Here is the honest part. Every abstraction hides something, and sometimes the hidden thing matters anyway.
- An ORM hides SQL, until a page makes 400 queries because of a loop.
- A managed queue hides brokers, until a message is delivered twice and you need to know why.
- A
Servicein Kubernetes hides pod addresses, until a pod is slow to become ready and traffic goes to it anyway.
I do not read this as “abstractions are bad”. I read it as: know what is underneath well enough to debug it, and do not pretend the layer is perfect. A leaky abstraction that is mostly right still saves a lot of thinking.
The cost of getting it wrong
There are two opposite mistakes, and they both hurt:
| Mistake | What it looks like | What it costs |
|---|---|---|
| Too little abstraction | Business logic calls the cloud SDK, the database driver and the HTTP client directly, everywhere | Every change touches many files. Testing needs the real world. |
| Too much abstraction | A Repository over a Gateway over a Provider, each with one implementation |
More code to read, and nobody can find where anything actually happens. |
A rule of thumb I find useful: do not add an abstraction for variation you do not have yet. If there is exactly one database and no plan to change it, a thin wrapper around the queries is enough. If there are already two (say, a real provider and a fake one for tests), an interface earns its place.
Decomposition: where to cut
If abstraction is how a part looks from outside, decomposition is how you decide what the parts are. There is no single correct way to cut a system. There are a few common ones, and they give very different results.
| Way to split | Example | Good when | Watch out for |
|---|---|---|---|
| By layer | UI, API, business logic, database | The system is small and one team owns all of it | A feature change touches every layer, so teams wait on each other |
| By feature or business capability | Billing, accounts, search | Different features change at different speeds or belong to different people | Shared data tables quietly tie the parts together again |
| By data ownership | Whoever writes a table is the only one who owns it | You want clear answers to “who is allowed to change this?” | Reports that need data from several owners get awkward |
| By rate of change | Stable core, fast-moving rules around it | Some parts churn weekly and others have not moved in years | Hard to see in advance, easier to see in the git history |
| By failure and load | The part that spikes is separate from the part that must never go down | A busy part would otherwise take down a critical one | More moving pieces to deploy and watch |
Most real systems mix these. A web app is often cut by layer inside each feature, and by feature across the whole. That is fine. What matters is that you can say why you cut it where you did.
Questions that find good seams
When I am unsure where to cut, these help:
- What changes together? Things that always change in the same commit probably belong together.
- What changes for different reasons? If a pricing rule and a PDF layout live in the same file, they will keep getting in each other’s way.
- Who owns it? A boundary that matches a team boundary tends to survive. One that cuts through a team tends to be ignored.
- What must survive a failure of the neighbour? If the page must still load when recommendations are down, recommendations should not be inside the page.
- What would we want to swap or test in isolation? That is a seam.
Decomposing is not the same as deploying separately
This is worth saying plainly, because it gets mixed up. You can decompose a system into well-separated parts and still ship them as one process. A modular monolith is exactly that: clear boundaries, one deployment. Splitting into separate services is a further step with its own costs (network calls, partial failure, versioning). The lesson on microservices versus modular monoliths comes later. For now, the point is that the thinking comes first, and the number of deployables comes second.
A worked example: a preview environment cleaner
Imagine a small tool that removes old preview environments from a Kubernetes cluster. A merge request opens, CI creates a namespace for it, and someone has to delete it when it goes stale. (This is a made-up sketch to show the method, not a description of any particular real tool.)
The first version is a single script: list namespaces, look at labels, check whether the merge request is closed, delete if so. That is fine. It works, and for a while it is the right design.
Then the questions arrive. Can it skip environments someone has pinned? Can it run in dry-run mode? Can we test the rules without a cluster? Can it also clean up databases created per preview? Now decomposition starts to pay.
Here is one way to cut it:
- Source: “what preview environments exist, and what do we know about each?” Hides the Kubernetes API.
- Policy: “given what we know, which should go?” Pure rules: age, merge request state, a
keeplabel. Hides nothing, and needs nothing from the outside world. - Actions: “remove this one.” Hides namespace deletion, and later maybe database cleanup.
A sketch of how the middle part might look in Python:
from dataclasses import dataclass
from datetime import datetime, timedelta
@dataclass
class Preview:
name: str
created: datetime
mr_open: bool
pinned: bool
def should_delete(p: Preview, now: datetime) -> bool:
if p.pinned:
return False
if p.mr_open:
return now - p.created > timedelta(days=14) # open but abandoned
return now - p.created > timedelta(hours=24) # closed MR: short grace period
Notice what the abstraction bought. should_delete takes a plain Preview and returns a bool. You can unit test every rule with no cluster, no GitLab and no network. The Source can be replaced by a fake that returns three hand-made Preview objects. And if the cluster API changes, only the Source changes.
Now the three questions from lesson one:
- More load? Here “load” is the number of environments. The only part that scales with it is Source (listing is a paged API call) and Actions (many deletes). Policy is a loop over a list. If it ever matters, it is clear which parts to look at.
- Something breaks? If Actions fails halfway through, the next run will just find the leftover environments and try again, as long as the policy depends on state rather than on “what I did last time”. That is a design property you can only reason about because the parts are separate. If Source returns garbage, a deletion bug could be catastrophic, so the decision to dry-run first belongs between Policy and Actions.
- What do we give up? More files, three small interfaces to maintain, and a little indirection when reading the code. For a ten-line script that is a bad trade. It starts being a good trade the day you want to test the rules or add a second kind of cleanup.
That last point is the honest summary of the whole lesson: the single script and the three-part design are both right, at different times.
Common mistakes I want to avoid
- Cutting by technology instead of by reason. “The Postgres part” and “the RabbitMQ part” are not capabilities. They are tools.
- Splitting too early. Boundaries guessed before you understand the problem tend to be wrong, and wrong boundaries are costly to move. It is often cheaper to split after the code has taught you where the seams are.
- Abstractions with one user and no reason. If nothing varies and nothing needs a test seam, the layer is just reading overhead.
- Interfaces shaped like the implementation. If the interface exposes a SQL cursor or a cloud SDK type, nothing is really hidden. Describe what the caller needs, not how it is done.
- Hiding things that must be visible. Timeouts, failures and cost are not details. A call that looks like a local function but can take ten seconds or fail halfway is a lie.
- Shared data behind separate parts. If two “independent” modules read and write the same tables, they are one module with extra steps.
Next up: coupling and cohesion, the two measures that tell you whether the boundaries you just drew are any good.