As the 12 factor app states, that each component of an application should work independent of each other. This is required in order to make the update process simpler. This is where many developers use architectural pattern, commonly known as hexagonal architecture.
The main aim of this architecture is to divide the application into loosely couple components. These components can then be connected using ports and adapters.
This is mainly used in a micro-service architecture where there are multiple components and you create each component independent of the other one.
Let us understand it with the help of the diagram of hexagonal architecture:
As we can see, the core application is surrounded by various adapters. Each adapter is solely responsible for the building of their domain. For instance, in our diagram we have FIX Adapter. The work of FIX Adapter is to handle all the FIX APIs functionality and rules. Then suppose in future, there might exists a need for REST APIs as well. So for that, we will create a separate adapter i.e. REST Adapter. These two adapters are independent of each other. Now, while building our application, we found a need to use web-socket, we can simply create an adapter for that without touching the rest of them. Also, most of the applications need to store and retrieve the data. This is where our database comes into play. So even for that, we can create a separate adapter.
- In case there is some functionality break in one adapter, the other one remains intact.
- If there comes a requirement change let’s say for REST APIs, then we need not to touch our FIX Adapter.
- In future, suppose there is no need for FIX APIs, we can remove it gracefully without breaking the functionality of REST APIs since they both are separate adapters.
The main role of ports in hexagonal architecture is to connect the adapters to the core application. There is no constraint on how many ports should be there in an application. Also, there could be a possibility that two adapters are using the same port. Let us take an example to understand this. Suppose, there is an application which takes input from both GUI and terminal/command line. In this case, same port can be used for both the adapters of GUI and terminal.