Video in progress

Strong Junior 🧩 Microservices

All services use a shared `common-dto` library. It looks convenient. What are the traps?

A shared DTO library ties services together at the code level: adding or changing a field in one class forces every service to move to the new version, often to deploy together. As a result, independence is lost.

📖 Answer

At first it looks like less duplication: OrderDto lives in one place and everyone imports it. A year later it's a different story:

  • common-dto is at version 1.14, payment is stuck on 1.9, warehouse on 1.12;
  • someone renamed a field in OrderDto: the services that get recompiled break, and the ones on an old version read the JSON differently;
  • "useful" utils, Spring configurations and even business logic gradually creep into the library.

There's another hidden problem: DTOs turn into the service's internal model. warehouse uses only 3 of OrderDto's 40 fields, but it's coupled to all of them.

What people do instead:

  • each service writes the DTOs it needs itself: a little duplication, but more independence;
  • the contract is a schema, not code: an OpenAPI specification or an Avro/Protobuf schema, with client code generated from it;
  • if there is a shared library, it's only for technical things (logging, tracing setup) and with strict backward compatibility.

The DRY rule works well inside a single service; between services, a little duplication is cheaper than coupling.