The most common architecture pattern is the layered architecture pattern, otherwise known as the n-tier architecture pattern. This pattern is the de facto standard for most Java EE applications and therefore is widely known by most architects, designers and developers. The layered architecture pattern closely matches the traditional IT communication and organizational structure found in most companies, making it a natural choice for most business application development efforts.
Components within the layered architecture pattern are organized into horizontal layers, each layer performing a specific role within the application (e.g., presentation logic or business logic). Although the layered architecture patter does not specify the number of types of layers that must exist in the patter, most the layered architectures consist of four standard layers: presentation, business, persistentce and database.
In some cases the business layer and the persistence layer are combined into a single business layer, particularly when the persistence logic (e.g. SQL or HSQL) is embedded within the business layer components. Thus smaller applications may have only three layers, whereas larger and more complex business applications may contain five or more layers.
Each layer of the layered architecture pattern has a specific role and responsibility within the application. For example, a presentation layer would be responsible for handling all user interface and browser communication logic, whereas a business layer would be responsible for executing specific rules associated with the request. Each layer in the architecture forms an abstraction around the work that needs to be done to satisfy a particular business request. For example, the presentation layer doesn’t need to know or worry about how to get customer data. It only needs to display that information on a screen in particular format. Similarly, the business layer doesn’t need to be concerned about how to format the customar data for display on a screen or even where the customer data is comming from. it only needs to get the data from the persistence layer, perform business logic against the data (e.g. calculate values or aggregate data or validate), and pass that information up to the presentation layer.
One of the powerful features of the layered architecture pattern is the separation of concerns (SOC) among components. Components within a specific layer deal only with logic that pertains to that layer. For example, components in the presentation layer deal only presentation logic, whereas components residing in the business layer deal only with business logic. This type of component classification makes it easy to build effective roles and responsibility models into your architecture and also makes it easy to develop, test, govern, and maintain applications using this architecture pattern due to well-defined component interfaces and limited component scope.
Notice in the second figure that each of the layers in the architecture is marked as being closed. This is very important concept in the layered architecture pattern. A closed layer means that as a request moves from layer to layer, it must go through the layer right bellow it to get the next layer below that one. For example, a request originating from the presentation layer must first go through the business layer and then to the persistence layer before finally hitting the database layer.
So why not allow the presentation layer direct access to either the persistence layer or database layer? After all, direct database access from the presentation layer is much faster than going through a bunch of unnecessary layers just to retrieve or save database information. The answer to this question lies in a key concept known as layers of isolation.
The layers of isolation concept means that changes made in one layer of the architecture generally don’t impact or affect components in other layers: the change is isolated to the components within that layer and possibly another associated layer ( such as a persistence layer containing SQL ). If you allow the presentation layer direct access to the persisntece layer, then changes made to SQL within the persistence layer would impact both the business and the presentation layers, thereby producing a very tightly coupled application with lots of interdependencies between components. This type of architecture then becomes very hard and expensive to change. Also known as the Messy Pattern 🙂
The layers of isolation concept also means that each layer is independent of the layers, thereby having little or no knowledge of the inner workings of other layers in the architecture. To understand the power and importance of this concept, consider a large refactoring effort to convert the presentation framework from JSP to JSF. Assuming that the contracts (e.g. models) used between the presentation layer and the business layer remain the same, the business layer is not affected by the refactoring and remains completely independent of the type of user interface framework used by the presentation layer.
While closed layers facilitate layers of isolation and therefore help isolate change within the architecture, there are times when it makes sense for certain layers to be open. For example, suppose you want to add a shared-services layer to an architecture containing common service components accessed by components within the business layer (e.g. data and string utility classes or auditing and logging classes). Creating a services layer is usually a good idea in this case because architecturally it restricts access to the shared services to the business later. Without a separate layer, there is nothing architecturally that restricts the presentation layer from accessing these common services, making it difficult to govern this access restriction.
In this example, the new services layer would likely reside below the business layer to indicate that components in this services layer are not accessible from the presentation layer. However, this presents a problem in that the business layer is now required to go through the services layer to get to the presentation layer, which makes no sense at all. This is an age-old problem with the layered architecture, and is solved by creating open layers within the architecture.
As illustrated on the next image the services layer in this case is marked as open, meaning requests are allowed to bypass this oepn layer and go directly to the layer below it. In the following example, since the services layer is open, the business layer is now allowed to bypass it and go directly to the persistence layer, which makes perfect sense.
Leveraging the concept of open and closed layers helps define the relationship between architecture layers and request flows and also provides designers and developers with the necessary information to understand the various layer access restrictions within the architecture. Failure to document or properly communicate which layers in the architecture are open and closed (and why) usually results in tightly coupled and brittle architectures that are very difficult to test, maintain and deploy.
The layered architecture pattern is solid general-purpose pattern making it a good starting point for most applications, particularly when you are not sure what architecture pattern is best suited for your application. However, there are a couple of things to consider from an architecture standpoint when choosing this patter.
The first thing to watch out for is what is known as the architecture sinkhole anti-pattern. This anti-pattern describes the situation where requests flow through multiple layers of the architecture as simple pass-through processing with little or no logic performed within each layer. For example, assume the presentation layer responds to a request from the user to retrieve customer data. The presentation layer passes the request to the business layer which simply passes the request to the persistence layer, which then makes a simple SQL call to the database layer to retrieve the customer data. The data is then passed all the way back up the stack with no additional processing or logic to aggregate, calculate or transform the data.
Every layered architecture will have at least some scenarios that fall into the architecture sinkhole anti-patter. The key, howeever is to analyze the percentage of requests that fall into this category. The 80-20 rule is usually a good practice to follow to determine whether or not you are experiencing the architecture sinkhole anti-pattern. It is typical to have around 20 percent of the requests as simple passthrough processing and 80% of the requests having some business logic associated with the request. However, if you find that this ratio is reversed and a majority of your requests are simple passthrough processing, you might want to consider making some of the architecture layers open, keeping in mind that it will be more difficult to contral change due to lack of layer isolation.
Another consideration with the layered architecture pattern is that it tends to lend itself toward monolithic applications, even if you split the presentation layer and business layers into separate deployable units. While this may not be a concern of some applications, it does pose some potential issues in terms of deployment, general robustness and reliability, performance, and scalability.
To illustrate the pattern with an example, consider a request from a business user to retrieve customer information for a particular individual as illustrated bellow. The black arrow shows the request flowing down to the database to retrieve the customer data, and the red arrow show the response flowing back up to the screen to display the data. In this example, the customer information consists of both customer data and order data.
The customer screen is responsible for accepting the request and displaying the customer information. It does not know where the data is, how it is retrieved, or how many database tables have to be queried to get the data. Once the customer screen receives a request to get customer information for a particular individual, it then forward the request onto the customer delegate module. This module is responsible for knowing which modules in the business layer can process that request and also how to get that module and what data it needs. The customer object in the business layer is responsible for aggregating all of the information needed by the business request (in this case to get the customer information). This module calls out to the customer dao ( data access object ) module in the persistence layer to get customer data, and also the order dao module to get order information. These modules in turn execute SQL statements to retrieve the corresponding data and pass it back up to the customer object in the business layer. Once the customer object receives the data, it aggregates the data and passes that information back up to the customer delegate, which then passes that data to the customer screen to be presented to the user.
From technology perspective, there are literally dozens of ways these modules can be implemented. For example, in the Java platform, the customer screen could be implemented as JSF ( java server faces ) screen coupled with the customer delegate as managed bean component. The customer object in the business layer can be local Spring bean or a remote EJB3 bean. The data access objects illustrated in the previous example can be implemented as simple POJO’s (Plain Old Java Objects ), MyBatis XML Mapper files or even objects encapsulating raw JDBC calls or Hibernate queries. From Microsoft platform perspective, the customer screen can be an ASP module using .NET framework to access C# modules in the business layer, with the customer and order data access modules implemented as ADO (ActiveX Data Objects). From the open source internet world, the presentation layer could be implemented as a one-page application written with angularjs (library from Google) or with ReactJs (library from Facebook) that communicate with the Business Layer via ajax requests. The Business Layer could be implemented in python or with PHP. For the layer itself you could use frameworks like Django or Laravel5.
The layered architecture patter is a solid general-purpose pattern, making it a good starting point for most applications, particularly when you are not sure what architecture pattern is best suited for your application. However, there are couple of things to consider from an architecture point of standpoint when choosing this pattern.
The first thing to watch out for is what is known as the architecture sinkhole anti-pattern. This anti-pattern describes the situations where requests flow through multiple layers of the architecture as simple pass-through processing with little or no logic performed within each layer. For example, assume the presentation layer responds to a request from the user to retrieve customer data. The presentation layer passes the request to the business layer, which simply passes the request to the presentation layer, which then makes a simple SQL call to the database layer to retrieve the customer data. The data is then passed all the way back up the stack with no additional processing or logic to aggregate, calculate, or transform the data.
Every layered architecture will have at least some scenarios that fall into the architecture sinkhole anti-pattern. The key, however, is to analyze the percentage of requests that fall into that category. The 80-20 rule is usually a good practice to follow to determine whether or not you are experiencing the architecture sinkhole anti-pattern.
Another consideration with the layer architecture pattern is that it tends to lend itself toward monolithic applications, even if you split the presentation layer and the business layer into separate deployable units. While this may not be a concern for some applications, it does pose some potential issues in terms of deployment, general robustness and reliability, performance and scalability.
The following table contains a rating and analysis of the common architecture characteristics for the layered architecture pattern. The rating for each characteristic is based on the natural tendency for that characteristic as a capability based on a typical implementation of a the pattern, as well as what pattern is generally known for.
Analysis: Overall agility is the ability to respond quickly to a constantly changing environment. While change can be isolated through the layers of isolation feature of this pattern, it is still cumbersome and time-consuming to make changes in this architecture pattern because of the monolithic nature of most implementations as well as the tight coupling of components usually found with this pattern.
Ease of deployment
Analysis: Depending on how you implement this pattern, deployment can become an issue, particularly for larger applications. One small change to a component can require a redeployment of the entire application, resulting in deployments that need to be planned, scheduled, and executed sometimes during off-hours or on weekends. As such, this pattern does not easily lend itself toward a continuous delivery pipeline, further reducing the overall rating for deployment.
Analysis: Because components belong to specific layers in the architecture, other layers can be mocked or stubbed, making this pattern is relatively easy to test. A developer can mock a presentation component or a screen to isolate testing within a business component, as well as mock the business layer to test certain screen functionality.
Analysis: While it is true layered architectures can perform well, the pattern does not lend itself to high-performance applications due to the inefficiencies of having to go through multiple layers of the architecture to fulfill a business request.
Analysis: Because of the trend towards tightly coupled and monolithic implementations of this pattern, applications build using this architecture pattern are generally difficult to scale. You can scale a layer architecture by splitting the layers into separate physical deployments or replicating the entire application into multiple nodes, but overall the granularity is too broad, making it expensive to scale.
Ease of development
Analysis: Ease of development gets a relatively high score, mostly because this pattern is so well known and is not overly complex to implement. Because most companies develop applications by separating skills set by layers (presentation, business, database) this pattern becomes a natural choice for most business-application development. The connection between a company’s communication and organization structure and the way it develops software is outlined is what is called Conway’s law. You can google “Conway’s law” to get more information about this fascinating correlation
Other software architecture approaches: Event-Driven Architecture