March 12, 2013

N-Tier Architecture

Overview:


In Software Engineering, multi-tier architecture is an industry proved software architecture model suitable to support client-server architecture in which presentation, application processing and data management functions are logically separated. N-tier architecture provides a model by which developer can create flexible and reusable applications. .Net has many tools and features but .Net doesn't have pre-defined ways to implement N-tier architecture. Therefore, in order to gain good design and implementation using this architecture, understanding fully its concepts are very important. This post tries to clarify many basic concepts related to this architecture which are misunderstood by some peoples.

Introduction:

First we need to clarify the difference between two basic terminologies of N-Tier architecture in .Net. These are:

Tier vs Layer:

Tier usually means the physical deployment computer. An individual running server is also called one tier and sometimes several servers may also be counted as one tier such server flavour clustering. 

Layer usually means logic software component group. Layer is used for software development process. Layer has many advantages and is a good way to achieve N-tier architecture. Each layer may run in an individual tier. However multiple layers may also be able to run in one tier.

3-Tier Architecture:

Learn 3-Tier architecture concept first, it will help you to understand other concepts easily. A layer can access directly public component of its direct-below layer. For example, presentation layer can able to only access public components in data layer, but not in presentation layer. The three layers are briefly describe here as:

Presentation Layer:

A layer that users can access directly such as Desktop UI, web page and etc. Also called Client.

Application Layer:

This layer encapsulate the business logic such as business rules and validations, domain concepts, data access logic and etc. Also called Middle Layer.

Data Layer:

The external data source to store the application data such as database servers etc. For N-tier architecture, we need non-embedded database servers like MS SQL Server, Oracle etc which has an ability to run individually on single computer whereas embedded database servers like MS Access etc haven't have that ability.

1,2,3.....N-Tier Architecture:

1-Tier:

In 1-Tier architecture, we need to use embedded database systems which cannot be able to run in an individual computer, otherwise there will be at least 2-Tier layer because non-embedded database systems can run in an individual computer

2-Tier:

Only one layer can be run in an individual computer. It might be presentation, application or data layer. The whole application cannot run in more than two computers.

3-Tier:

The simplest case of N-tier architecture, all above layers are able to run in three separate computers. Practically three layers can also be deployed into one computer.

N-Tier:

Some layers in 3-Tier architecture can further broken into layers. These broken layers may able to run in many tiers. For example, Application Layer can broken into Business Layer, Presentation Layer can be broken into Client Layer. Detail of broken layers are:

Client Layer:

This layer is involve with users directly, There may be several different types of client coexisting are WPF, windows form, HTML web pages and etc.

Business Layer:

This layer handles and encapsulate all business domain logics and validations and also called Domain Layer.

Advantages and Disadvantages:

1 or 2-Tier Architecture:

Advantages: Simple and fast for lower number of users due to fewer processes. Easy to maintain and deployment due to less hardware and network bandwidth needed.

Disadvantages: Will face issue when number of users increasing. 

N-Tier Architecture:

Advantages: 
  • Scalable: This is due to its capability of multi-tier deployment and tier decoupling it brought.
  • Better Security Control: We can enforce the security for different tier if the system requirement is different for each tier. 
  • Better Fault Finder: The databases in data layer can be clusters for load balance purpose without effecting other layers.
  • Independent Tier Upgrading/Changes: Interface-dependency implementation can decouples all layers very well so that each layer can change without effecting other layers. 
  • Friendly and Efficient: The decouples layers are logic software component groups mainly by functionality. Each layer can be assigned individually to the team who specialise functional areas.
  • Better Re usability: Loosely coupled component groups are usually implemented in more general ways so they can be reuse by other applications.
Disadvantages: 
  • Slow Performance: The performance of whole application may be slow if the hardware and network bandwidth aren't good enough because more networks, computers and processes are involved.
  • High Cost Needed: More cost for hardware, network, maintenance and deployment because more hardware and better network bandwidth is needed. 

Effect on N-Tier Architecture:

The effect of N-Tier deployment on application performance are double edge issue. On one side, if the number of user isn't big enough, the performance may be slow due to more computers, process and network involvement. Namely if we put every thing on one layer, then performance of application will become better for a small number of users but when the number of users are big enough then scalability brought by N-Tier will improve the overall performance such as load balancing and database clustering which all improve the performance of  N-Tier architecture.

Business Validation in N-Tier Architecture:

Data validation is important in order to keep whole system healthy and integral. There are some rules for business validation as:
  • Data validation can be check on every layer. Usually the closer to the client layer is, the more efficient the performance is. 
  • When we decide which layer should do validation, we need to achieve a balance result between performance, reliability and also we need to make decision based on actual situation. 
  • A more practical way is to do a simple data validation using client-side validation such as JavaScript for performance and further do server-side validation for reliability.
  • We should implement and maintain one version of validation logics in one place, regardless of where the validation will be checked. All layers should share this version of validation logic in order to gain re-usability and avoid duplication to be called by any layer if needed.