The Clean Architecture

The Clean Architecture

We have seen the N-Layered architecture in the previous article. The problems which are in layered architecture those will be solved by the fallowing architecture that is The Clean Architecture.

Article content
Clean Architecture

  • Separates the elements of a design into circle levels.
  • Organize code with encapsulates the business logic.
  • Keep the core business logic and application domain at the center of the solution structure that independent with presentation and data access layers.
  • Clean architecture divided into two main elements: the policies and the details.
  • The policies: the business rules and procedures.
  • The details: The implementation code to carry out the policies.
  • Focus on the policies and business logics that build on project requirements.
  • The internal layers contains the business rules and has not dependency of any third-party library.

The Dependency Rule

  • The dependencies of a source code can only point inwards.
  • Code dependencies can only move from the outer levels inward.
  • Code on the inner layers can have no knowledge of functions on the outer layers.
  • Inner layer cannot have any information about elements of an outer layer.
  • Classes, functions, variables, data format, or any entity declared in an outer layer must not be mentioned by the code of an inner layer.

Layers of Clean Architecture

Article content
Layers of Clean architecture

Benefits of Clean Architecture

  • Independent of Database and Frameworks:

The software is not dependent on an ORM tool or Database. you can change them easily.

  • Independent of UI:

The UI can change easily, without changing the rest of the system and business rules.

  • Testable:

It is naturally testable. You can test business rules without considering UI, Database, Mock servers etc..

  • Independence of any external agency:

In fact, your business rules simply don't know anything at all about the outside world.


Article content
Clean Architecture

Let see the clean architecture with the real world technologies:

Article content
Real world technologies in clean architecture

Evaluation of Clean Architecture:

Benefits of clean architecture:

  • Easy development, Debug and Deploy.
  • Loosely Coupled of independent layers.
  • Flexible logical layers.
  • The code becomes more testable and independent changeable to 3rd party libraries

Drawbacks of clean architecture:

  • Vertical business logical implementation codes required to modify all layers i.e. add to basket, checkout orders.
  • It is still monolithic and has scalability issues.

Problem with clean architecture:

It is still monolithic architecture only, so:

  1. When our E-commerce business is growing.
  2. Need to handle greater amount of requests per second during any festivals offers.
  3. Still, the latency problem with the users.

Solutions for solving each problem of Clean Architecture:

Scalability

  • Scalability is the number of requests an application can handle.
  • Measured by the number of requests and it can effectively support simultaneously.
  • If no longer handle any more simultaneous requests, it has reached its scalability limit.
  • To prevent downtime, and reduce latency, you must scale.
  • Horizontal scaling and vertical scaling both involve adding resources to your computing infrastructure.
  • Horizontal scaling by adding more machines.
  • Vertical scaling by adding more power.

Vertical Scaling - Scaling UP

  • Vertical scaling is basically makes the nodes stronger.
  • Make the server stronger with adding more hardware. Adding more resources to a single node.
  • Make optimization the hardware that will allow you to handle more requests.
  • Vertical scaling keeps your existing infrastructure but adding more computing power.
  • Your existing code doesn't need to change.
  • Adding additional CPU, RAM, and DISK to cope with an increasing workload.
  • By scaling up, you increase the capacity of a single machine. And it has limits. That is named Scalability Limits.
  • Because even the hardware has maximum capacity limitations.
  • For handling millions of request, we need horizontal scaling or scaling out.

Horizontal Scaling - Scale out

  • Horizontal scaling is splitting the load between different servers.
  • Simply adds more instances of machines without changing to existing specifications.
  • Share the processing power and load balancing across multiple machines.
  • Horizontal scaling means adding more machines to the resource pool.
  • Scaling horizontally gives you scalability but also reliability.
  • Preferred way to scale in distributed architectures.
  • When splitting into multiple servers, we need to consider if you have a state or not.
  • If we have a state like database servers, than we need to manage more considerations like CAP theorem.

Adding Load balancer to application architecture:

  • Balance the traffic across to all nodes of our applications.
  • Spread the traffic across a cluster of servers to improve responsiveness and availability.
  • Load Balancer sits between the client and the server.
  • Load Balancer is accepting incoming network and application traffic.
  • Distributing the traffic across multiple backend servers using different algorithms.
  • Load Balancers should be fault tolerance and improves availability.
  • Mostly uses the consistent hashing algorithms. Consistent hashing is an algorithms for dividing up data between multiple machines.
  • It solves the horizontal scalability problems. Don’t have to re-arrange all the keys

Now the application architecture becomes like:

Article content

Pain points of Clean Architecture

  • Context Switching

When we were working on a feature and needed to work on any combination of data access, domain or application logic, we had to switch contexts into different software project.

  • We had to remember and re-visit codes from other folder and continue with totally different code base. The folder structure requires DDD-bounded contexts approaches.
  • Vertical Slices

When adding a feature in an application, we are developing into almost all layers in the application code.

  • Changing the user interface, adding new Use Case classes into Application Layer, adding fields to models, modifying Data Access Codes, and so on.
  • So that means we are highly couple with vertically slice when developing features

Article content
While new feature implementing in clean architecture

Problems with Agility of New Features and Split Agile Teams

  • Our E-Commerce Business is growing.
  • Business teams are separated teams as per departments; Product, Sale, Payment..
  • And all teams wants to add new features to compete the market.
  • Codebase not allowed to manage it.
  • Context Switching and Vertical Slices problems on Clean Architecture

The solution for above problems is: Modular Monolithic Architecture.

Thanks for reading...

Bùi Huy Đạt

.NET Developer | Azure

7mo

Very interesting article, it saved me time to understand more about Clean Architecture. I have some questions so I hope you can answer. I think the issues mentioned in the "Problem with clean architecture" section are not entirely accurate. They are not problems unique to Clean Architecture but rather common issues in software development in general. As I understand it, Clean Architecture is simply a methodology that helps us organize code more effectively, dividing it into multiple layers and not dependencies between these layers. It is not limited to Monolithic Architecture but can also be applied to organize code in Microservices.

Mourya Varma Kolukuluri

Data Engineer, AI Generalist

7mo

Interesting

To view or add a comment, sign in

Others also viewed

Explore topics