Difference between Layers and Tiers
In this article, I am going to discuss one of the most frequently asked interview questions i.e. what is the difference between layers and tiers? This question can also be asked in many different ways like
- What are 3-layer and 3-tier architecture? Or
- What do you mean by n-layer and n-tier architecture?
Layers:
The Layers refer to the logical separation of the application code. Logical layers help us to organize our code in a better way. For example, an application can have the following layers.
- Presentation Layer or UI Layer
- Business Layer or Business Logic Layer
- Data Access Layer or Data Layer
The above three layers reside in their own projects, maybe in 3 different projects or even more. When we compile the projects we get the respective layer DLL. So we have 3 DLL’s now.
Depending on how we deploy our application, we may have 1 to 3 tiers. As we have 3 DLL’s, if we deploy all the DLL’s on the same machine, then we have only 1 physical tier but 3 logical layers.
If we choose to deploy each DLL on a separate machine, then we have 3 tiers and 3 layers.
So, in short, we can define the Layers are the logical separation and the Tiers are the physical separation of an application. We can also say that tiers are the physical deployment of layers.
Tiers:
- The Presentation Tier which is also known as the UI Tier (Hosts the Presentation Layer or UI Layer). This can be considered as a web server in case of an ASP.NET web application.
- The Application Tier which is also called as Business Tier (Hosts Business Layer or Business Logic Layer) of the application.
- The Data Access Tier also is known as Data Tier (Hosts Data Access Layer or Data Layer).
- The Database Tier – It maybe SQL Server or Oracle or any other database which has tables, stored procedures, and other database objects.
The architecture of a typical n layer and n-tier Application:
Responsibilities of each layer or tier in an application:
- The Presentation Layer or Tier is usually responsible for interacting with the user.
- Business Layer or Tier is responsible for implementing the business logic of the application.
- The Data Access Layer or Tier is responsible for encapsulating the code that accesses the persistent data stores such as a relational database.
Once we understand the difference between n-tier and n-layer architecture, let’s discuss the advantages and disadvantages of using layered architecture.
Advantages of a Layered Architecture:
Layered architecture increases flexibility, maintainability, and scalability. In Layered architecture, we separate the user interface (i.e. presentation layer) from the business logic and the business logic from the data access logic layer. Separation of concerns among these logical layers and components is easily achieved with the help of layered architecture.
Multiple applications can reuse the components. The layered architecture allows us to reuse the same components in multiple applications. For example, if we want a windows user interface rather than a web browser interface, this can be done very easily just by replacing the UI component. All the other components like business logic, data access logic and the database remains the same.
The layered architecture enables teams to work on different parts of the application parallel with minimal dependencies on other teams. For example, some developers may work on the presentation layer while some other developers may work on the business logic layer at the same time some developers might work on the data access layer as well.
Different components of the application can be independently deployed, maintained, and updated, on different time schedules.
Layered architecture also makes it possible to configure different levels of security to different components deployed on different machines. So Layered architecture, enables us to secure portions of the application behind the firewall and make other components accessible from the Internet.
Layered architecture also helps us to test the components independently of each other.
Disadvantages of a Layered Architecture:
There might be a negative impact on the performance as we have the extra overhead of passing through layers instead of calling a component directly.
The development of user-intensive applications can sometimes take longer if the layering prevents the use of user interface components that directly interact with the database.
The use of layers helps to control and encapsulate the complexity of large applications but adds complexity to simple applications.
In this article, I try to explain the difference between layers and tiers in an application. I hope you enjoy this article and understand the difference between Layers and Tiers in an application.