Quick revision
The short answer
A shared library of entity classes and DTOs ties every service's release cycle to every change in that library and erases the boundaries between bounded contexts. I would let each service own its API models and share contracts such as OpenAPI documents or event schemas instead of classes, keep shared libraries only for technical concerns like logging, security and observability with stable APIs, version them with semantic versioning so unrelated changes never force an upgrade, and migrate by copying the classes each service needs into it and then removing the dependency.
What to remember
In this answer, we saw that a shared library of entities and DTOs couples the release cycles of every service and breaks bounded contexts, because every team's change becomes everyone's change. We replaced shared classes with shared contracts such as OpenAPI documents and event schemas, kept shared libraries only for stable technical concerns, versioned them with semantic versioning so unrelated changes never force upgrades, and migrated by copying the needed classes into each service and removing the dependency.
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 shared library of entity classes and DTOs ties every service's release cycle to every change in that library and erases the boundaries between bounded contexts. I would let each service own its API models and share contracts such as OpenAPI documents or event schemas instead of classes, keep shared libraries only for technical concerns like logging, security and observability with stable APIs, version them with semantic versioning so unrelated changes never force an upgrade, and migrate by copying the classes each service needs into it and then removing the dependency.
Why this question is asked
A common-domain library usually starts with good intentions: why write the Order class twenty times when we can write it once? A few years later, adding one field to Order means twenty pull requests, twenty test runs and a release train. Interviewers ask this scenario because it is one of the most common ways teams build a distributed monolith without noticing. In this answer, we will see why shared domain classes couple services, what should be shared instead, what kind of shared library is healthy, how to version it, and how to migrate away from the current one step by step.
1Why Shared Domain Classes Couple Everything
When twenty services depend on common-domain, they all compile against the same Order, Customer and Invoice classes. Suppose billing needs a new field, taxRegion, on Invoice. The library team adds it and releases version 4.0. Now services that deserialize Invoice with strict settings fail when they see the new field, services that construct Invoice need code changes because the constructor changed, and the dependency graph pulls in the new version for everyone who upgrades anything else.
The deeper problem is conceptual. The library assumes Invoice means the same thing to every service, but each bounded context (the part of the system where a model has one precise meaning) needs a different shape. Shipping does not care about tax regions; billing does not care about parcel sizes. A single shared class has to carry the union of every team's fields, and every team's change becomes everyone's change.
2Share Contracts, Not Classes
Services still need to agree on what crosses their boundaries, but that agreement is a contract, not a class. For HTTP APIs, the owning service publishes an OpenAPI document; for events, it publishes a schema in a schema registry, for example Avro or JSON Schema. Each consumer generates or writes its own classes from the contract, containing only the fields it uses.
The difference is who changes when. If billing adds taxRegion to its invoice API in a backward-compatible way (a new optional field), consumers that do not need it change nothing, because their own classes ignore unknown fields. Contract tests, for example with Spring Cloud Contract or Pact, check that the provider still honours what each consumer relies on, so compatibility is verified by tests rather than by forcing everyone onto the same jar.
Note that Jackson's default in Spring Boot already ignores unknown properties, because Boot turns off FAIL_ON_UNKNOWN_PROPERTIES; the annotation makes the intent explicit for code that may run with a plain ObjectMapper.
3What a Healthy Shared Library Looks Like
Shared libraries are not bad in themselves. They are healthy when they hold technical concerns that are the same everywhere: structured logging setup, security filters for validating tokens, observability defaults (metrics, tracing propagation), a standard error response format, HTTP client configuration. These change rarely, have stable APIs and contain no business meaning, so upgrading them is a routine maintenance step rather than a coordinated release.
The test I use is simple: if a change to the library could be triggered by a business requirement, the code does not belong in a shared library.
| Thing | Share as | Why |
|---|---|---|
| Entities, domain rules | Do not share | Each context models its own |
| API request and response shapes | OpenAPI contract | Consumers keep their own classes |
| Event formats | Schema in a registry | Compatibility checked on publish |
| Logging, tracing, security, errors | Small technical library or starter | Same everywhere, rarely changes |
4Versioning Without Forced Upgrades
Whatever we keep shared should follow semantic versioning: a patch version for fixes, a minor version for backward-compatible additions and a major version only for breaking changes. A service on 2.3 should be able to move to 2.7 without touching code, and a breaking 3.0 should be rare, announced and supported alongside 2.x for a defined period.
We should also split libraries so unrelated changes do not travel together. If the logging setup and the security filter live in separate modules, a security fix does not drag a logging change into every service. Automated dependency update tools, such as Renovate or Dependabot, can then open the upgrade pull requests, and each team merges them on its own schedule.
5Migrating Away From common-domain
We cannot delete the library on Monday, so we migrate service by service. For each service, we copy into it the classes it actually uses from common-domain, usually into its own package such as com.shop.shipping.client.billing, trim them to the fields it needs, and switch its imports. Once nothing imports common-domain, we remove the dependency from that service's build.
Two practical points make this smoother. First, we freeze common-domain for new business fields during the migration; new fields go into the owning service's contract instead. Second, we start with services that use only a few classes, to build confidence, and leave the heaviest users for last. When the last service drops the dependency, the library is archived.
- Find used classesimports from common-domain
- Copy and trim themonly the fields this service reads or writes
- Generate from contracts where possibleOpenAPI or event schema
- Run contract and integration testsprovider still satisfies this consumer
- Remove the dependencybuild fails if anything still imports it
5 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Isn't copying classes into each service duplicated code?
It is duplication of shape, not of logic, and that is a deliberate trade. Each copy is small, holds only the fields that service uses and can change on its own schedule. The cost of a few duplicated records is far lower than the cost of twenty services releasing together for every field.
What if a rule really must be the same everywhere, such as how money is rounded?
Then it should either live in the one service that owns that rule and be exposed through its API, or, if it is a true technical utility such as a Money type with rounding, be put in a small, stable library with no other business classes. The key is that the library has a single purpose and rarely changes.
How do you stop the problem coming back?
Make it visible in reviews and builds. A rule in the platform's build checks can flag any dependency on a shared library that contains entity or DTO classes, and architecture guidelines should say plainly that API models belong to their service and contracts are shared through OpenAPI or a schema registry.
7Conclusion
In this answer, we saw that a shared library of entities and DTOs couples the release cycles of every service and breaks bounded contexts, because every team's change becomes everyone's change. We replaced shared classes with shared contracts such as OpenAPI documents and event schemas, kept shared libraries only for stable technical concerns, versioned them with semantic versioning so unrelated changes never force upgrades, and migrated by copying the needed classes into each service and removing the dependency.
Open with
The root problem is that the library shares domain models, so every business change in one team becomes a release for twenty services, and the services stop being independent.
Then, if they want more
It also breaks bounded contexts, because one Invoice class has to carry every team's fields. I would change the rule so that each service owns its own API models and what we share are contracts: OpenAPI documents for HTTP APIs and schemas in a registry for events.
Consumers generate or write their own small classes with only the fields they use and ignore unknown fields, and contract tests check compatibility, so adding an optional field in billing does not touch shipping.
I would keep shared libraries only for technical concerns that are the same everywhere, like logging, security filters, tracing and the error format, with small modules and stable APIs, versioned with semantic versioning so a minor upgrade never needs code changes and updates can be merged by each team on its own schedule.
For the migration, I would freeze common-domain for new business fields, then service by service copy the classes it uses into its own package, trim them, switch imports, run contract tests and remove the dependency, starting with the lightest users. When nobody imports it anymore, we archive the library.
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 Cloud documentation, Resilience4j documentation
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 "Your services depend on a shared "common-domain" library with entity classes and DTOs....": https://scrivio-interview-library.netlify.app/spring-boot/microservice-design/24aeef00b8/