Scrivio Interview library
DesignAdvanced6 min read · updated 12 November 2026

Design architecture fitness functions that prevent architecture anti-patterns across 50 repositories owned by different teams.

We package a shared set of ArchUnit rules for layering, cycles, forbidden dependencies and naming, and run them in CI for every repository, with per-repository exemption lists that must be justified. We add API checks such as OpenAPI linting and backward-compatibility checks, use tracing data to build the real dependency graph between services and spot unexpected coupling, report results per team on a dashboard, fix rules that raise false alarms, and introduce each rule as a warning before making it blocking.

Why this question is asked

An architecture fitness function is an automated check that tells us whether a system still has a property we care about, such as no cycles between packages or no breaking API changes. The term comes from evolutionary architecture, borrowing the idea of a fitness function from evolutionary algorithms. Across 50 repositories owned by different teams, written guidelines and review meetings cannot keep up; automated checks can. But checks that are noisy or imposed overnight get disabled. Interviewers ask this design question to see whether we can build checks that work technically and are accepted by teams. In this answer, we will design the shared code rules, roll them out across repositories, add API checks, use runtime data for coupling between services, and make the results visible.

1What Fitness Functions Check

We start from the anti-patterns we want to prevent and pick a check for each. Inside a codebase: layering violations such as controllers calling repositories, cycles between packages, forbidden dependencies such as domain code importing web types, entity classes returned from controllers, and naming conventions that keep code navigable. At service boundaries: breaking changes in APIs and events. Between services: unexpected runtime dependencies and long synchronous chains. Each check has a level: some run on every build, some nightly, and some are reported rather than enforced.

Step throughFitness functions at three levels
  1. Code rules in every buildArchUnit: layers, cycles, forbidden imports, naming
  2. Contract checks on every PROpenAPI lint, breaking-change diff, event schema compatibility
  3. Runtime checks nightlyservice dependency graph from traces, call-chain depth
  4. Team dashboardresults per repository and team, trends, exemptions

4 layers, from the top down. Step through them one at a time.

Fast checks block builds; slower, fuzzier checks feed the dashboard.

2A Shared ArchUnit Rule Library

ArchUnit lets us write architecture rules as JUnit tests. Instead of each team writing its own, we publish a library, for example acme-arch-rules, with a curated rule set and a version number. Each repository adds it as a test dependency and runs it with a one-line test class.

Java
// In the shared library
public final class AcmeRules {
@ArchTest public static final ArchRule noCycles =
slices().matching("com.acme.(*)..").should().beFreeOfCycles();
@ArchTest public static final ArchRule controllersDoNotUseRepositories =
noClasses().that().areAnnotatedWith(RestController.class)
.should().dependOnClassesThat().areAssignableTo(Repository.class);
@ArchTest public static final ArchRule domainIsFrameworkFree =
noClasses().that().resideInAPackage("..domain..")
.should().dependOnClassesThat().resideInAnyPackage("org.springframework..", "jakarta.persistence..");
}
// In each repository
@AnalyzeClasses(packages = "com.acme")
class ArchitectureTest {
@ArchTest static final ArchTests acme = ArchTests.in(AcmeRules.class);
}

Because the rules live in one versioned library, improving a rule or fixing a false alarm reaches all 50 repositories through a version bump. Rules should come with a because() clause that explains the reason, so a developer who sees the failure learns why the rule exists.

3Running in CI With Justified Exemptions

Every repository runs the rules in its normal CI build. Old codebases will have existing violations, and demanding they all be fixed first would stop adoption. ArchUnit's FreezingArchRule helps here: it records the current violations in a store file committed to the repository and fails only on new ones. Fixed violations are removed from the store, so it can only shrink.

Beyond frozen legacy violations, a team may need a real exception, such as a batch module that legitimately breaks a layering rule. Those go into an exemption file in the repository, each entry with a reason and an owner, and the pull request that adds one is reviewed by someone outside the team. Unjustified or expired exemptions show up on the dashboard.

4Checking APIs and Events

Code rules cannot see what a change does to other teams. For HTTP APIs we lint OpenAPI specifications with a shared rule set, for consistent naming, error format, pagination and versioning, using a linter such as Spectral. Then we compare each pull request's specification with the version in production using a breaking-change detector such as oasdiff or openapi-diff, and fail the build if a field is removed, a type changes or a required parameter is added.

For events, a schema registry with a compatibility mode, such as backward compatible, rejects incompatible schema versions at registration, and the build can check compatibility before merge. Where teams use consumer-driven contract tests, those also run in the provider's pipeline.

5Finding Coupling Between Services From Traces

Some anti-patterns only show up at runtime, such as a service that started calling five others, or a request chain that grew to seven hops. Distributed tracing already records which service calls which. A nightly job aggregates trace data into a service dependency graph, compares it with the previous week and with the declared dependencies in the service catalogue, and flags new edges, cycles between services and request paths deeper than an agreed limit, such as four synchronous hops.

These findings are not build-blocking, because they are fuzzier: a new edge may be perfectly intended. They go to the dashboard and to the owning team as a question: did you mean to add this dependency?

Step throughHandling one new rule
  1. Propose the rule with its reasonan incident or recurring review comment
  2. Ship it as a warningreported in CI and on the dashboard
  3. False alarms reported?
    YesFix or narrow the rule, release a new versionNoKeep collecting data
  4. Most repositories clean for a month?
    YesMake it blocking in the next versionNoHelp teams fix violations first

4 steps in order. Play them, or step through them one at a time.

Warn first, listen, then enforce.

6Dashboards, Feedback and Gradual Enforcement

Results are collected from every pipeline into a dashboard per team and per repository: which rules pass, how many frozen violations remain, how many exemptions exist, and trends over time. Teams see their own progress, and platform engineers see which rules fail most often across the organisation, which usually means either a widespread problem or a bad rule.

Every new rule starts as a warning that is reported but does not fail the build. We announce it, watch the data and fix false alarms quickly, because nothing destroys trust in fitness functions faster than a rule that blocks a release for no reason. Once most repositories are clean and teams have had time to adapt, typically a month or two, the next library version makes it blocking. Teams can also propose rules, so the set reflects real problems rather than one group's opinions.

7Follow-up questions

What if a team simply disables the architecture test?

The pipeline template runs the shared rules as a standard step, so disabling them is visible in the configuration and on the dashboard, which shows the repository as not reporting. We treat it as a conversation, not a violation: usually the team hit a false alarm or a deadline, and fixing the rule or granting a recorded exemption is better than a policy fight.

How many rules should the shared set have?

Few, perhaps ten to fifteen, each tied to a real problem the organisation has experienced. A large rule set produces many violations nobody understands. Teams can add their own local rules on top for their specific structure.

How do fitness functions differ from code quality tools?

Code quality tools look at local properties such as complexity or style. Fitness functions check architectural properties, such as dependency direction, boundaries, contracts and runtime coupling, that span many classes or services. Both are useful and they often run in the same pipeline.

8Conclusion

In this answer, we designed fitness functions for 50 repositories. A shared, versioned ArchUnit rule library checks layering, cycles, forbidden dependencies and naming in every CI build, with frozen legacy violations and justified exemptions per repository. OpenAPI linting, breaking-change checks and schema compatibility protect contracts, and a nightly job over tracing data finds unexpected coupling between services. Results go to per-team dashboards, false alarms are fixed quickly, and each rule moves from warning to blocking only after teams have adapted.

What to say in the interviewAbout 80 seconds aloud

Open with

I would build it in layers. In every build, a shared ArchUnit rule library, versioned and pulled in as a test dependency, checks layering like controllers not using repositories, package cycles, forbidden dependencies such as domain code importing Spring or JPA, and a few naming conventions.

Then, if they want more

Each rule has a because clause so people learn the reason. Since old repositories have existing violations, I would use ArchUnit's freezing rules so only new violations fail, and allow per-repository exemptions in a file, each with a reason and owner and reviewed by someone outside the team.

On every pull request that changes an API, I would lint the OpenAPI spec against shared rules and run a breaking-change diff against production, and for events use a schema registry with compatibility checks.

Some coupling only shows at runtime, so a nightly job would build the service dependency graph from tracing data and flag new edges, cycles and long synchronous chains, as questions to the owning team rather than build failures. All results go to a dashboard per team.

And the rollout matters as much as the rules: every new rule starts as a warning, we fix false alarms fast, and only after most repositories are clean for a month or so does it become blocking.

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 "Design architecture fitness functions that prevent architecture anti-patterns across 50...": https://scrivio-interview-library.netlify.app/java/application-architecture/819d5285ba/