Design Clean Architecture for E-Commerce Applications with Step by Step

In this article, we are going to learn how to design E-Commerce Applications with Clean Architecture and Step by Step.

Clean Architecture

Designing an e-commerce application using Clean architecture can be a complex task. We will iterate the architecture design one by one as per requirements. But before designing E-Commerce application, We should ask ourselves: What we have in our design toolbox ?

I have just published a new course — Design Microservices Architecture with Patterns & Principles.

Architecture Design Flow

Before we design any architecture we will always start from Problem which is Business Problem that drive our design considerations.

Software Architecture: Way of Learning

After that, we have learned Architecture Design Patterns and Principles about Clean Architectures that you can check articles below:

So now, according to our Architecture Design flow at above, We are at Design step. But before design our system We should ask ourselves:

  • What we have in our design toolbox ? What we have learned so far ?

So that means, every time when we design our architecture, We should always start with writing down FRs and NFRs. Also its good to write-down Constraints or Limitations of project, for example cost, time and so on.

Architecture Design Toolbox

Here you can see the Architecture Design Toolbox that we will use during the article when designing our E-Commerce application:

Functional Requirements — FRs

Basically we are going to design simple e-commerce application and we need to write down business cases that means write down Functional-requirements:

  • List products
  • Filter products as per brand and categories
  • Put products into the shopping cart
  • Apply coupon for discounts and see the total cost all for all of the items in shopping cart
  • Checkout the shopping cart and create an order
  • List my old orders and order items history

Non-Functional Requirements — N-FRs

After getting requirements, as a Software Architect, we should consider Non-Functional Requirements. Since this is the first iteration of our e-commerce application, we don’t aim to super scalability and handle millions of request. Instead we will start with small and basics thats why I have added to

  • Availability
  • Small number of concurrent users that we should handle in the first design.
  • Maintainability
  • Flexibility
  • Testable

and also in this for this section, I have added to Flexibility and Testable which required for the organizing project codes properly.

Architecture Styles

After understanding Functional and Non-Functional requirements, now we can see our Design toolbox which includes Architectures, Patterns, Principles and Best Practices that we have learned and will apply on our project. Lets see which architecture styles that we have learned of course we have using Monolithic Architecture and Layered Architecture but also we will use Clean Architecture in our design.

As you know that, Clean Architecture also still following Monolithic architecture in terms of server design perspective so we can say that Layered Architecture is a sub-architecture style of Monolithic architecture. Because we will still have big application server, we won’t decompose servers now, instead we will separate logical layers into our 1 big application server in order to organize our projects better.

This time we will focus on broke the dependencies of horizontal logical layers to the circle layers with Clean Architecture.

Patterns & Principles

Its good to add principles in our picture in order to remember them always. And If we expand our Patterns & Principles, we can see that

And also we have learned and added below principles for this chapter;

  • The Dependency Rule

We are going to consider these principles when design our architecture. So for now we have learned only these items and load our design toolbox with these items. But with continue our course we will discover new architectures, patterns and principles.

According to our design toolbox, now lets design the first version of our e-commerce application with using Clean architecture.

Design Clean Architecture — E-Commerce App

Here you can see the final architecture design for our e-commerce application which is applying Clean architecture.

Let me explain flow of request in our architecture:

  • Client will be send request from web, and this request accommodate from our big monolithic application server but our project organized by Clean architecture which every layers concerns their own responsibilities and perform database calls in order to persist data on relational database.

The only differences are now we are in flexible design and able change our UI, Database and Frameworks with testable codes.

Adapt Technology Stack — E-Commerce App — Clean Architecture

We will implement possible Technology choices. As you know that we have designed our architecture with applying Clean Architecture and Design principles which is The Dependency Rule.

You can see image of “Way of Learning — The Design Flow” of our articles: According to Flow, now its time to Adapting Technology Chooses.

In this step we will Apply Software Tools, Application Frameworks and Databases for our current design. So, I have created 2 different Technology stack, one for Java another for Microsoft .Net ecosystem:

As you can see the image, for Java Technology stack. I have added Tomcat Web Server to deploy our monolithic application into this. And Developing our application with Java Enterprise Edition and created 1 big jar or war file to deploy to our Tomcat web server Application , Java Single WAR Artifact and Tomcat Container. And perform database operations with relational database which is MySQL, or you can pick PostgreSQL or Oracle in here.
If we look at the Microsoft .Net ecosystem: tools changed a bit: We use IIS for Web Server for .Net applications. We use Asp.Net as an application framework when developing our layered architecture and we use SQL Server as a relational database for performing database crud operations.

As you can see that we have design and adapt with technology stack for our e-commerce application.

DEMO: Clean Architecture Code Review — E-Commerce App

We are going to code review of e-commerce application which is implemented on Clean Architecture with .Net ecosystem and Asp.Net and use SQL server for relational databases. This is real-world Asp.Net e commerce web application implemented Clean Architecture, I have developed this repository a long time ago but it is still relevant for our topic.

Here you can see 2 GitHub link at below. In the first link we go to GitHub repository page: Code review of Asp.Net e-commerce web application:

Please goto links and examine Readme file. With Second link you can open repository as a project folder view. This is implementation of aspnetrun-basic template on real-world basic e-commerce web application project; You can see the animation we have developed for all FR of our e-commerce application like listing products, add to basket, checkout order and so on.

Here you can see the folder structure that represents our Layers. Repository include layers divided by 4 project;

Core Layer: Development of Domain Logic with abstraction. Interfaces drives business requirements with light implementation. The Core project is the center of the Clean Architecture design, and all other project dependencies should point toward it.

Infrastructure Layer: Implementation of Core interfaces in this project with Entity Framework Core and other dependencies. Most of your application’s dependence on external resources should be implemented in classes defined in the Infrastructure project.

Application Layer: Development of Domain Logic with implementation. Interfaces drives business requirements and implementations in this layer. Application layer defines that user required actions in app services classes.

Web Layer: Development of UI Logic with implementation. Interfaces drives business requirements and implementations in this layer. The application’s main starting point is the ASP.NET Core web project.

Evaluate: Clean Architecture

If we evaluate the current Clean Architecture for E-Commerce Application, you can see the Benefits and Drawbacks at below :

So these are the drawbacks of our current design. That means, even we have separate logical layers, it has lots of Drawbacks. If we look at the drawback we can divide by 2;

  1. First one is code organization problems, yes we separate clean logical layer but still struggle to implement vertical business logics
  2. Second one is Scalability.

I would like to focus and solve the second problem which is Scalability issues.

Problem: Increased Traffic, Handle More Request

We are going to discover Problems of current Clean Architecture — E-Commerce Application. So when we are in discover Problem step,
we should ask ourselves

  • What’s wrong with this current architecture ?
  • How can we improve the current design when project goes more advanced level ?

Here you can see Problems and potential solutions of current design:

If you look at the table on image, you can see that we have handle only 2K concurrent request per second. But Our E-Commerce Business is growing and required to handle more amout of concurrent request at the same time and it should be acceptable latency for our users.

  • So How can we scale the application if we need to handle more users in our application ?

And the solutions are

  • Vertical and Horizontal Scaling
  • Scale Up and Scale Out
  • Load Balancer

Design Microservice Architecture — E-Commerce App

If we design e-commerce application with Microservice architecture, you can see the image below:

Product microservice can use NoSQL document database Shopping Cart microservice can use NoSQL key-value pair database and Order microservice can use Relational database as per microservice data storage requirements.

What’s Next ?

Step by Step Design Architectures w/ Course

I have just published a new course — Design Microservices Architecture with Patterns & Principles.

In this course, we’re going to learn how to Design Microservices Architecture with using Design Patterns, Principles and the Best Practices. We will start with designing Monolithic to Event-Driven Microservices step by step and together using the right architecture design patterns and techniques.

--

--

Mehmet Ozkaya
Design Microservices Architecture with Patterns & Principles

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya