liveBook · Manning

Loose coupling

As explained in Microservices Patterns, Second Edition

An architectural property in which software elements have limited dependencies on one another, allowing them to change more independently.

Loose coupling means that one software element has little influence on another. It limits the effects of changes and failures, making systems easier to modify and less prone to operational disruption. Some coupling is inevitable, however, and eliminating it can have significant drawbacks. section 4.1 section 4.6

Assess coupling in four distinct ways:

  • Design-time: a change to one element is unlikely to require changes to another. This localizes work and supports team autonomy. section 4.1.2 section 4.6
  • Build-time: changing one build module requires fewer other modules to be recompiled and retested, helping keep deployment pipelines fast. section 4.6
  • Runtime: one element’s failure or slowness is less likely to make another fail or slow down. Tight coupling can reduce availability and add latency. section 4.1.2 section 4.6
  • Infrastructure: elements sharing infrastructure are less likely to affect each other through resource competition or configuration changes. section 4.6

Localizing a customer change

Order Management invokes the Customer Management API, so the two are design-time coupled. With loose coupling, a Customer Management change is unlikely to require an Order Management change. If separate teams own the modules, localization also reduces cross-team coordination. High cohesion complements loose coupling by keeping related changes within the same package or module. section 4.2.2 section 5.2.2

Design for change by exposing stable concepts through small APIs and hiding volatile implementation details. This is a prediction: if an API changes unexpectedly, changes can ripple to its clients. Iterate as domain knowledge improves, and keep each piece of knowledge in one authoritative representation to avoid inconsistent copies. section 4.2.4

Figure 4.7 There are two ways that Order Management can notify other subdomains of a new order: it can invoke each subdomain’s API, or it can publish an event that the subdomains subscribe to. The first option design-time couples Order Management to the other subdomains. The second option inverts the dependencies so that the subdomains are design-time coupled to Order Management. Which option is best depends on the stability of each subdomain’s API.
managing dependencies

Compare direct API calls with event publication: the dependency direction changes, and the better choice depends on API stability. section 4.2.4

For builds, separate a stable API build project from its frequently changing implementation, so clients need not be retested for every implementation change. In a microservice architecture, independent deployment and loose coupling are both essential; without them, services risk becoming a distributed monolith. Watch dependencies as the system evolves. section 5.4.4 section 7.3 section 7.4.1

Breaking a service API still requires coordination: add the new version alongside the old one, migrate clients, and remove the old version after migration. Runtime coupling is a separate concern; colocating subdomains in one service can sometimes avoid it. section 7.3.2

How Microservices Patterns, Second Edition covers it

How deep it goes
Defines four coupling types and develops API design, build organization, and service-boundary trade-offs.
Examples
Uses Customer Management and Order Management to illustrate change localization, and compares direct API calls with event publication.
What you can do after
Evaluate and understand coupling across design, build, runtime, and infrastructure, and choose dependency arrangements that localize change.
How it connects
Connects loose coupling to cohesion, modifiability, independent deployment, microservices, and availability.

Also explained in

Compare how 7 books explain loose coupling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.