My dear reader, how are you? السلام عليكم
In this post, we will be discussing software architectural patterns. Following previous posts from web-journey will help you move forward from here:
- How a Web Application Works: From Beginning to Boundaries (DirectMe)
- Web Services: A dive into yesterday and shaping today (DirectMe)
- Software Architecture: An Abstract Introduction (DirectMe)
Layered or N-tier Pattern
We will start with the most common architectural pattern, i.e., layered or N-tier pattern. A most common 3-layers architectural pattern is shown in Figure 1; including a presentation layer (most commonly containing business rules) dealing directly with the user/client, business layers containing the business logic and the data-access backend layer interacting with the database. Each layer interacts with each other using API’s.
Fig. 1: A 3-layered architectural pattern
What is a layer is this pattern? You can think of a layer as an application; a small independent portion of the system. It can be a single program file, modules, libraries, and packages; and, it can be something more sophisticated but the key idea is that each layer should be independent. From independence, came in my mind a quote here, “Freedom cannot be bestowed — it must be achieved.” So, achieving independence of layers in an N-layered architectural pattern is trivial. Well, layered systems are nowadays becoming less popular and the rules of the layered patterns are not respected. Developed usually start writing code without keeping an architectural pattern in mind and then end up in separating the piece of codes into layers. This ultimately returns a big-ball-of-mud. And as the system grows, achieving the independence of layers becomes difficult and it greatly affects the scalability and agility of the software. Each layer responds to the layers above in a system called closed layered pattern. But since the layers are independent, the request can be made to layers bypassing one or more layers in a system called open layered system. This reduces latency. Variations of layered patterns include flexibility of the number of tiers (2-tier, 3-tier, 4-tier, or N-tier), closed/open layers and cacheable layers. Revise the concepts 1) DirectMe, and 2) DirectMe.
Planning to develop an application using a layered pattern? Steps are below:
- Group functionalities specific to one aspect of the system. In this, you are free to decide the name and number of layers and choose how big a layer would be.
- Develop the layers and decide which one will be open. It is advisable to keep the layers close unless there is no other choice. Also, APIs should be written up properly in this step for each layer’s communication.
- Setup a messaging method in between the layers. It can be an HTTP protocol but in any case, it should be well defined. And,
- decide the communication model and design the request flow model, i.e., a top-down or bottom-up approach.
Microservices Architectural Pattern
Another architectural pattern has risen and been widely adopted by the developers’ community as an alternative to layered or service-oriented architectures called the micro-services architectural pattern. Let us explore it in detail now. Figure 2 explains the basic architecture of microservices pattern.
Fig. 2: A Microservices architectural pattern
Let us first explain the service. A service could be anything from a single function to a group of classes that combine and cooperate for it to achieve a sort of functionality. The service should be micro or it should have a single or small functionality, e.g., say, producing a pdf. Each service should be an independently deployable unit and they should be separate from each other. The overall system must be loosely coupled. The most common approach to implement a microservice architectural pattern is as below.
Implementing a microservice pattern is just simply to split up the layers into chunks of functionalities or services. We keep on breaking the layers into sub-components until we reach a point where the application can be described as a collection of all independent and micro level services. Explore it in more detail from following resources, 1) webDev Cave tutorial DirectMe and 2) edureka tutorial DirectMe.
In the next post, I will be discussing one more architectural pattern, i.e., microkernel architectural pattern in detail and will share a few resources to learn more about it in detail.