What is Coupling and What are the types of Coupling in Microservices
In this tutorial, we are going to learn about the Coupling and its types in the Microservices which is an important question in the interview room.
What is Coupling
Coupling is a degree of interdependence of software modules, components or services. To understand the Coupling better, we need to know What is Dependency.
Dependency
Dependency is an essential functionality, library, component, service or code that is essential for a different part of the software system to work.
Example of Loose Coupling
- Imagine a software system, Where we have two components or two Micro-services named A and B.
- A is dependent on B. If you remove B, A cannot work anymore. So there are different ways in which A can use B. lets say B had restful API’s, then A can make a call to those restful API’s.
- If B is replaced with another Micro-service or component which has exactly same API’s with same contract and same everything, A would still continue using that API and it would still work as earlier. In this case we can see that A is dependent on B, but it is not strongly dependent due to loose coupling between the services.
Example of Strong coupling
- let’s say, B is an DLL(Dynamic Linked Library-> Windows is made of thousands of DLL’s which are just compiled and packaged to binaries) instead of REST API A would have to have a code to import to B along with its function to use them. Here if B is completely replaced then A wouldn’t work which resembles strong coupling between the services.
Note: Loose Coupling is always the best practice and preferred in the Microservice architecture.
Types of Coupling
-
Content Coupling: One class can access the private members of another class.
- For example, Many programming languages like Java and C-sharp don’t allow this, But there some languages like C++ that allows one class can access the private members of another class because of the performance which is a most important thing in the programming language.
-
Common Coupling: Two classes access the same shared data i.e., global variables and static properties.
- For example, In visual basic, there are global variables that can be shared across the multiple classes that basically increases the coupling and also static properties in C sharp and Java also may increase the coupling.
-
Control Coupling: When a function controls the flow of another function.
- For example, there are two functions. One of them has a loop and status variable, another function changes the same status variable in which one function is controlling the other function that is called controlled coupling.
-
Routine Coupling: When one function calls another function without passing any parameters.
- For example, One class is working on the one of the shared private field. This is very similar to Common Coupling.
- Data Coupling: When two systems share the same database.
- Type Use Coupling: When a member or property of class B is of type class A.
-
Stamp Coupling: When in class B a method has a parameter of type class A.
- For example, If you want to change something in class A you need to change the method signature in class B.
- Import Coupling: When a library is imported into another program i.e., DLL file in windows.
- External Coupling: When communicating with an external program.