Quick revision
The short answer
A fitness function is an automated check that protects an architectural property, such as layering, module boundaries or latency, so that it does not erode as the code changes. In a Spring Boot codebase, two practical ones are an ArchUnit test that forbids web controllers from calling repositories directly, and a Spring Modulith test that verifies module boundaries; a performance test in CI that fails when p95 latency exceeds a threshold is a good third. They run in CI, so a violation fails the build and is caught in review rather than discovered months later.
What to remember
In this answer, we saw that fitness functions are automated checks protecting architectural properties over time. We implemented an ArchUnit rule forbidding controllers from using repositories, together with a layered architecture rule, and a Spring Modulith test verifying module boundaries, and looked at a latency budget check as an operational example. Running them in CI turns architectural drift into a failing build that is caught in review.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A fitness function is an automated check that protects an architectural property, such as layering, module boundaries or latency, so that it does not erode as the code changes. In a Spring Boot codebase, two practical ones are an ArchUnit test that forbids web controllers from calling repositories directly, and a Spring Modulith test that verifies module boundaries; a performance test in CI that fails when p95 latency exceeds a threshold is a good third. They run in CI, so a violation fails the build and is caught in review rather than discovered months later.
Why this question is asked
Architecture decisions are usually written once and then slowly eroded by hundreds of small, reasonable-looking changes. A controller calls a repository 'just this once', a module reaches into another's internal class, an endpoint gets a little slower each release. Nobody decides to break the architecture; it just happens. Interviewers ask about fitness functions to see whether we know how to protect architecture with automation rather than hope. In this answer, we will recap the idea from evolutionary architecture, implement an ArchUnit rule and a Spring Modulith check, add a latency check, and see how to run them in CI.
1A Quick Recap of Fitness Functions
The term comes from the book Building Evolutionary Architectures by Neal Ford, Rebecca Parsons and Patrick Kua. It borrows from evolutionary computing, where a fitness function measures how close a candidate solution is to the goal. In software architecture, a fitness function is any objective, preferably automated, check that tells us whether the system still has a property we care about.
The properties vary: structural ones (which packages may depend on which), operational ones (latency, error rates, startup time), security ones (no known critical vulnerabilities in dependencies) and data ones (no breaking schema change). The key idea is that an architectural rule which is only written in a document will drift, while one checked on every build stays true or fails loudly.
| Property | Fitness function | Runs |
|---|---|---|
| Layering | ArchUnit: controllers do not use repositories | every build |
| Module boundaries | Spring Modulith verify() | every build |
| Latency | load test: p95 under 200 ms | nightly or per merge |
| Dependency security | scan fails on critical CVEs | every build |
| API compatibility | contract tests against consumers | every build |
2Fitness Function 1: Layering Rules With ArchUnit
ArchUnit is a Java library that imports our compiled classes and lets us write rules about their structure as ordinary JUnit tests. Suppose our architecture says controllers handle HTTP, services hold business logic and transactions, and repositories access the database. A controller that injects a repository skips the service layer, along with its validation and transaction boundaries.
Notice that the rule is written in the language of the architecture, not of individual files. When a developer breaks it, the failure message explains exactly which rule and which class, so the conversation in review is about the design, not about style. Repository here is Spring Data's marker interface, which every Spring Data repository extends.
3Fitness Function 2: Module Boundaries With Spring Modulith
In a modular monolith, the important property is that modules use each other only through their public API. Spring Modulith treats each direct sub-package of the main application package, such as com.example.shop.orders and com.example.shop.billing, as a module. Types in a module's base package are its API; types in sub-packages, such as billing.internal, are internal. A single test verifies the whole structure.
The advantage over a hand-written ArchUnit rule is that Spring Modulith derives the rules from the package structure, so new modules are covered automatically. It also detects cycles between modules, which are a strong sign that two modules are really one. Check your Spring Modulith version for the exact output format and options such as explicitly allowed dependencies.
4A Third Example: Latency Budgets in CI
Structural rules are cheap to check. Operational properties need more setup but are equally important. A latency fitness function runs a short load test against the service, for example started with Testcontainers dependencies in a CI job, and fails if the p95 latency of key endpoints exceeds a budget, say 200 milliseconds at 100 requests per second. Tools like Gatling express this as an assertion, such as a check that the 95th percentile response time stays below 200 milliseconds, which fails the run when broken.
Because performance tests are noisy and slower, teams usually run them nightly or on merges to the main branch rather than on every pull request, and set the threshold with some margin. The point is not perfect measurement but catching the change that suddenly adds 300 milliseconds before it reaches production.
5Running Fitness Functions in CI
A fitness function that runs only on a developer's laptop protects nothing. ArchUnit and Spring Modulith tests are ordinary JUnit tests, so they run with mvn test or gradle test in every pull request pipeline, taking a few seconds. When they fail, the pull request cannot be merged, and the violation is discussed in review, which is the cheapest moment to fix it.
Two practical tips help adoption in an existing codebase. First, if there are already violations, ArchUnit's FreezingArchRule records the current ones and fails only on new violations, so we can introduce a rule without fixing everything first. Second, each rule should have a short comment or a link to the ADR that explains why it exists, so that someone who hits it understands the reason instead of looking for a way around it.
- Developer opens a pull request
- Unit tests, ArchUnit and Modulith checksseconds
- Any rule violated?YesBuild fails; discussed in reviewNoMerge allowed
- Nightly load test on mainp95 budget per endpoint
- Budget exceeded?YesAlert the team with the offending change rangeNoDone
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Are fitness functions only for big systems?
No. A single ArchUnit test protecting the layering of a small service costs a few minutes to write and runs in seconds. The bigger and older the codebase, the more value they give, but the habit is cheapest to start early.
What if a rule is wrong for a specific case?
Then either the rule needs refining, for example allowing a specific package, or the exception should be explicit in the rule with a comment explaining why. Silently disabling the test removes the protection for everyone. If many exceptions pile up, revisit the architectural decision behind the rule.
How do fitness functions relate to ADRs?
An ADR records a decision and its reasons; a fitness function enforces the part of that decision that can be checked automatically. Linking them means a failing test leads the developer to the reasoning, and changing the rule becomes a conscious decision recorded in a new ADR.
7Conclusion
In this answer, we saw that fitness functions are automated checks protecting architectural properties over time. We implemented an ArchUnit rule forbidding controllers from using repositories, together with a layered architecture rule, and a Spring Modulith test verifying module boundaries, and looked at a latency budget check as an operational example. Running them in CI turns architectural drift into a failing build that is caught in review.
Open with
A fitness function is an automated check that protects an architectural property, so the architecture does not erode through lots of small reasonable-looking changes. The idea comes from the evolutionary architecture book by Ford, Parsons and Kua.
Then, if they want more
In a Spring Boot codebase, the first one I would write is an ArchUnit test, which is just a JUnit test that inspects the compiled classes: for example, no class annotated with RestController may depend on a Spring Data Repository, or a full layered rule where web may only call services and only services may call persistence.
If someone injects a repository into a controller, the build fails with a message naming the rule. The second is Spring Modulith's verification: ApplicationModules.of our application class, then verify, which treats each top-level package as a module and fails if one module uses another's internal types or if there are cycles.
A third useful one is a latency budget, a load test that fails if the p95 of key endpoints goes over, say, 200 milliseconds, usually run nightly because it is slower. The important part is running them in CI so violations block the merge and get discussed in review.
For an existing codebase, ArchUnit's freezing rules let us stop new violations without fixing all the old ones first.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What are fitness functions in evolutionary architecture, and how would you implement two...": https://scrivio-interview-library.netlify.app/architecture/architecture-decisions/34c8757890/