Video in progress

Why does sharing a common "common-model" jar (with DTOs and entities) between several services often become a problem?

A shared model library couples services at compile time: if one field changes, all services are forced to release together. That's a sign of a "distributed monolith". Sharing is only appropriate for things that are stable, small and versioned.

📖 Answer

It always starts with good intentions: "let's not write the DTOs twice, DRY". Two years later the picture is:

  • common-model has 300 classes, each service uses 10 of them, but pulls in all of them;
  • the billing team adds a field to Customer, and until the delivery service moves to the new version you get deserialization errors or a version conflict;
  • it comes with Jackson and Hibernate annotations, and transitive dependency conflicts start;
  • nobody can delete an old field, because nobody knows who uses it.

The root of the problem: DRY is about not duplicating knowledge, not code. The CustomerDto in two services may look the same, but they change for different reasons, so it's different knowledge.

What you can share:

  • A contract, not code: OpenAPI, an Avro/Protobuf schema, versioned in a schema registry. Each service generates its own classes.
  • Small, stable libraries: Money, the error format, tracing helpers. With semver and rare breaking changes.
  • A shared kernel, only if two teams work closely and agree on changes together.

How to get out of an existing common-model: each service copies the classes it needs into itself (yes, copies), then the dependency on the library is removed. On deserialization use FAIL_ON_UNKNOWN_PROPERTIES=false (the Spring Boot default), so new fields don't break old consumers.