SlideShare a Scribd company logo
Patterns & Practices for Implementing
Event-driven Microservices
Nikhil Barthwal
@nikhilbarthwal
nikhilbarthwal@hotmail.com
www.nikhilbarthwal.com
Agenda (1/2)
• Motivations to move from Monolithic to Microservices
• Understanding the Distributed Data in Microservices
• Using Domain Driven Design for partitioning data
• Problems with Distributed Data
• Event Driven Microservices
2
Agenda (2/2)
• Communication Patterns for Microservices
• Event Sourcing
• Command Query Responsibility Segregation
• Introducing Sagas
• Resources for further reading
3
Monolithic: The old school approach …
• Large code base → Slower development
• More time to test & deploy
• Too much team interdependency
Database
Web Service
Easy to Develop, Test and Deploy …
But doesn’t scale!
4
Microservices: The Brave new world ...
• Smaller code base to understand & change
• Autonomous teams
• Faster testing & deployment
Assembly of Loosely
coupled services!
Image Source: https://www.slideshare.net/JontheBeach/distributed-sagas-a-protocol-for-coordinating-microservices
5
Loose Coupling → Distributed Data
Database per Service!
Faster Innovation!
Loosely coupled services
Reduces team interdependencies
Image Source: http://microservices.io/patterns/microservices.html
6
How to create service boundaries?
An approach to software development for complex needs
by connecting the implementation to an evolving model.
• Entities
• Value Objects
• Bounded Context
• Aggregates
Infrastructure Layer
Domain Layer
Application Layer
User Interface
Use Domain Driven Design (DDD)!
7
DDD: Aggregates
• Cluster of domain objects that
can be treated as a single unit
• One of its component is root
• All outside reference would
only go to the aggregate root
Domain Model
=
Collection of Aggregates
8
Data Partition Strategies: Use DDD
Aggregates
provide
boundaries
for
partition
9
Problems with Distributed Data
• How do we keep data consistent?
• How do we query scattered data?
10
Cannot use ACID Transactions
BEGIN TRANSACTION
SELECT ADDRESS FROM CUSTOMERS WHERE CUSTOMER_ID = XXX
SELECT PRICE FROM PRODUCTS WHERE CUSTOMER_ID = YYY
INSERT INTO ORDERS ...
COMMIT TRANSACTION
Private to Customer service
Private to Product service
11
Cannot use 2-Phase Commit
• Coordinator is a single point of failure
• Very chatty, with O(4n) messages, O(n2) with retries
• Locks would decrease throughputs
• Impacts availability (CAP theorem limitations)
Guarantees consistency, but …
12
Consistency Eventual Consistency
Availability
Partition (Network)
Eventual Consistency
Use Event Driven Microservice Architecture!
13
Event Driven Architecture
• Event occurs when a change happens in system
• All listeners get notified of the event, may take action
• Highly distributed/loosely coupled architecture
• Often used for asynchronous flows of information
14
Communication: Direct Messaging
Benefits:
• Easier implementation
• Better Error control
Drawbacks:
• Increased coupling
• Difficult to scale
Order Service
Product
Service
Customer
Service
Fulfillment
Service
Payment
Service
Shipping
Service
15
Communication: Via Message broker
Benefits:
• More scalable
• Increased Decoupling
Drawbacks:
• Requires Broker as Highly
Available component
Order Service
Product
Service
Customer
Service
Fulfillment
Service
Payment
Service
Shipping
Service
Message Broker
16
Communication Patterns
Request/Response
(synchronous)
Event-Driven
(asynchronous)
Tier-3: Little to No impact for Customer
Tier-2: Impacting Customers indirectly
Tier-1: Directly Customer facing
17
Event Sourcing: Introduction
• Modeling state changes as sequence of events
• Storing the event that could trigger the state change
• Enables rolling back to particular time in history
Examples:
18
Event Sourcing: Snapshots
Alternative to playing events: Snapshots
19
Event Sourcing: Multiple views
Image Source: https://www.confluent.io/wp-content/uploads/2016/09/Event-sourced-based-architecture.jpeg
Adding applications that process event …
but create a different view!
20
Event Sourcing: Benefits & Drawbacks
Benefits:
• 100% accurate audit logging
• Easy temporal queries
• Applications can process
same events but create views
Drawbacks:
• Adds Complexity
• No Strict Consistency
• Longer bootup times
(Snapshots can help)
21
Command Query Responsibility
Segregation
CQRS Pattern
Must for Event Sourcing!
Read
Write
Image Source: https://martinfowler.com/bliki/CQRS.html
22
CQRS: Benefits & Drawbacks
Benefits:
• Needed for Event Sourcing
• Improved separation of
concerns
• Supports scalable multiple
denormalized views
Drawbacks:
• Increased complexity
• Potential code duplication
• Replication lag as No Strict
Consistency
23
Sagas: Introduction
• Based on a 1987 paper
• Initially for a single database running
on one node
• Now adapted for distributed systems
with asynchrony and partial failure
24
Transaction & Rollback Transaction
• Every Transaction has a Rollback transaction
• This logic must be included in the service
T1 T2 T3
R1 R2 R3
Transactions →
Rollback Transactions →
25
Sagas: Example
This action initiates the saga
Check Credit Service Withdrawal Service Deposit Service
Deposit Check
Long running transactions …
use compensating actions to handle failures!
Does Sender have
enough money in his
account?
Withdraw the money
(unless it is on hold)?
Deposit the money
(unless account is
deactivated)?
26
Resources
• Using DDD to develop Microservices: https://www.infoq.com/news/2016/02/ddd-
microservices
• Overview of Saga Pattern: http://microservices.io/patterns/data/saga.html
• Distributed Sagas: https://www.slideshare.net/JontheBeach/distributed-sagas-a-
protocol-for-coordinating-microservices
27
Summary
• Microservices enable faster innovation
• DDD can used to define service boundaries
• Data is distributed which create challenges
• Use Event Driven Architecture for eventual consistency
• Understand Event Sourcing & CQRS
• Use Sagas to maintain data consistency across services
28
Thank you!
29
Nikhil Barthwal
Questions?
@nikhilbarthwal
nikhilbarthwal@hotmail.com
www.nikhilbarthwal.com
Ad

Recommended

Event-Driven Architecture (EDA)
Event-Driven Architecture (EDA)
WSO2
 
Event-driven microservices
Event-driven microservices
Andrew Schofield
 
Micro services Architecture
Micro services Architecture
Araf Karsh Hamid
 
Introduction to Microservices
Introduction to Microservices
MahmoudZidan41
 
Microservice vs. Monolithic Architecture
Microservice vs. Monolithic Architecture
Paul Mooney
 
Event driven architecture
Event driven architecture
Shadrach Jabonir
 
Introduction to Microservices
Introduction to Microservices
Roger van de Kimmenade
 
Microservice Architecture
Microservice Architecture
tyrantbrian
 
Windows Azure Service Bus
Windows Azure Service Bus
Return on Intelligence
 
Microservices
Microservices
SmartBear
 
Introduction to microservices
Introduction to microservices
Paulo Gandra de Sousa
 
Microservices Architecture - Cloud Native Apps
Microservices Architecture - Cloud Native Apps
Araf Karsh Hamid
 
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Edureka!
 
Event Driven Architecture
Event Driven Architecture
Stefan Norberg
 
Event Driven Software Architecture Pattern
Event Driven Software Architecture Pattern
jeetendra mandal
 
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Richard Langlois P. Eng.
 
Microservice architecture design principles
Microservice architecture design principles
Sanjoy Kumar Roy
 
Event Driven Architecture
Event Driven Architecture
Chris Patterson
 
Event driven microservices
Event driven microservices
Anthony Martin
 
IBM MQ - High Availability and Disaster Recovery
IBM MQ - High Availability and Disaster Recovery
MarkTaylorIBM
 
Introduction To Microservices
Introduction To Microservices
Lalit Kale
 
Introduction to Microservices Patterns
Introduction to Microservices Patterns
Dimosthenis Botsaris
 
Event Driven Architecture (EDA) Reference Architecture
Event Driven Architecture (EDA) Reference Architecture
Bob Rhubart
 
Why a Multi-cloud Strategy is Essential
Why a Multi-cloud Strategy is Essential
Alibaba Cloud
 
What are Microservices | Microservices Architecture Training | Microservices ...
What are Microservices | Microservices Architecture Training | Microservices ...
Edureka!
 
App Modernization Pitch Deck.pptx
App Modernization Pitch Deck.pptx
MONISH407209
 
Event Driven Architecture (EDA) Reference Architecture | Anbu Krishnaswamy
Event Driven Architecture (EDA) Reference Architecture | Anbu Krishnaswamy
Bob Rhubart
 
Infrastructure as Code
Infrastructure as Code
Robert Greiner
 
Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)
Rick Hightower
 
Iot cloud service v2.0
Iot cloud service v2.0
Vinod Wilson
 

More Related Content

What's hot (20)

Windows Azure Service Bus
Windows Azure Service Bus
Return on Intelligence
 
Microservices
Microservices
SmartBear
 
Introduction to microservices
Introduction to microservices
Paulo Gandra de Sousa
 
Microservices Architecture - Cloud Native Apps
Microservices Architecture - Cloud Native Apps
Araf Karsh Hamid
 
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Edureka!
 
Event Driven Architecture
Event Driven Architecture
Stefan Norberg
 
Event Driven Software Architecture Pattern
Event Driven Software Architecture Pattern
jeetendra mandal
 
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Richard Langlois P. Eng.
 
Microservice architecture design principles
Microservice architecture design principles
Sanjoy Kumar Roy
 
Event Driven Architecture
Event Driven Architecture
Chris Patterson
 
Event driven microservices
Event driven microservices
Anthony Martin
 
IBM MQ - High Availability and Disaster Recovery
IBM MQ - High Availability and Disaster Recovery
MarkTaylorIBM
 
Introduction To Microservices
Introduction To Microservices
Lalit Kale
 
Introduction to Microservices Patterns
Introduction to Microservices Patterns
Dimosthenis Botsaris
 
Event Driven Architecture (EDA) Reference Architecture
Event Driven Architecture (EDA) Reference Architecture
Bob Rhubart
 
Why a Multi-cloud Strategy is Essential
Why a Multi-cloud Strategy is Essential
Alibaba Cloud
 
What are Microservices | Microservices Architecture Training | Microservices ...
What are Microservices | Microservices Architecture Training | Microservices ...
Edureka!
 
App Modernization Pitch Deck.pptx
App Modernization Pitch Deck.pptx
MONISH407209
 
Event Driven Architecture (EDA) Reference Architecture | Anbu Krishnaswamy
Event Driven Architecture (EDA) Reference Architecture | Anbu Krishnaswamy
Bob Rhubart
 
Infrastructure as Code
Infrastructure as Code
Robert Greiner
 
Microservices
Microservices
SmartBear
 
Microservices Architecture - Cloud Native Apps
Microservices Architecture - Cloud Native Apps
Araf Karsh Hamid
 
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Microservice Architecture | Microservices Tutorial for Beginners | Microservi...
Edureka!
 
Event Driven Architecture
Event Driven Architecture
Stefan Norberg
 
Event Driven Software Architecture Pattern
Event Driven Software Architecture Pattern
jeetendra mandal
 
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Microservice Architecture Patterns, by Richard Langlois P. Eng.
Richard Langlois P. Eng.
 
Microservice architecture design principles
Microservice architecture design principles
Sanjoy Kumar Roy
 
Event Driven Architecture
Event Driven Architecture
Chris Patterson
 
Event driven microservices
Event driven microservices
Anthony Martin
 
IBM MQ - High Availability and Disaster Recovery
IBM MQ - High Availability and Disaster Recovery
MarkTaylorIBM
 
Introduction To Microservices
Introduction To Microservices
Lalit Kale
 
Introduction to Microservices Patterns
Introduction to Microservices Patterns
Dimosthenis Botsaris
 
Event Driven Architecture (EDA) Reference Architecture
Event Driven Architecture (EDA) Reference Architecture
Bob Rhubart
 
Why a Multi-cloud Strategy is Essential
Why a Multi-cloud Strategy is Essential
Alibaba Cloud
 
What are Microservices | Microservices Architecture Training | Microservices ...
What are Microservices | Microservices Architecture Training | Microservices ...
Edureka!
 
App Modernization Pitch Deck.pptx
App Modernization Pitch Deck.pptx
MONISH407209
 
Event Driven Architecture (EDA) Reference Architecture | Anbu Krishnaswamy
Event Driven Architecture (EDA) Reference Architecture | Anbu Krishnaswamy
Bob Rhubart
 
Infrastructure as Code
Infrastructure as Code
Robert Greiner
 

Similar to Event Driven Microservices architecture (20)

Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)
Rick Hightower
 
Iot cloud service v2.0
Iot cloud service v2.0
Vinod Wilson
 
Microservices in Action
Microservices in Action
Bhagwat Kumar
 
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
VMware Tanzu
 
Concurrency at Scale: Evolution to Micro-Services
Concurrency at Scale: Evolution to Micro-Services
Randy Shoup
 
The Need of Cloud-Native Application
The Need of Cloud-Native Application
Emiliano Pecis
 
Do I Need A Service Mesh.pptx
Do I Need A Service Mesh.pptx
PINGXIONG3
 
Service Mesh Talk for CTO Forum
Service Mesh Talk for CTO Forum
Rick Hightower
 
Microservices session 1
Microservices session 1
Shouri Varanasi
 
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
HostedbyConfluent
 
Redis and Kafka - Advanced Microservices Design Patterns Simplified
Redis and Kafka - Advanced Microservices Design Patterns Simplified
Allen Terleto
 
apidays LIVE Jakarta - Building an Event-Driven Architecture by Harin Honesty...
apidays LIVE Jakarta - Building an Event-Driven Architecture by Harin Honesty...
apidays
 
Patterns of Distributed Application Design
Patterns of Distributed Application Design
GlobalLogic Ukraine
 
Cloud Application architecture styles
Cloud Application architecture styles
Nilay Shrivastava
 
Building data-driven microservices
Building data-driven microservices
Streamlio
 
Microservice intro
Microservice intro
ramesh_sharma
 
Tokyo Azure Meetup #5 - Microservices and Azure Service Fabric
Tokyo Azure Meetup #5 - Microservices and Azure Service Fabric
Tokyo Azure Meetup
 
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
AFAS Software
 
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Tammy Bednar
 
BlockChain-1.pptx
BlockChain-1.pptx
HussainPashaShaik1
 
Service Mesh CTO Forum (Draft 3)
Service Mesh CTO Forum (Draft 3)
Rick Hightower
 
Iot cloud service v2.0
Iot cloud service v2.0
Vinod Wilson
 
Microservices in Action
Microservices in Action
Bhagwat Kumar
 
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
VMware Tanzu
 
Concurrency at Scale: Evolution to Micro-Services
Concurrency at Scale: Evolution to Micro-Services
Randy Shoup
 
The Need of Cloud-Native Application
The Need of Cloud-Native Application
Emiliano Pecis
 
Do I Need A Service Mesh.pptx
Do I Need A Service Mesh.pptx
PINGXIONG3
 
Service Mesh Talk for CTO Forum
Service Mesh Talk for CTO Forum
Rick Hightower
 
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
HostedbyConfluent
 
Redis and Kafka - Advanced Microservices Design Patterns Simplified
Redis and Kafka - Advanced Microservices Design Patterns Simplified
Allen Terleto
 
apidays LIVE Jakarta - Building an Event-Driven Architecture by Harin Honesty...
apidays LIVE Jakarta - Building an Event-Driven Architecture by Harin Honesty...
apidays
 
Patterns of Distributed Application Design
Patterns of Distributed Application Design
GlobalLogic Ukraine
 
Cloud Application architecture styles
Cloud Application architecture styles
Nilay Shrivastava
 
Building data-driven microservices
Building data-driven microservices
Streamlio
 
Tokyo Azure Meetup #5 - Microservices and Azure Service Fabric
Tokyo Azure Meetup #5 - Microservices and Azure Service Fabric
Tokyo Azure Meetup
 
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
AFAS Software
 
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Tammy Bednar
 
Ad

Recently uploaded (20)

ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
IntroSlides-June-GDG-Cloud-Munich community [email protected]
IntroSlides-June-GDG-Cloud-Munich community [email protected]
Luiz Carneiro
 
How to Un-Obsolete Your Legacy Keypad Design
How to Un-Obsolete Your Legacy Keypad Design
Epec Engineered Technologies
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
IPL_Logic_Flow.pdf Mainframe IPLMainframe IPL
KhadijaKhadijaAouadi
 
Machine Learning - Classification Algorithms
Machine Learning - Classification Algorithms
resming1
 
Fundamentals of Digital Design_Class_21st May - Copy.pptx
Fundamentals of Digital Design_Class_21st May - Copy.pptx
drdebarshi1993
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
FUNDAMENTALS OF COMPUTER ORGANIZATION AND ARCHITECTURE
Shabista Imam
 
Industrial internet of things IOT Week-3.pptx
Industrial internet of things IOT Week-3.pptx
KNaveenKumarECE
 
20CE404-Soil Mechanics - Slide Share PPT
20CE404-Soil Mechanics - Slide Share PPT
saravananr808639
 
Stay Safe Women Security Android App Project Report.pdf
Stay Safe Women Security Android App Project Report.pdf
Kamal Acharya
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
International Journal of Advanced Information Technology (IJAIT)
International Journal of Advanced Information Technology (IJAIT)
ijait
 
Complete University of Calculus :: 2nd edition
Complete University of Calculus :: 2nd edition
Shabista Imam
 
System design handwritten notes guidance
System design handwritten notes guidance
Shabista Imam
 
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
Learning – Types of Machine Learning – Supervised Learning – Unsupervised UNI...
23Q95A6706
 
David Boutry - Mentors Junior Developers
David Boutry - Mentors Junior Developers
David Boutry
 
NEW Strengthened Senior High School Gen Math.pptx
NEW Strengthened Senior High School Gen Math.pptx
DaryllWhere
 
Modern multi-proposer consensus implementations
Modern multi-proposer consensus implementations
François Garillot
 
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Abraham Silberschatz-Operating System Concepts (9th,2012.12).pdf
Shabista Imam
 
Ad

Event Driven Microservices architecture

  • 1. Patterns & Practices for Implementing Event-driven Microservices Nikhil Barthwal @nikhilbarthwal [email protected] www.nikhilbarthwal.com
  • 2. Agenda (1/2) • Motivations to move from Monolithic to Microservices • Understanding the Distributed Data in Microservices • Using Domain Driven Design for partitioning data • Problems with Distributed Data • Event Driven Microservices 2
  • 3. Agenda (2/2) • Communication Patterns for Microservices • Event Sourcing • Command Query Responsibility Segregation • Introducing Sagas • Resources for further reading 3
  • 4. Monolithic: The old school approach … • Large code base → Slower development • More time to test & deploy • Too much team interdependency Database Web Service Easy to Develop, Test and Deploy … But doesn’t scale! 4
  • 5. Microservices: The Brave new world ... • Smaller code base to understand & change • Autonomous teams • Faster testing & deployment Assembly of Loosely coupled services! Image Source: https://www.slideshare.net/JontheBeach/distributed-sagas-a-protocol-for-coordinating-microservices 5
  • 6. Loose Coupling → Distributed Data Database per Service! Faster Innovation! Loosely coupled services Reduces team interdependencies Image Source: http://microservices.io/patterns/microservices.html 6
  • 7. How to create service boundaries? An approach to software development for complex needs by connecting the implementation to an evolving model. • Entities • Value Objects • Bounded Context • Aggregates Infrastructure Layer Domain Layer Application Layer User Interface Use Domain Driven Design (DDD)! 7
  • 8. DDD: Aggregates • Cluster of domain objects that can be treated as a single unit • One of its component is root • All outside reference would only go to the aggregate root Domain Model = Collection of Aggregates 8
  • 9. Data Partition Strategies: Use DDD Aggregates provide boundaries for partition 9
  • 10. Problems with Distributed Data • How do we keep data consistent? • How do we query scattered data? 10
  • 11. Cannot use ACID Transactions BEGIN TRANSACTION SELECT ADDRESS FROM CUSTOMERS WHERE CUSTOMER_ID = XXX SELECT PRICE FROM PRODUCTS WHERE CUSTOMER_ID = YYY INSERT INTO ORDERS ... COMMIT TRANSACTION Private to Customer service Private to Product service 11
  • 12. Cannot use 2-Phase Commit • Coordinator is a single point of failure • Very chatty, with O(4n) messages, O(n2) with retries • Locks would decrease throughputs • Impacts availability (CAP theorem limitations) Guarantees consistency, but … 12
  • 13. Consistency Eventual Consistency Availability Partition (Network) Eventual Consistency Use Event Driven Microservice Architecture! 13
  • 14. Event Driven Architecture • Event occurs when a change happens in system • All listeners get notified of the event, may take action • Highly distributed/loosely coupled architecture • Often used for asynchronous flows of information 14
  • 15. Communication: Direct Messaging Benefits: • Easier implementation • Better Error control Drawbacks: • Increased coupling • Difficult to scale Order Service Product Service Customer Service Fulfillment Service Payment Service Shipping Service 15
  • 16. Communication: Via Message broker Benefits: • More scalable • Increased Decoupling Drawbacks: • Requires Broker as Highly Available component Order Service Product Service Customer Service Fulfillment Service Payment Service Shipping Service Message Broker 16
  • 17. Communication Patterns Request/Response (synchronous) Event-Driven (asynchronous) Tier-3: Little to No impact for Customer Tier-2: Impacting Customers indirectly Tier-1: Directly Customer facing 17
  • 18. Event Sourcing: Introduction • Modeling state changes as sequence of events • Storing the event that could trigger the state change • Enables rolling back to particular time in history Examples: 18
  • 19. Event Sourcing: Snapshots Alternative to playing events: Snapshots 19
  • 20. Event Sourcing: Multiple views Image Source: https://www.confluent.io/wp-content/uploads/2016/09/Event-sourced-based-architecture.jpeg Adding applications that process event … but create a different view! 20
  • 21. Event Sourcing: Benefits & Drawbacks Benefits: • 100% accurate audit logging • Easy temporal queries • Applications can process same events but create views Drawbacks: • Adds Complexity • No Strict Consistency • Longer bootup times (Snapshots can help) 21
  • 22. Command Query Responsibility Segregation CQRS Pattern Must for Event Sourcing! Read Write Image Source: https://martinfowler.com/bliki/CQRS.html 22
  • 23. CQRS: Benefits & Drawbacks Benefits: • Needed for Event Sourcing • Improved separation of concerns • Supports scalable multiple denormalized views Drawbacks: • Increased complexity • Potential code duplication • Replication lag as No Strict Consistency 23
  • 24. Sagas: Introduction • Based on a 1987 paper • Initially for a single database running on one node • Now adapted for distributed systems with asynchrony and partial failure 24
  • 25. Transaction & Rollback Transaction • Every Transaction has a Rollback transaction • This logic must be included in the service T1 T2 T3 R1 R2 R3 Transactions → Rollback Transactions → 25
  • 26. Sagas: Example This action initiates the saga Check Credit Service Withdrawal Service Deposit Service Deposit Check Long running transactions … use compensating actions to handle failures! Does Sender have enough money in his account? Withdraw the money (unless it is on hold)? Deposit the money (unless account is deactivated)? 26
  • 27. Resources • Using DDD to develop Microservices: https://www.infoq.com/news/2016/02/ddd- microservices • Overview of Saga Pattern: http://microservices.io/patterns/data/saga.html • Distributed Sagas: https://www.slideshare.net/JontheBeach/distributed-sagas-a- protocol-for-coordinating-microservices 27
  • 28. Summary • Microservices enable faster innovation • DDD can used to define service boundaries • Data is distributed which create challenges • Use Event Driven Architecture for eventual consistency • Understand Event Sourcing & CQRS • Use Sagas to maintain data consistency across services 28