Another weekend, another weekend read, this time all about the Myth of Loose Coupling
Loose coupling has become a rallying cry in modern software architecture, especially among advocates of event-driven architectures. But what makes a system loosely coupled-and do events truly deliver on this promise?
Coupling
Coupling refers to the dependencies between components in a system. We can model these dependencies as a graph:
Nodes represent components.
Links represent dependencies between components.
This simple model allows us to focus on the concepts and relationships that matter to us while ignoring the ones that don’t matter to us.
An E-Commerce Application
Let’s explore two different ways to implement an e-commerce application consisting of a shopping cart, an inventory service, a payment service, and a shipping service: a procedure-based implementation and an event-based implementation.
Procedure-based Implementation
In a procedure-based implementation (also called orchestration), each service has a set of APIs and each API has a signature, a schema. On checkout, the shopping cart invokes the inventory, payment, and shipping service APIs.
Each service, each API, and each schema constitute a component. Services depend on the APIs they invoke including the API schemas.
Event-based Implementation
In an event-based implementation (also called choreography), the shopping cart raises an event such as checkout. Other services subscribe to this event and emit their own events in response.
Each service, each event, and each event schema constitute a component. Services depend on the events they consume including their schema.
Loose Coupling
Which implementation is more strongly or more loosely coupled? The answer isn’t straightforward:
In the procedure-based implementation, the shopping cart depends on the inventory, payment, and shipping services.
In the event-based implementation, the inventory, payment, and shipping services depend on the shopping cart’s event and event schema.
At a glance, there does not seem to be an objective metric telling us which of these implementations are strongly coupled and which are loosely coupled.
Conclusion
Neither procedure-based nor event-based implementations eliminate coupling, arguably they do not even loosen coupling, they merely transform coupling.
Which approach is “looser“? There’s no universal answer.
Happy Reading