Video in progress

When splitting a monolith into services, how do you measure coupling and cohesion, and how is the decision made?

I look at static dependencies (the class/package graph), change frequency together (git history) and runtime calls (tracing) at the same time. A cluster with high internal cohesion and low external coupling is a good candidate; splitting things that change together is a mistake.

📖 Answer

Splitting by intuition often leads to a "distributed monolith": the services are separate, but every change requires releasing three of them together. You have to rely on data.

Three kinds of signals:

  1. Static coupling. Produce the package graph with jdeps, ArchUnit or the IDE's dependency matrix. Cycles and "everyone needs it" packages (util, common) are visible immediately.
  2. Change coupling. From git history, count which files change together in one commit:
git log --since=1.year --name-only --pretty=format: \
  | grep '\.java$' | sort | uniq -c | sort -rn | head -50

There are tools like code-maat for pairwise analysis. If ordering and pricing change together in 70% of commits, don't separate them. 3. Runtime coupling. With tracing (OpenTelemetry), see which modules one request calls and how many times. If you move chatty modules onto the network, latency and errors go up.

Decision criteria:

  • the module owns its data and others rarely touch it;
  • a separate team can own it;
  • independent scaling or release rhythm is a real business need.

For the first split, pick the most peripheral, loosely coupled module (notifications, reports): the team learns on it and the risk is small. Splitting out the central domain first is the classic mistake.