Quick revision
The short answer
I would accept a deliberate amount of duplication, because each layer must protect itself, and attack only the harmful kind. Shape rules are generated for clients from the API specification, business rules live only in the service that owns the data, and a short ownership map says which layer owns which rule. Because rules outlive the reasons for them, I would also review them whenever a domain changes and retire the ones nobody needs.
What to remember
In this answer, we accepted that each layer must protect itself and focused on the harmful duplication of business rules. Shape rules are written once in the API specification and generated into clients and DTOs, business rules live only in the service that owns the data, an ownership map says which layer owns which rule, and regular reviews with business owners retire rules that no longer serve a purpose.
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.
I would accept a deliberate amount of duplication, because each layer must protect itself, and attack only the harmful kind. Shape rules are generated for clients from the API specification, business rules live only in the service that owns the data, and a short ownership map says which layer owns which rule. Because rules outlive the reasons for them, I would also review them whenever a domain changes and retire the ones nobody needs.
Background to this question
Over years, validation spreads: a frontend team adds checks to forms, an API team annotates DTOs, a service adds if statements, and a DBA adds constraints. Then a rule changes, say the maximum length of a company name goes from 50 to 100, and it takes a month and three incidents to find every copy. At the organisation level, the question is not how to write a constraint but how to keep hundreds of them coherent while teams and systems change. In this answer, we will separate useful from harmful duplication, see how generating rules from API specs helps, decide where business rules live, define an ownership map, and set up a review habit.
1Not All Duplication Is a Problem
The first step is to stop treating every repeated rule as waste. Each layer has a reason to check things on its own. The form checks for the user's benefit, so errors appear instantly. The API checks because it cannot trust any client. The database checks because it is the only layer that sees every writer, including scripts and other services. If any of these were removed to avoid duplication, the system would become fragile.
The harmful duplication is a rule with business meaning implemented independently in several places, with no single source. Simple rules such as required or maximum length are cheap to repeat and rarely change. A rule like a customer may have at most three open disputes unless they are a premium customer changes with the business and gets copied with small differences. That is the kind we manage.
| Kind of rule | Example | Source of truth | Copies allowed |
|---|---|---|---|
| Shape | name required, max 100 | API specification | generated in client and DTO |
| Business rule | max 3 open disputes | owning service | none; others call the service |
| Invariant | email unique per account | database constraint | service check for a friendly error |
| User convenience | password strength meter | frontend | server enforces the real minimum |
2Generating Client Rules From the API Specification
For shape rules, the best way to stop drift is to write them once in a contract and generate the rest. If the API is described in OpenAPI, the specification already holds required fields, maxLength, pattern, minimum and enum values. Code generators can turn the same description into Java DTOs with Bean Validation annotations on the server and into TypeScript types and form validation schemas on the client.
Now the change from 50 to 100 is one line in the specification, reviewed once, and both sides pick it up on their next build. The cost is a generation step in each build and some discipline: nobody edits generated code by hand. For companies that already treat the API specification as the contract, this is cheap; for those that do not, it is a reason to start.
3Business Rules Belong to the Owning Service
Rules with business meaning should live in exactly one place: the service that owns the data the rule is about. The disputes rule lives in the disputes service. The checkout frontend does not reimplement it; if it wants to warn early, it calls an endpoint such as GET /customers/42/dispute-eligibility, which returns the answer and a reason code. Other services do the same. When the rule changes, one team changes one service, and everyone sees the new behaviour at once.
The cost is a network call and a dependency, which is why we do not do this for simple shape rules. But for rules that change with the business, the call is far cheaper than finding and fixing three diverging copies after an incident.
4Writing Down Who Owns Which Rule
Duplication grows when nobody knows where a rule is supposed to live, so people add it wherever they happen to be working. A short ownership map fixes that. For each domain, it lists the important rules, the layer and service that is the source of truth, and where copies are allowed. It lives next to the API specification, in the repository, and is part of design reviews: a pull request that adds a business rule to a frontend or a second service gets a review comment pointing to the map.
This does not need to be heavy. A table per domain with twenty lines is enough, and it becomes the place new engineers look first when they ask where is this checked.
5Reviewing Rules as Domains Change
Validation rules are rarely deleted. A rule added for a regulation that changed, a product line that was dropped or a bug that was fixed years ago stays in the code, keeps rejecting requests and confuses everyone. I would make rule review part of domain changes: when a team changes a domain, it reviews that domain's rules in the ownership map with the business owner. Logging which validation errors actually fire helps a lot here: a rule that has not rejected a request in a year is a candidate for removal, and one that rejects many valid-looking requests is a candidate for a conversation with the business.
- Business asks for a rule changemax name length 50 -> 100
- Is it a shape rule?YesChange the API spec, regenerateNoFind the owner in the map
- Owning service changes the ruleone place, one release
- Database constraint adjusted if neededmigration with the release
- Review the domain's other rulesremove ones that never fire
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Does generating validation from specs work for everything?
No. Specifications express shape well: types, required fields, lengths, patterns and ranges. They cannot express rules that depend on other fields in complex ways or on stored data. Those stay in code, in the owning service, and the spec can only document them in prose and in error codes.
How do you convince teams to remove their own copies of a rule?
By showing the cost with a real example, such as the incident where two copies disagreed, and by making the alternative easy: a well-documented eligibility endpoint or a generated schema. Teams remove copies when calling the owner is simpler than maintaining their own version.
What if the owning service is slow or down when the frontend wants to check a rule?
The frontend check is only an early warning, so it can degrade gracefully and let the user submit; the owning service still enforces the rule when the real request arrives. That keeps availability high without weakening the rule.
7Conclusion
In this answer, we accepted that each layer must protect itself and focused on the harmful duplication of business rules. Shape rules are written once in the API specification and generated into clients and DTOs, business rules live only in the service that owns the data, an ownership map says which layer owns which rule, and regular reviews with business owners retire rules that no longer serve a purpose.
Open with
I would start by accepting some duplication. Each layer has a reason to check things: the frontend for user experience, the API because it cannot trust clients, the database because it sees every writer.
Then, if they want more
So simple rules like required fields and maximum lengths can repeat, and the goal is not zero duplication. What I would go after is business rules copied in several places, because those drift.
For shape rules, I would make the API specification, usually OpenAPI, the single source and generate both the server DTOs with Bean Validation annotations and the client types and form schemas from it, so a change is one line reviewed once.
For business rules, I would say they live only in the service that owns the data, and anyone who wants to check early calls that service, for example an eligibility endpoint, instead of copying the logic.
Then I would keep a short ownership map per domain, in the repository, listing the important rules, their source of truth and where copies are allowed, and use it in design reviews.
Finally, rules outlive their reasons, so when a domain changes we review its rules with the business owner, and I like logging which rules actually fire so we can remove ones that never do.
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 documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
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 "Validation logic often ends up duplicated in frontends, APIs, services and databases....": https://scrivio-interview-library.netlify.app/spring-boot/request-validation/fa3ee29d9b/