Acoplamiento Débil

Acoplamiento y cohesión · Contratos

Escrito por Darjus Kazakevic · revisado el

Depend on the contract, never on the neighbour's insides.

Coupling is how much of B you must know to change A. Two services that share a database table are tightly coupled, however many network hops sit between them: rename a column and both break. Two services that talk through a versioned event are loosely coupled. The producer can replace its whole database and the consumer never knows.

Loose coupling is bought with contracts, and contracts are slower to change than shared code. There is also a limit. Split too finely and one feature crosses six contracts. Then cohesion (keeping things that change together in one place) is lost.

En esencia — Let each part know only the published contract of the others, such as an API or an event, so one can change, deploy or fail without the others.

Cuándo recurrir a él

Recurre a él cuando

  • Teams deploy separately, yet a change keeps forcing coordinated releases
  • Two services share a table, so one rename breaks both
  • A failure in one service keeps taking down its callers

Reconsidéralo cuando

  • Code that changes together for every feature — splitting kills cohesion
  • One team owns both sides, and they always ship together
  • Early in a product, while the boundaries are still unclear

Conceptos clave

Coupling and cohesion
Coupling is how much one part depends on another's details. Cohesion is how closely the things inside one part belong together. Aim for low coupling between parts and high cohesion within each.
Contract
The published surface that others may depend on: an API, an event schema, a file format. It is versioned and documented, and within a version it only grows.
Temporal coupling (Sync vs async)
A can work only while B is up. A synchronous call couples them in time. A queue or an event lets A continue while B is slow, down or being deployed.
Kinds of coupling
From tightest to loosest: a shared database, a shared library with domain logic, a synchronous API, an asynchronous event. Each step lets the parts change more independently.

Reglas de diseño

Never read a neighbour's tables
A shared table is a contract nobody agreed to. Consume a versioned API or event instead, take only the fields you need, and let breaking changes ship as v2 beside v1.
Measure coupling by what a change forces
Before a change, list what else must change and deploy with it. If the list is long or crosses teams, the coupling is too tight, wherever the code runs.
Make contracts additive
Add optional fields; never rename or remove one inside a version. Consumers must ignore fields they do not know, so producers can add fields without a coordinated deploy.
Go async where the caller need not wait
If A does not need B's answer to reply to its user, publish an event instead of calling B. A then keeps working while B is slow, down or being deployed.

Errores habituales

  • A shared 'common' library full of domain types. Every service must upgrade together, so the coupling returns through the build.
  • Events that carry the producer's table rows. Consumers now depend on its schema, which is the shared table again.
  • Splitting into services before the boundaries are clear. One feature then needs changes and deploys in five services.
  • Long synchronous chains: A calls B calls C. Availability multiplies down the chain, and one slow service slows all of them.