Professional Documents
Culture Documents
In this article, we’re going to learn how to Design Microservices Architecture with
using Design Patterns, Principles and the Best Practices. We will using the right
architecture design patterns and techniques.
By the end of the article, you will Learn how to handle millions of request with
designing system for high availability, high scalability, low latency, and resilience to
network failures on microservices distributed architectures.
Event-Driven Architecture
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 1/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
This course is will be the journey of software architecture design with step by step
evolving architecture monolithic to event driven microservices.
Layered Architecture
SOA
Microservices
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 2/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
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.
Article Flow
In the article flow will be the both theoretical and practical information;
After that we will see the Reference architectures that applied these patterns
After that we will design our architecture with applying this newly learned pattern
with together
And lastly we will decide which Technologies can be choices for that architectures.
Evolve architecture
We will evolve these architectures as per questions
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 3/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
Scalability and reliability are measures of how well your application can be served to
end-users. If our e-commerce application can serve millions of users without noticeable
downtime, then we can say the system is highly scalable and reliable. Scalability and
availability are probably the major factors for designing good architecture.
As you can see in the table, we will start a small e-commerce application that get only 2K
concurrent user and gets 500 request per second.
And we will design our e-commerce architecture as per these expected volumes.
After that, when our business grow and grow, it will required more resources to
accommodate bigger request counts, and we will see how we can evolve our architecture
as per these numbers.
Monolithic Architecture
There are many approaches and patterns that evolved over decades of software
development, and all have their own benefits and challenges.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 4/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
When it comes to legacy applications, we can say that most of legacy applications are
mainly implemented as a monolithic architecture.
There are several advantages to the monolith approach that we will discuss them in the
upcoming videos. But let me say some main advantages and disadvantages here.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 5/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
Since it is a single code base, its easy to pull and get started to project.
Since project structure in 1 project and easy to debug business interactions across
different modules.
Unfortunately, the monolith architecture has lots of disadvantages, we can say that;
It becomes too large in code size with time thats why its really difficult to manage.
and so on..
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 6/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
In this section we are going to design our e-commerce application with the monolithic
architecture step by step.
We will iterate the architecture design one by one as per requirements.
We should always start with writing down FRs (Functional Requirements) and NFRs
(Non-Functional Requirements).
Functional Requirements
List products
Apply coupon for discounts and see the total cost all for all of the items in shopping
cart
Non-Functional Requirements
Scalability
Also, its good to add principles in our picture in order to remember them always.
Principles
KISS
YAGNI
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 7/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
As you can see that we have designed our e-commerce application with Monolithic
Architecture.
We have added the big e-commerce box what is the components of our e-commerce
application; Shop UI, Catalog Service, SC Service, Discount Service, Order Service. As
you can see that all modules of this traditional web application are single artifacts in the
container.
This monolithic application has a massive codebase that includes all modules. If you
introduce new modules to this application, you have to make changes to the existing
code and then deploy the artifact with a different code to the Tomcat server. We
followed our KISS principle that is keep it simple.
And we will refactor our design as per requirements and step by step iterate together.
Basically, Load Balancer will accommodate the request and send request to our e-
commerce application servers with using consistent hashing algorithms. This will
provide to load equally for the servers.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 8/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
As you can see the image, we have picked the potential options for our e-commerce
monolithic application. NGINX is very good option for Load Balancing and also Java —
Oracle is standart implementations of this kind of applications.
Microservices Architecture
Microservice are small business services that can work together and can be deployed
autonomously / independently.
Microservices
— have their own technology stack, included the database and data management
model;
— communicate to each other over a combination of REST APIs, event streaming, and
message brokers;
— are organized by business capability, with the line separating services often referred
to as a bounded context.
we will also see how we can decouple microservices with bounded context in the
upcoming sections.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a76827279… 9/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
Microservices Characteristics
Microservices are small, independent, and loosely coupled. A single small team of
developers can write and maintain a service. Each service is a separate codebase, which
can be managed by a small development team.
Services can be deployed independently. A team can update an existing service without
rebuilding and redeploying the entire application.
Services are responsible for persisting their own data or external state. This differs from
the traditional model, where a separate data layer handles data persistence.
Scalability.
Microservices can be scaled independently, so you scale out sub-services that require
less resources, without scaling out the entire application.
Data integrity.
Microservice has its own data persistence. So data consistency can be a challenge.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 10/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
So now these database can be polyglot persistence. That means Product ms can use
NoSQL document database SC ms can use NoSQL key-value pair database and Order ms
can use Relational database as per ms data storage requirements.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 11/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
See that UI and Microservices communication are directly, and it seems hard to manage
communications.
We now we should focus on Microservices communications !!
Microservices Communications
One of the biggest challenge when moving to microservices-based application is
changing the communication mechanism. Because microservices are distributed and
microservices communicate with each other by inter-service communication on network
level. Each microservice has its own instance and process.
Therefore, services must interact using an inter-service communication protocols like
HTTP, gRPC or message brokers AMQP protocol.
Since microservices are complex structure into independently developed and deployed
services, we should be careful when considering communication types and manage
them into design phases.
The pattern provides a reverse proxy to redirect or route requests to your internal
microservices endpoints. An API gateway provides a single endpoint for the client
applications, and it internally maps the requests to internal microservices. We should
use API Gateways between client and internal microservices.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 12/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
In summary, the API gateway locate between the client apps and the internal
microservices. It is working as a reverse proxy and routing requests from clients to
backend services. It is also provide cross-cutting concerns like authentication, SSL
termination, and cache.
You can see the image that is collect client request in single entry-point and route
request to internal microservices.
This will handle the client requests and route the internal microservices,
also aggregate multiple internal microservices into a single client request
and performs cross-cutting concerns like Authentication and authorization, Rate
limiting and throttling and so on..
We will continue to evolve our architecture, but please look at the current design and
think how we can improve design ?
There are several client applications connect to single API Gateway in here.
We should careful about this situation, because if we put here a single api gateway, that
means its possible to single-point-of-failure risk in here.
If these client applications increase, or adding more logic to business complexity in API
Gateway, it would be anti-pattern.
But this makes a single-point-of failure So In order to solve this problem BFF offers to
create several API Gateways and grouping the client applications according to their
boundaries and split them different API Gateways.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 14/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
A single and complex api gateway ca be risky and becoming a bottleneck into your
architecture. Larger systems often expose multiple API Gateways by grouping client type
like mobile, web and desktop functionality. BFF pattern is useful when you want to avoid
customizing a single backend for multiple interfaces.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 15/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
As you can see that we have added to several API Gateways in our application. These api
gateways provide to best match the needs of the frontend environment, without
worrying about affecting other frontend applications. The Backend for Frontends
pattern provides direction for implementing multiple gateways.
When designing microservices applications, we should careful about that how back-end
internal microservices communicate with each other. The best practice is reducing inter-
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 16/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
For example, look at the image and think about the use case like;
If the client checkout shopping cart, this will start a set of operations.
So if we try to perform this place order use case with Request/Response sync Messaging
pattern, than it will seems like as this image.
AS you can see that there is 6 sync http request for one client http request.
So it is obvious that increase latency and negatively impact the performance, scalability,
and availability of our system.
If we have this of use case, what if the step 5 or 6 is failed, or what if some middle service
are down ?
Even if there is no down, it could be busy of some services that can’t response in a time
manner and that caused not acceptable high latencies.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 17/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 18/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
As you can see that we have applied Service Aggregator Pattern — Service Registry
Pattern for our e-commerce architecture.
Otherwise that dependency and coupling of microservices will create bottleneck and
create serious problems of the architecture.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 19/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
Similarly, subscribers express interest and only receive messages that are of interest,
without knowledge of which publishers send to them.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 20/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
In this section, we are going to iterate our e-commerce architecture with adding
Pub/Sub Message Broker for providing Microservices Asynchronous Communications
Design.
As you can see that we have applied Pub/Sub Message Broker — Microservices
Asynchronous Communications Design patterns.
If we Adapting Technology Stack, we basically start with what options can be for
Pub/Sub Message Brokers. There 2 good alternatives that you can pick;
1- Kafka
2- RabbitMQ
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 21/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
So that means we have several patterns and practices when handling data interactions
between microservices, we will learn this patterns and principles in this section.
Microservices are independent and perform only specific functional requirements, For
our case in e-commerce application we have product, basket, discount, ordering
microservices need to interact each other to perform customer use cases. So that means
they need to integrate frequently with each others. And mostly these integrations are
querying each services data for aggregation or perform logics.
In order isolate Commands and Queries, its best practices to separate read and write
database with 2 database physically. By this way, if our application is read-intensive that
means reading more than writing, we can define custom data schema to optimized for
queries.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 22/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
By this isolation, we can even use different database for reading and writing database
types like using no-sql document database for reading and using relational database for
crud operations.
After that the read database of CQRS design pattern provides materialized views of the
data with denormalized tables. Of course this materialized views read database
consumes events from write database and convert them into denormalized views.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 23/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
With applying Event Sourcing pattern, it is changing to data save operations into
database. Instead of saving latest status of data into database, Event Sourcing pattern
offers to save all events into database with sequential ordered of data events. This events
database called event store.
Instead of updating the status of a data record, it append each change to a sequential list
of events. So The Event Store becomes the source-of-truth for the data. After that, these
event store convert to read database with following the materialized views pattern. This
convert operation can handle by publish/subscribe pattern with publish event with
message broker systems.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 24/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
So when user create or update an order, I am going to use relational write database, and
when user query order or order history, I am going to use no-sql read database and make
consistent them when syncing 2 databases with using message broker system with
applying publish/subscribe pattern.
Now we can consider tech stack of these databases, I am going to use SQL Server for
relational writing database and using Cassandra for no-sql read database. Of course we
will use Kafka for syncing these 2 database with pub/sub Kafka topic exchanges.
As you can see that we have finished to design with microservices database patterns.
Lets deep dive into these event-driven architecture in microservices.
We said that, with the event-driven architectures we can do asynchronous behavior and
loosely coupled structures. In example, instead of sending request when data needed,
services consume them via events. This will provide to performance increases.
But also there are huge Innovations on the Event-Driven Microservices Architectures like
using real-time messaging platforms, stream-processing, event hubs, real-time
processing, batch processing, data intelligence and so on.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 25/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
So we can make this event-driven approach is more generic and real-time event
processing features with evolving this architecture.
Now lets decide to Technology stack in this architecture. Of course we should pick
Apache Kafka — as a Event hub and Apache Spark for real-time and near-real-time
streaming applications that transform or react to the streams of data.
As you can see that now we have reactive design with Event-Driven Microservices
Architecture.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 26/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
And with this latest event-driven microservices architecture which Deployments with
Containers and Orchestrators, can be accommodate target concurrent request in a low
latency.
Because this architecture is fully loosely coupled and design for high Scalability and high
Availability.
As you can see that we have designed our e-commerce microservices architecture with
all aspects of design principles and patterns. Now you can ready to design your own
architectures with these learning and know how to use these patterns toolbox in your
designs.
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.
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 27/28
9/16/21, 2:27 PM Monolithic to Microservices Architecture with Patterns & Best Practices | by Mehmet Özkaya | Design Microservices Architecture …
Architecture Pattern
https://medium.com/design-microservices-architecture-with-patterns/monolithic-to-microservices-architecture-with-patterns-best-practices-a7682727… 28/28