YURKOL Ltd - Custom Software and Cloud Architectural Solutions for Modern Businesses

Intro to Microservices architecture - coupling and cohesion.

In this article I'm going to present some theoretical basics of the Microservices architectural style.

Coupling

In order to understand what coupling is one needs to understand what dependency is. Dependency is an essential functionality, library, component, service or code that is essential for a different part of a software system to work. For example, a video player won't be able to play a video file, if there are no corresponding video codec properly installed on your system. Here the codec is the dependency of the multimedia software.

Yet the codec is not the only dependency for the player to be functioning properly; it also requires correctly working video hardware's driver, a network connection for downloading multimedia's metadata etc.

No modern web application can be thought without a database - such an application depends on a database.

Coupling - is a degree of dependence of a system's component on other system's components. In the above examples, the multimedia player is coupled with a video codec; the web service is coupled with a database. High coupling is when a component has many dependencies; low coupling is when a component has few dependencies. Overall, high coupling is not desirable - if we want to change some component, we ought to change its dependants as well.


Types of coupling

In software architecture & systems design there are following types of coupling:

Content coupling: for example, when one class has an access to another class' private/inner members.

Common coupling: for example, when two components has access to same shared area of memory (like global variables).

Control coupling: for example, when one function controls flow of another function. A goroutine that controls exectution of another goroutine through a channel is a great illustration.

Data coupling: for example, when two services share the same database.

Type use coupling: when a field/member of type A is an object of type B.

Stamp coupling: when a method of type A is a method of type B.

Import coupling: when a programming module depends on other modules which it imports.

External coupling: when a programm depends on another program.

Cohesion

Another concept that is related to coupling is cohesion. Cohesion defines how good a system's components work together on same task. For example, imagine a class, one method of which is responsible for logging incoming requests, and another method is responsible for handling connection to a database: this methods work on unrelated tasks, thus this class has low inner cohesion.

When designing a system, an architect should aim for high cohesion: elements have to be good in achieving same goal together.