Choosing an architecture from requirements
What you will learn on this page
Section titled “What you will learn on this page”- Why an architecture is chosen from requirements, one part at a time, and not picked by name before the problem is understood.
- Which requirement signals justify each structure in this section: layers, modules, ports, storage patterns, event reactions, user-interface separation, plugins, and a separate process.
- How to use a decision table that maps each signal to a choice, what it costs, and the page that builds it.
- How to justify every added part, and how to notice a part that is not paying for itself.
- How to write a short architecture decision record that says what was decided, why, and what would change the decision.
The problem: an architecture picked by name
Section titled “The problem: an architecture picked by name”A team is asked for the registrar’s enrollment program. Before anyone reads the requirements closely, the plan says:
Every part of it is a real technique, and every part of it is unjustified:
- Nothing in it names a requirement. Nobody can say which requirement would fail if a part were removed, so nobody can say whether it is needed.
- It is hard to undo. Three services and an event bus mean network calls, separate deployments, and data in three places. Enrolling now needs a call over the network to the students service to confirm the student is registered (E2, E8), and another to notifications (E6). Each can fail half-way, after the seat was taken or before, where one program would make two function calls.
- It costs before it pays. One team maintains a program for about 2,000 students. The structure is sized for a problem the registrar’s office does not have.
- “Extensible” is not a requirement. Extensible for which change, by whom? Without an answer, the plugin system has no interface to offer, and it is built around guesses.
The fix is not a better name. It is a different order: read the requirements, start from the smallest design that meets them, and add each part only when a requirement asks for it, writing down which one.
Step 1: classify the requirements by the force they put on structure
Section titled “Step 1: classify the requirements by the force they put on structure”Most requirements say what the program does, and are met by classes and functions. A few say something about how the program must be able to change, be tested, fail, or be split. Those are the ones that decide architecture. For each requirement, ask:
| Question | If yes, it pushes toward |
|---|---|
| Are there rules worth testing without input, output, or storage? | a domain apart from the rest |
| Is there, or will there soon be, more than one way in? | use cases that every way in calls |
| Is a technology (storage, mail, user interface) likely to be replaced? | an interface the core owns, with adapters |
| Are there separate capabilities, each with its own data and reasons to change? | modules by feature |
| Must data survive a restart, or must several changes succeed together? | storage behind repositories, and a transaction boundary |
| Do different audiences need different shapes of the same data? | separate types at each boundary |
| Does input from outside need checking before it reaches the rules? | a validation boundary and an error contract |
| Is there work that follows a command, and must not slow or fail it? | event reactions |
| Does a screen have logic worth testing without drawing it? | MVC, MVP, or MVVM |
| Will other people add features on their own schedule? | a plugin API |
| Must a part be deployed, scaled, or allowed to fail on its own, or run code you do not trust? | a separate process |
A requirement that answers none of these is still important; it just does not need structure beyond good classes. That is most of them.
The decision table
Section titled “The decision table”Each row is a signal from the requirements, the choice it justifies (with the page that builds it), what that choice costs, and what would make it not worth it. The first table shapes the program from the inside; the second covers its edges, and splitting it.
Inside the program:
| Signal in the requirements | Choice | Its cost | Not worth it when |
|---|---|---|---|
| Rules worth testing alone | a domain that knows no input, output, or storage (Layered) | calls passed through layers | there are no rules, or the program is short-lived |
| More than one way in | use cases that every way in calls (Layered) | one more layer to pass through | there is one way in, and it is thin |
| Separate capabilities with their own data | feature modules with public headers (Modular monolith) | a boundary and an interface per module | the program is one capability |
| Technology likely to change; a core worth testing fast | ports owned by the core, adapters outside (Hexagonal) | an interface per port, and mapping at the port | a port has one adapter and no test needs a substitute |
| Many use cases, owned by different people | one interactor per use case (Clean architecture) | about twice the files of layers | create, read, update, delete over a few tables |
| Objects with different owners and lifetimes | one place, main or an App class, that builds and owns them (Composition root) |
wiring to keep in step | a class is not worth it for a few objects with no run-time choices; locals in main do |
| Rules that span several objects | aggregates and domain services (DDD building blocks) | deciding the boundaries | each rule is about one object |
| Data must survive a restart | repositories and a unit of work (Repository and Unit of Work) | a storage interface and its adapters | data is read once from a file and never written |
| Several changes must succeed or fail together | a transaction per use case; an outbox for messages (Transaction boundaries) | a store with transactions, a relay | each request changes one thing |
At the edges, and beyond one program:
| Signal in the requirements | Choice | Its cost | Not worth it when |
|---|---|---|---|
| Different audiences for the same data | separate request, domain, and stored types (DTOs and domain objects) | mapping code | one audience, one shape |
| Input from outside | a boundary that checks shape; rules in the domain (API contracts and validation) | two places to look for a check | almost never; only the amount varies |
| A screen with logic worth testing | MVC, MVP, or MVVM (MVC, MVP, and MVVM) | a view interface or bindings | the screen only shows data |
| Work that follows a command, and must not fail it | event reactions, synchronous or queued (Event-driven components) | harder to follow; ordering and retries | the caller needs the result, or the steps must happen together |
| Other people add features on their own schedule | a versioned plugin API and a registry (Plugin architecture) | a promise to keep across versions | one team writes every extension |
| A part must deploy, scale, or fail on its own; or run untrusted code, or be isolated | a separate process (From classes to components) | network calls that fail, time out, or repeat | one team, one release, and consistency needed across the parts |
Read the tables from the left. Starting from a choice (“we want hexagonal”) and looking for a signal to justify it is how the flawed plan above was written.
The main questions are best asked in an order, because some answers depend on earlier ones:
- Ports protect a domain. If the first question found no rules worth a domain, a port in front of storage has little to protect, and the technology question usually answers itself.
- Modules make splitting possible. The separate-process question is easiest to answer, and to act on, when each module already owns its data and talks through a small interface.
- Storage comes before reactions. Whether a reaction can be lost when the program stops depends on whether the change itself is kept.
Text description of the diagram
A flowchart that starts at “one module, good classes” and asks eight yes-or-no questions in order. Rules worth testing alone? Yes adds a domain apart. More than one way in? Yes adds use cases that every way in calls. Separate capabilities with their own data? Yes adds feature modules. Technology likely to change? Yes adds ports and adapters. Data must survive a restart, or changes succeed together? Yes adds repositories and transactions. Work that follows a command? Yes adds event reactions. Others extend it on their own schedule? Yes adds a plugin API. A part must deploy, fail, or run alone? Yes adds a separate process. After each answer, yes or no, the next question is asked, and the flowchart ends at “record each yes as a decision”. Several rows of the tables are not drawn, because they are asked where their part appears rather than in this order: a screen’s structure, data shapes and validation at each boundary, one interactor per use case, the composition root, and aggregate boundaries.
Diagram source (Mermaid)
flowchart TD
Start["one module, good classes"]
Q1{"rules worth testing alone?"}
Q2{"more than one way in?"}
Q3{"separate capabilities with their own data?"}
Q4{"technology likely to change?"}
Q5{"data must survive a restart, or changes succeed together?"}
Q6{"work that follows a command?"}
Q7{"others extend it on their own schedule?"}
Q8{"a part must deploy, fail, or run alone?"}
Done["record each yes as a decision"]
Start --> Q1
Q1 -->|yes| D["a domain apart"]
Q1 -->|no| Q2
D --> Q2
Q2 -->|yes| U["use cases every way in calls"]
Q2 -->|no| Q3
U --> Q3
Q3 -->|yes| M["feature modules"]
Q3 -->|no| Q4
M --> Q4
Q4 -->|yes| P["ports and adapters"]
Q4 -->|no| Q5
P --> Q5
Q5 -->|yes| R["repositories and transactions"]
Q5 -->|no| Q6
R --> Q6
Q6 -->|yes| E["event reactions"]
Q6 -->|no| Q7
E --> Q7
Q7 -->|yes| X["plugin API"]
Q7 -->|no| Q8
X --> Q8
Q8 -->|yes| S["a separate process"]
Q8 -->|no| Done
S --> DoneA separate process is usually the last question. Everything above it can be done inside one program, where moving code between modules is a refactor; after it, the same move is a migration. Some requirements answer it on the first day, though: code you do not trust, a part that must run on another machine or in another language, or an existing external system the program must talk to. Then the process boundary is a given, and the other questions are asked on each side of it.
Step 2: justify every added part
Section titled “Step 2: justify every added part”For each part you add, you should be able to answer four questions in a sentence each. If one has no answer, the part is a guess.
- Which requirement needs it? Name it: E3, or “the web form in the next release”. “Good practice” is not a requirement.
- What does it make easier? A test that runs without a database, a change that touches one module, a failure that does not reach the user.
- What does it cost? Files, interfaces, copies, calls that are harder to follow, things to keep in step.
- What would make you remove it? If the answer is “nothing could”, the part is not being judged by the requirements anymore.
The second and third questions are the trade-off that the cost of abstraction page puts in one test: an interface without variation, and without a test that needs a substitute, is indirection that only costs.
The enrollment program, decision by decision
Section titled “The enrollment program, decision by decision”The section’s running example shows the method end to end. Its requirements, E1 to E8, describe courses, seats, waitlists, and notices. Its scale is one team, one office, and about 2,000 students, and seats and the waitlist of a course must always change together.
| Requirement or change | Decision | Where it is built |
|---|---|---|
| E3 to E5: seats, waitlists, promotion | a domain::Course that keeps its own rules, tested alone |
Layered, DDD building blocks |
| E4, E8: refuse with a reason, change nothing | checks at the boundary, invariants in the domain | API contracts and validation |
| One team; seats and waitlist change together | one program, split into modules, not services | From classes to components, Modular monolith |
| Planned: data survives a restart | a storage port, repositories, one transaction per use case | Hexagonal, Repository and Unit of Work, Transaction boundaries |
| Planned: a web form replaces the console | use cases the form calls as the console did, so the change is one new adapter; separate request types | Hexagonal, DTOs and domain objects |
| E6 and planned: notices must not slow enrollment | notices as reactions to domain events, queued | Event-driven components |
| Planned: departments add their own rules | modules if the program’s team writes them; a plugin API if departments do | Modular monolith, Plugin architecture |
What the requirements did not ask for, and what was left out:
- Separate services. One team, one release, and a consistency rule inside one course all point to one program.
- Clean architecture’s full set of boundaries. The clean architecture page built it and counted: 25 files against the hexagonal version’s 17, for the same behaviour. Five use cases and one team do not need an interactor and input boundary per use case.
- MVVM or MVP. The console prints replies; it has no screen logic worth testing alone. A rich screen would change that answer.
- A plugin API for report formats, until someone asks. The running example never says who adds report formats. While one team writes them, a map of makers, as on Factory Method, is enough. The plugins page assumes departments write their own, and that assumption is what justifies its API.
The last row of the table is the interesting one: the same requirement, “departments add their own rules”, leads to two different designs depending on a question it does not answer: who writes the rules? That is a clarification question, not an architecture one, and it must be asked before the decision is made.
Recording decisions
Section titled “Recording decisions”A decision that is not written down is made again, often differently, by the next person who does not know why. An architecture decision record (often called an ADR) is a short note, kept with the code, that records one decision. It has a few parts:
- Title and status: what was decided, and whether it is proposed, accepted, or replaced by a later record (usually called superseded).
- Context: the requirements and constraints that forced a choice, by name.
- Decision: what was chosen, in one or two sentences.
- Alternatives: what else was considered, and why not.
- Consequences: what gets easier, what gets harder, and what would make you revisit it.
Two of the enrollment program’s decisions, written this way; its other records, such as the ones for storage and the web form, are not shown. These are records, not code:
ADR-1 One program, split into modules, not separate servicesStatus: accepted
Context: One team maintains the program for one registrar's office,about 2,000 students. Seats and the waitlist of a course must alwayschange together. Planned changes: storage, a web form, email and SMSnotices, department rules. None asks for separate deployment.
Decision: Build one C++ program. Split it into feature modules(students, enrollment, notifications, console), each a library witha public header directory. The build and CTest refuse includes ofanother module's private headers and of modules not linked; changesto the build files are reviewed for new dependencies.
Alternatives: Services per feature: rejected; enrolling would neednetwork calls to check the student and to send notices, each ofwhich can fail half-way, and nothing needs separate deployment. One module with no boundaries: rejectedonce notifications and storage are added; changes would crosseverything.
Consequences: A change across modules is one function call and onetransaction. Modules can be tested alone. Revisit if notificationsmust keep running while enrollment is upgraded, or if a second teamtakes over a module with its own release.ADR-4 Notices are queued reactions to domain eventsStatus: accepted
Context: E6 says students are told about seats and waitlist places.Planned: email and SMS must not slow enrollment, and a mail serverthat is down must not fail an enrollment that succeeded.
Decision: The course records events. After the change is made (oncestorage exists, after the transaction commits), they are publishedto a queued dispatcher; the notifier subscribes.Failed deliveries are kept and retried, with a limit.
Alternatives: Calling the notifier from the use case: rejected; amail failure would fail the command. Synchronous events: rejected formail, because handlers run inside the command.
Consequences: Enrollment does not wait for mail. Notices can arrivelate, and the order of follow-up events must be tested. Events stillqueued are lost if the program stops, unless they are stored withthe change (an outbox). Revisit if a notice must be confirmed beforethe reply is sent.- One decision per record, numbered, so a later record can say “supersedes ADR-4” instead of editing history.
- Name requirements in the context, so a changed requirement leads straight to the decisions it affects. That is traceability for structure.
- Write the revisit condition. It turns “we always did it this way” into a check anyone can make.
- Keep it short. A record nobody reads is no better than none. Half a page is usually enough.
Common mistakes
Section titled “Common mistakes”- Choosing by name first. “We use hexagonal” before reading the requirements leads to ports with one adapter and no test that needs a second.
- Copying the structure of a large system. A design for many teams and many releases puts their costs on a program that has neither.
- Treating every possible change as a requirement. “What if we change databases?” is a requirement only if someone expects it. Otherwise, keep the parts that would make it cheap later, such as rules out of the storage code, and add the interface when it is asked for.
- Splitting into services to get modules. A module boundary in one program gives most of the separation, with none of the network failures. Separate processes are for separate deployment, scaling, or failure.
- Never removing a part. A part added for a requirement that was dropped is now only cost. The decision record’s revisit condition is the reminder.
- No record of why. The next person cannot tell a deliberate choice from an accident, and either keeps a part that no longer pays, or removes one that does.
When the requirements change
Section titled “When the requirements change”- A requirement is added: run the questions again for it alone. Most additions answer no to all of them, and need only classes.
- A requirement a decision named is dropped: look up the records that name it. If the part no longer has a requirement, plan its removal.
- The scale changes, for example a second campus with its own team: the “separate process” question can now be yes for part of the program. A module that owns its data and talks through events or a small interface is the easiest to move out.
- A decision turns out wrong: write a new record that replaces the old one, and say what was learned. The old record stays, so the history makes sense.
Exercise
Section titled “Exercise”Exercise: Choose an architecture for three requirement sets
For each system, run the questions from Step 1 and the decision table. Say which parts you would add, which you would leave out and why, and write one decision record for the most important choice, numbering each system’s records from 1. One team builds each system in C++.
A. Shelf labels. A librarian runs a command-line tool once a month. It reads a CSV file of new books and writes a file of labels in one fixed layout for one printer. The layout changes about once a year.
B. Clinic appointments. Reception books, moves, and cancels appointments. A doctor cannot have two appointments at once, and a cancellation less than 24 hours ahead is recorded as late. It starts with a console at the reception desk; patients will book on the web next year. Bookings must survive a restart. Patients get an SMS reminder the day before, and a failed SMS must not affect booking.
C. Image editor. A desktop editor has a canvas, a layer list, and a filter panel. Its team wants to test what the screens show without drawing them. Other companies sell filters, built against an interface the editor publishes, and customers install them without a new editor release.
Show the explained answer
A. Shelf labels. Almost nothing. There are no rules worth a
domain layer, one way in, no technology expected to change, and no
data to keep. One module: a function that parses a CSV line into a
Book value, a function that lays out a label, and main. The
parsing and layout functions are tested directly. Left out: layers,
ports, and a plugin for the layout, since one person changes it once
a year.
Labels ADR-1 One module of functions, no layersStatus: acceptedContext: One user, run monthly, one input and one output format; thelayout changes yearly, by the team.Decision: One program: parse, lay out, write. Parsing and layout areseparate functions with unit tests.Alternatives: Layers or ports: no rules to protect and nothing toreplace. A layout plugin: nobody else writes layouts.Consequences: Short and easy to read. Revisit if a second printer,a second layout per department, or a second user interface appears.B. Clinic appointments. Real rules (no double booking, late cancellation) are worth a domain tested alone. A doctor’s schedule that refuses overlapping appointments is a natural aggregate, and the boundary for conflicts: reception and the web can book the same doctor at once, so a booking that loses a conflict reruns its use case, as on the transactions page. “Less than 24 hours ahead” needs the time, so a clock sits behind a port and tests set it. A second way in is planned, so use cases sit in front of the domain, and the console and the web both call them. Storage must survive a restart, so repositories, a unit of work, and one transaction per use case. A move between two doctors changes two schedules, and the appointment must not be lost or held twice, so that use case keeps both changes in one transaction: a deliberate exception to one aggregate per transaction, recorded as such. The web form gets its own request types and a validation boundary.
The reminder is not a reaction to a command: it is due a day before the appointment, whenever that was booked. So it is a scheduled job, another way in, that calls a “send tomorrow’s reminders” use case once a day. It reads the appointments as they are then, so a cancelled or moved appointment is never reminded at its old time, and it records each reminder sent. The SMS sender is behind a port. Left out: event reactions and an outbox (nothing has to follow a command), separate services (one team, consistency needed), plugins (nobody else extends it), and clean architecture’s per-use-case boundaries, unless the use cases grow and are owned by different people.
Clinic ADR-1 Reminders are sent by a daily job from stored appointmentsStatus: acceptedContext: Day-before SMS reminders; a failed SMS must not affectbooking; appointments can be cancelled or moved; bookings survive arestart.Decision: Once a day, a job calls a use case that reads tomorrow'sappointments, sends a reminder for each one not yet reminded throughan SMS port, and records it as sent.Alternatives: Sending when booking: too early, and a failed SMS wouldfail the booking. A queued reaction or an outbox entry per booking:every cancel and move would have to find and void or reschedule it.Consequences: Booking never waits for SMS. A failed send is triedagain on the next run while the appointment is still ahead. If theprogram stops between sending and recording, one reminder can besent twice; recording first would risk sending none, and a repeat isthe smaller harm here. Revisit if reminders must go out at a settime before each appointment, or if patients must confirm by reply.C. Image editor. The screens have logic worth testing without
drawing, so MVVM or MVP: view models for the layer list and filter
panel that tests can drive. Filters are written by other companies on
their own schedule and installed without a release, so a versioned
plugin API, loaded at run time from shared libraries: discovery in
a plugins directory, an extern "C" entry point, a boundary that
does not depend on matching compilers (often a C interface), a
version check before creating a filter, and a plan for a filter that
crashes. Left out: storage patterns beyond saving a file, and
separate services.
Editor ADR-1 Filters are dynamically loaded plugins with a C boundaryStatus: acceptedContext: Third parties sell filters; customers install them withouta new editor release; the editor and a filter may be built withdifferent compilers.Decision: Filters are shared libraries in a plugins directory, eachexporting one extern "C" function that returns a versioned table ofC functions. The editor wraps it in a C++ class.Alternatives: Static plugins: needs a release per filter. A C++class across the boundary: requires matching compilers and standardlibraries.Consequences: The editor must keep old API versions working, and afilter can crash the editor. Revisit if filters must run untrusted,which would move them into a separate process.Summary
Section titled “Summary”- Start from the smallest design that meets the requirements: one module with good classes.
- Add each part only for a requirement, read from the signal to the choice, never from the choice to a signal.
- For every part, name the requirement, what it makes easier, what it costs, and what would make you remove it.
- Do early what is expensive to add later, such as keeping rules out of the input and output code; wait for what is cheap to add.
- A separate process is usually the last question, because it is among the hardest decisions to undo, unless a requirement such as untrusted code answers it on the first day.
- When a requirement leads to two designs, it is missing an answer: ask the question, then decide.
- Write each decision as a short record: context, decision, alternatives, consequences, and when to revisit it.