Video in progress

What problems come up when microservices share domain classes through a common "common-model" jar, and how do you get out of it?

A shared jar couples services at the deploy level: if one field changes, every service has to be updated together. The way out is for each service to keep its own model, and for the contract to be shared through a schema (OpenAPI, Avro/Protobuf).

📖 Answer

At first the idea sounds logical: "the Customer class is the same everywhere, why write it ten times?". A year later the picture looks like this:

  • common-model is at 4.x, and three services are stuck on 3.x because upgrading breaks their tests;
  • Customer has 60 fields, and each team added the 5 it needs;
  • the jar contains Jackson, JPA annotations, even a Spring dependency, so the consumer's dependency tree fills up with versions you didn't choose;
  • one team renames a field and another service fails on deserialization.

This is the shortest path to a "distributed monolith": the services are deployed separately but can't change independently.

The way out, step by step:

  1. Define the contract. What data actually moves between services? You write it down as an OpenAPI or Avro/Protobuf schema. The schema is versioned in a registry, and a compatibility check (for example BACKWARD) runs in CI.
  2. Each service gets its own model. The consumer generates code from the schema or writes its own DTO and reads only the fields it needs (FAIL_ON_UNKNOWN_PROPERTIES=false, a tolerant reader).
  3. Drain the shared jar. New code doesn't use it, and old usages are moved gradually. ArchUnit or a dependency check forbids new imports.

What can stay shared: things that are truly stable and have no business meaning, such as the Money value object, trace header names, and the error response format. They are small, rarely change, and are tied to no framework.

You have to accept the cost of duplication. Having a CustomerDto in two services is a cheap price for their independence. DRY is good inside a codebase, but across service boundaries it is worse than coupling.