SlideShare a Scribd company logo
1 of 39
Data Management in
Microservice - Part I
Okis Chuang
1
Patterns of Data Management in
Microservices
Databas
e per
Service
Event-
driven
Architectur
e
CQRS
Command
Query
Responsibili
ty
Segregation
From Chris Richardson
2
Database
as a queue
(Application
events)
Event-
sourcing
Mining
Transact
ion Log
Agenda
• Monolith vs. Microservices
• Distributed data management in microservice
• Database per Service pattern
• Event-driven architecture
• Patterns to achieve Atomicity
• Database as a queue
• Transaction log mining
• Event sourcing
3
Monolith vs. Microservices
4
Let’s imagine you’re building a large,
complex application, e.g. an online store
5
Successful software
development
Architecture
Process Organization
Agile
Continuous Delivery
…
Small
Autonomous Team
…
?
6
But successful application
keeps growing…
7
Architecture
Process Organization
Agile
Continuous Delivery
…
Small
Autonomous Team
…
Monolithic
8
Architecture
Process Organization
✅Agile
✅Continuous Delivery
…
✅Small
✅Autonomous Team
…
Microservice
10
Problem
What’s the database architecture in a microservice
application?
11
How to deal with distributed data management problems?
Forces
• Services must be loosely coupled so that they
can be developed, deployed, and scaled
independently.
12
Patterns of Data Management in
Microservices
Event-
driven
Architectur
e
CQRS
Command
Query
Responsibili
ty
Segregation
From Chris Richardson
13
Database
as a queue
(Application
events)
Event-
sourcing
Mining
Transact
ion Log
Databas
e per
Service
Decentralized Data Management
Pattern: Keep a service’s persistent data private
14
Atomicity
Consistency
Isolation
Durability
Shared Database
Database per
Service
Database Architecture
15
Shared Database
Order Service Customer Service …Service
Order Table
order_total
Customer Table
credit_limit
…
…
Tight Coupling😭 Simple and ACID ✌️
16
Database per Service
Order Service Customer Service
Order Table
order_total
Customer Table
credit_limit
Loosely coupling 🤘🤘 but more complex 😧
17
DB Architecture in a
microservice application
• Microservice-based applications often use mixture of
SQL/NoSQL databases, which called polyglot
persistence
• Database per Service (Keep a service’s persistent data
private)
• Private-tables-per-service
• Schema-per-service
• Database-server-per-service
19
How we guarantee data
consistency in microservice?
20
21
Use 2PC algorithm?
22
Use 2 Phase Commit to
cope (a.k.a distributed
transaction) ?
24
25
Coordinator Coordinator
Cohort Cohort
2PC is not an option
• It’s a BLOCKING protocol
• According to CAP theorem, you need to choose
between availability and ACID-style consistency
• Availability is usually a better choice
• Moreover, most NoSQL databases do not support
2PC
• BUT maintaining data consistency across services and
databases is essential
26
Patterns of Data Management in
Microservices
Databas
e per
Service
CQRS
Command
Query
Responsibili
ty
Segregation
From Chris Richardson
27
Database
as a queue
(Application
events)
Event-
sourcing
Mining
Transact
ion Log
Event-
driven
Architectu
re
28
29
30
The Architecture provides
that
• Each service atomically updates its database and
publish events
• The message broker guarantees that events are
delivered at least once then you can implement
business transactions that span multiple services
• trading some consistency for availability can lead to
dramatic improvements in scalability (1)
• Eventual consistency instead of Strong
consistency
31
use events to maintain materialized views
that pre-join data owned by multiple
microservices.
could be NoSQL like
document-based
MongoDB
We’re not done yet…
What if Order service crashes
after updating the database
but before publishing the event
We might be loosing Atomicity
(Atomically update state and publish events)
lution to transactions that span multiple services and provide eve
Enables an application to maintain materialized views
ogramming model is more complex than when using ACID transac
plement compensating transaction to recover application-level fail
Materialized view is not up to date
Duplicate event should be detect
Patterns of Data Management in
Microservices
Databas
e per
Service
Event-
driven
Architectur
e
CQRS
Command
Query
Responsibili
ty
Segregation
From Chris Richardson
35
Event-
sourcing
Mining
Transact
ion Log
Database
as a queue
(Applicatio
n events)
Publishing Events Using Local Transactions
1. Create EVENT table for storing state of business entities
2. Local transaction for business logic and EVENT table
3. A separate application thread or process queries the EVENT table,
publish events to Message Broker, then use local transaction to mark
the events as published
Patterns of Data Management in
Microservices
Databas
e per
Service
Event-
driven
Architectur
e
CQRS
Command
Query
Responsibili
ty
Segregation
From Chris Richardson
37
Database
as a queue
(Application
events)
Event-
sourcing
Mining
Transac
tion Log
Mining Database transaction Log
LinkedIn opensource
Stream mechanism in AWS DynamoDB
Patterns of Data Management in
Microservices
Databas
e per
Service
Event-
driven
Architectur
e
CQRS
Command
Query
Responsibili
ty
Segregation
From Chris Richardson
39
Database
as a queue
(Application
events)
Mining
Transact
ion Log
Event-
sourcin
g
• Different and unfamiliar style of programming
• The event store only directly supports the lookup of
business entities by primary key
• Should use CQRS to implement query
Summary
• In microservices, each service has its own private
datastore which might be different SQL/NoSQL
databases
• While the database architecture has significant benefits,
it also derive distributed data management challenge
• How to implement business transaction that maintain
consistency across multiple DBs
• If adopting Event-driven, how to atomically update
state and publish event

More Related Content

What's hot

Microservice and Service Fabric talk
Microservice and Service Fabric talkMicroservice and Service Fabric talk
Microservice and Service Fabric talkDaniel Kreuzhofer
 
Building a Global Multi-Tenant Monitoring Platform
Building a Global Multi-Tenant Monitoring PlatformBuilding a Global Multi-Tenant Monitoring Platform
Building a Global Multi-Tenant Monitoring PlatformAmazon Web Services
 
Staying Ahead of New Availability Requirements with the hybrid Cloud.
Staying Ahead of New Availability Requirements with the hybrid Cloud.Staying Ahead of New Availability Requirements with the hybrid Cloud.
Staying Ahead of New Availability Requirements with the hybrid Cloud.Tanawit Chansuchai
 
Getting the most from Windows Virtual Desktop in Azure
Getting the most from Windows Virtual Desktop in AzureGetting the most from Windows Virtual Desktop in Azure
Getting the most from Windows Virtual Desktop in AzureRichard Conway
 
Managing your virtual environment with System Center & Windows Server 2012
Managing your virtual environment with System Center & Windows Server 2012Managing your virtual environment with System Center & Windows Server 2012
Managing your virtual environment with System Center & Windows Server 2012C/D/H Technology Consultants
 
Toni bernal citrix day 2012 overview v2
Toni bernal citrix day 2012 overview v2Toni bernal citrix day 2012 overview v2
Toni bernal citrix day 2012 overview v2Digicomp Academy AG
 
Microsoft Operations Management Suite
Microsoft Operations Management Suite Microsoft Operations Management Suite
Microsoft Operations Management Suite Engin Özkurt
 
Building Multi-tenant, Configurable, High Quality Applications on .NET for an...
Building Multi-tenant, Configurable, High Quality Applications on .NET for an...Building Multi-tenant, Configurable, High Quality Applications on .NET for an...
Building Multi-tenant, Configurable, High Quality Applications on .NET for an...Techcello
 
Transforming the Data Center
Transforming the Data CenterTransforming the Data Center
Transforming the Data CenterSynergy Technical
 
SDE Solution Overview v1.5
SDE Solution Overview v1.5SDE Solution Overview v1.5
SDE Solution Overview v1.5Joe Kaluza
 
How Cloud Hosted Desktop Differs From Remote Desktop Services?
How Cloud Hosted Desktop Differs From Remote Desktop Services?How Cloud Hosted Desktop Differs From Remote Desktop Services?
How Cloud Hosted Desktop Differs From Remote Desktop Services?Yuvraj Jain
 
vRealize Network Insight 3.3
vRealize Network Insight 3.3vRealize Network Insight 3.3
vRealize Network Insight 3.3VMware
 
Citrix Day 2013: CloudPlatform & Cloud Portal Business Manager
Citrix Day 2013: CloudPlatform & Cloud Portal Business ManagerCitrix Day 2013: CloudPlatform & Cloud Portal Business Manager
Citrix Day 2013: CloudPlatform & Cloud Portal Business ManagerDigicomp Academy AG
 
vRealize Operations (vROps) Management Pack for Microsoft SQL Server Overview
vRealize Operations (vROps) Management Pack for Microsoft SQL Server OverviewvRealize Operations (vROps) Management Pack for Microsoft SQL Server Overview
vRealize Operations (vROps) Management Pack for Microsoft SQL Server OverviewBlue Medora
 
2016, A new era of OS and Cloud Security
2016, A new era of OS and Cloud Security2016, A new era of OS and Cloud Security
2016, A new era of OS and Cloud SecurityTudor Damian
 
How Docker and ASP.NET Core will change the life of a Microsoft developer
How Docker and ASP.NET Core will change the life of a Microsoft developerHow Docker and ASP.NET Core will change the life of a Microsoft developer
How Docker and ASP.NET Core will change the life of a Microsoft developerAlex Thissen
 
Deep dive into service fabric after 2 years
Deep dive into service fabric after 2 yearsDeep dive into service fabric after 2 years
Deep dive into service fabric after 2 yearsTomasz Kopacz
 
Windows Virtual Desktop Customer benefits
Windows Virtual Desktop Customer benefitsWindows Virtual Desktop Customer benefits
Windows Virtual Desktop Customer benefitsPrime Infoserv
 

What's hot (20)

OMS Overview
OMS OverviewOMS Overview
OMS Overview
 
Microservice and Service Fabric talk
Microservice and Service Fabric talkMicroservice and Service Fabric talk
Microservice and Service Fabric talk
 
Building a Global Multi-Tenant Monitoring Platform
Building a Global Multi-Tenant Monitoring PlatformBuilding a Global Multi-Tenant Monitoring Platform
Building a Global Multi-Tenant Monitoring Platform
 
Staying Ahead of New Availability Requirements with the hybrid Cloud.
Staying Ahead of New Availability Requirements with the hybrid Cloud.Staying Ahead of New Availability Requirements with the hybrid Cloud.
Staying Ahead of New Availability Requirements with the hybrid Cloud.
 
Getting the most from Windows Virtual Desktop in Azure
Getting the most from Windows Virtual Desktop in AzureGetting the most from Windows Virtual Desktop in Azure
Getting the most from Windows Virtual Desktop in Azure
 
Managing your virtual environment with System Center & Windows Server 2012
Managing your virtual environment with System Center & Windows Server 2012Managing your virtual environment with System Center & Windows Server 2012
Managing your virtual environment with System Center & Windows Server 2012
 
Toni bernal citrix day 2012 overview v2
Toni bernal citrix day 2012 overview v2Toni bernal citrix day 2012 overview v2
Toni bernal citrix day 2012 overview v2
 
Microsoft Operations Management Suite
Microsoft Operations Management Suite Microsoft Operations Management Suite
Microsoft Operations Management Suite
 
Building Multi-tenant, Configurable, High Quality Applications on .NET for an...
Building Multi-tenant, Configurable, High Quality Applications on .NET for an...Building Multi-tenant, Configurable, High Quality Applications on .NET for an...
Building Multi-tenant, Configurable, High Quality Applications on .NET for an...
 
Transforming the Data Center
Transforming the Data CenterTransforming the Data Center
Transforming the Data Center
 
SDE Solution Overview v1.5
SDE Solution Overview v1.5SDE Solution Overview v1.5
SDE Solution Overview v1.5
 
How Cloud Hosted Desktop Differs From Remote Desktop Services?
How Cloud Hosted Desktop Differs From Remote Desktop Services?How Cloud Hosted Desktop Differs From Remote Desktop Services?
How Cloud Hosted Desktop Differs From Remote Desktop Services?
 
vRealize Network Insight 3.3
vRealize Network Insight 3.3vRealize Network Insight 3.3
vRealize Network Insight 3.3
 
Citrix Day 2013: CloudPlatform & Cloud Portal Business Manager
Citrix Day 2013: CloudPlatform & Cloud Portal Business ManagerCitrix Day 2013: CloudPlatform & Cloud Portal Business Manager
Citrix Day 2013: CloudPlatform & Cloud Portal Business Manager
 
vRealize Operations (vROps) Management Pack for Microsoft SQL Server Overview
vRealize Operations (vROps) Management Pack for Microsoft SQL Server OverviewvRealize Operations (vROps) Management Pack for Microsoft SQL Server Overview
vRealize Operations (vROps) Management Pack for Microsoft SQL Server Overview
 
2016, A new era of OS and Cloud Security
2016, A new era of OS and Cloud Security2016, A new era of OS and Cloud Security
2016, A new era of OS and Cloud Security
 
How Docker and ASP.NET Core will change the life of a Microsoft developer
How Docker and ASP.NET Core will change the life of a Microsoft developerHow Docker and ASP.NET Core will change the life of a Microsoft developer
How Docker and ASP.NET Core will change the life of a Microsoft developer
 
Deep dive into service fabric after 2 years
Deep dive into service fabric after 2 yearsDeep dive into service fabric after 2 years
Deep dive into service fabric after 2 years
 
Azure Administrator
Azure AdministratorAzure Administrator
Azure Administrator
 
Windows Virtual Desktop Customer benefits
Windows Virtual Desktop Customer benefitsWindows Virtual Desktop Customer benefits
Windows Virtual Desktop Customer benefits
 

Similar to Microservice - Data Management

Microservices Patterns with GoldenGate
Microservices Patterns with GoldenGateMicroservices Patterns with GoldenGate
Microservices Patterns with GoldenGateJeffrey T. Pollock
 
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
Get the Message Across: Seamlessly Transport Data to Apps, AnywhereGet the Message Across: Seamlessly Transport Data to Apps, Anywhere
Get the Message Across: Seamlessly Transport Data to Apps, AnywhereVMware Tanzu
 
Microservices using .Net core
Microservices using .Net coreMicroservices using .Net core
Microservices using .Net coregirish goudar
 
Gartner 2017 London: How to re-invent your IT Architecture?
Gartner 2017 London: How to re-invent your IT Architecture?Gartner 2017 London: How to re-invent your IT Architecture?
Gartner 2017 London: How to re-invent your IT Architecture?LeanIX GmbH
 
Event Driven Microservices architecture
Event Driven Microservices architectureEvent Driven Microservices architecture
Event Driven Microservices architectureNikhilBarthwal4
 
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...Tammy Bednar
 
How to Overcome Data Challenges When Refactoring Monoliths to Microservices
How to Overcome Data Challenges When Refactoring Monoliths to MicroservicesHow to Overcome Data Challenges When Refactoring Monoliths to Microservices
How to Overcome Data Challenges When Refactoring Monoliths to MicroservicesVMware Tanzu
 
Event driven architecure
Event driven architecureEvent driven architecure
Event driven architecureTouraj Ebrahimi
 
Discover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS Summit
Discover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS SummitDiscover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS Summit
Discover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS SummitAmazon Web Services
 
Application Architecture Summit - Monitoring the Dynamic Cloud
Application Architecture Summit - Monitoring the Dynamic Cloud Application Architecture Summit - Monitoring the Dynamic Cloud
Application Architecture Summit - Monitoring the Dynamic Cloud New Relic
 
Stateful on Stateless - The Future of Applications in the Cloud
Stateful on Stateless - The Future of Applications in the CloudStateful on Stateless - The Future of Applications in the Cloud
Stateful on Stateless - The Future of Applications in the CloudMarkus Eisele
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservicesAnil Allewar
 
Service Fabric – building tomorrows applications today
Service Fabric – building tomorrows applications todayService Fabric – building tomorrows applications today
Service Fabric – building tomorrows applications todayBizTalk360
 
Iot cloud service v2.0
Iot cloud service v2.0Iot cloud service v2.0
Iot cloud service v2.0Vinod Wilson
 
Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...
Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...
Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...Amazon Web Services
 
Redis and Kafka - Advanced Microservices Design Patterns Simplified
Redis and Kafka - Advanced Microservices Design Patterns SimplifiedRedis and Kafka - Advanced Microservices Design Patterns Simplified
Redis and Kafka - Advanced Microservices Design Patterns SimplifiedAllen Terleto
 
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...HostedbyConfluent
 
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...AFAS Software
 

Similar to Microservice - Data Management (20)

Microservices Patterns with GoldenGate
Microservices Patterns with GoldenGateMicroservices Patterns with GoldenGate
Microservices Patterns with GoldenGate
 
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
Get the Message Across: Seamlessly Transport Data to Apps, AnywhereGet the Message Across: Seamlessly Transport Data to Apps, Anywhere
Get the Message Across: Seamlessly Transport Data to Apps, Anywhere
 
Microservices using .Net core
Microservices using .Net coreMicroservices using .Net core
Microservices using .Net core
 
Gartner 2017 London: How to re-invent your IT Architecture?
Gartner 2017 London: How to re-invent your IT Architecture?Gartner 2017 London: How to re-invent your IT Architecture?
Gartner 2017 London: How to re-invent your IT Architecture?
 
Event Driven Microservices architecture
Event Driven Microservices architectureEvent Driven Microservices architecture
Event Driven Microservices architecture
 
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
Database@Home : Data Driven Apps - Data-driven Microservices Architecture wit...
 
How to Overcome Data Challenges When Refactoring Monoliths to Microservices
How to Overcome Data Challenges When Refactoring Monoliths to MicroservicesHow to Overcome Data Challenges When Refactoring Monoliths to Microservices
How to Overcome Data Challenges When Refactoring Monoliths to Microservices
 
Event driven architecure
Event driven architecureEvent driven architecure
Event driven architecure
 
Discover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS Summit
Discover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS SummitDiscover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS Summit
Discover MongoDB Atlas and MongoDB Stitch - DEM02-S - Mexico City AWS Summit
 
Application Architecture Summit - Monitoring the Dynamic Cloud
Application Architecture Summit - Monitoring the Dynamic Cloud Application Architecture Summit - Monitoring the Dynamic Cloud
Application Architecture Summit - Monitoring the Dynamic Cloud
 
Microservices Architecture
Microservices ArchitectureMicroservices Architecture
Microservices Architecture
 
Stateful on Stateless - The Future of Applications in the Cloud
Stateful on Stateless - The Future of Applications in the CloudStateful on Stateless - The Future of Applications in the Cloud
Stateful on Stateless - The Future of Applications in the Cloud
 
Introduction to microservices
Introduction to microservicesIntroduction to microservices
Introduction to microservices
 
Service Fabric – building tomorrows applications today
Service Fabric – building tomorrows applications todayService Fabric – building tomorrows applications today
Service Fabric – building tomorrows applications today
 
Iot cloud service v2.0
Iot cloud service v2.0Iot cloud service v2.0
Iot cloud service v2.0
 
Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...
Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...
Power Big Data Analytics with Informatica Cloud Integration for Redshift, Kin...
 
Redis and Kafka - Advanced Microservices Design Patterns Simplified
Redis and Kafka - Advanced Microservices Design Patterns SimplifiedRedis and Kafka - Advanced Microservices Design Patterns Simplified
Redis and Kafka - Advanced Microservices Design Patterns Simplified
 
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
Redis and Kafka - Simplifying Advanced Design Patterns within Microservices A...
 
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
Alex Thissen (Xpirit) - Een verschuiving in architectuur: op weg naar microse...
 
Microservices Corporate Style
Microservices Corporate StyleMicroservices Corporate Style
Microservices Corporate Style
 

More from Okis Chuang

Make Your Own IoT Device by ZigBee
Make Your Own IoT Device by ZigBeeMake Your Own IoT Device by ZigBee
Make Your Own IoT Device by ZigBeeOkis Chuang
 
Introduction to HTTP/2
Introduction to HTTP/2Introduction to HTTP/2
Introduction to HTTP/2Okis Chuang
 
Let's Talk About Serverless - Focusing on AWS Lambda
Let's Talk About Serverless - Focusing on AWS LambdaLet's Talk About Serverless - Focusing on AWS Lambda
Let's Talk About Serverless - Focusing on AWS LambdaOkis Chuang
 
Neural Network Basics
Neural Network BasicsNeural Network Basics
Neural Network BasicsOkis Chuang
 
Oh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes BasicsOh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes BasicsOkis Chuang
 
Experiences sharing about Lambda, Kinesis, and Postgresql
Experiences sharing about Lambda, Kinesis, and PostgresqlExperiences sharing about Lambda, Kinesis, and Postgresql
Experiences sharing about Lambda, Kinesis, and PostgresqlOkis Chuang
 

More from Okis Chuang (6)

Make Your Own IoT Device by ZigBee
Make Your Own IoT Device by ZigBeeMake Your Own IoT Device by ZigBee
Make Your Own IoT Device by ZigBee
 
Introduction to HTTP/2
Introduction to HTTP/2Introduction to HTTP/2
Introduction to HTTP/2
 
Let's Talk About Serverless - Focusing on AWS Lambda
Let's Talk About Serverless - Focusing on AWS LambdaLet's Talk About Serverless - Focusing on AWS Lambda
Let's Talk About Serverless - Focusing on AWS Lambda
 
Neural Network Basics
Neural Network BasicsNeural Network Basics
Neural Network Basics
 
Oh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes BasicsOh K8s Is Swag - Kubernetes Basics
Oh K8s Is Swag - Kubernetes Basics
 
Experiences sharing about Lambda, Kinesis, and Postgresql
Experiences sharing about Lambda, Kinesis, and PostgresqlExperiences sharing about Lambda, Kinesis, and Postgresql
Experiences sharing about Lambda, Kinesis, and Postgresql
 

Recently uploaded

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 

Recently uploaded (20)

MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 

Microservice - Data Management

  • 1. Data Management in Microservice - Part I Okis Chuang 1
  • 2. Patterns of Data Management in Microservices Databas e per Service Event- driven Architectur e CQRS Command Query Responsibili ty Segregation From Chris Richardson 2 Database as a queue (Application events) Event- sourcing Mining Transact ion Log
  • 3. Agenda • Monolith vs. Microservices • Distributed data management in microservice • Database per Service pattern • Event-driven architecture • Patterns to achieve Atomicity • Database as a queue • Transaction log mining • Event sourcing 3
  • 5. Let’s imagine you’re building a large, complex application, e.g. an online store 5
  • 9.
  • 11. Problem What’s the database architecture in a microservice application? 11 How to deal with distributed data management problems?
  • 12. Forces • Services must be loosely coupled so that they can be developed, deployed, and scaled independently. 12
  • 13. Patterns of Data Management in Microservices Event- driven Architectur e CQRS Command Query Responsibili ty Segregation From Chris Richardson 13 Database as a queue (Application events) Event- sourcing Mining Transact ion Log Databas e per Service
  • 14. Decentralized Data Management Pattern: Keep a service’s persistent data private 14 Atomicity Consistency Isolation Durability
  • 16. Shared Database Order Service Customer Service …Service Order Table order_total Customer Table credit_limit … … Tight Coupling😭 Simple and ACID ✌️ 16
  • 17. Database per Service Order Service Customer Service Order Table order_total Customer Table credit_limit Loosely coupling 🤘🤘 but more complex 😧 17
  • 18. DB Architecture in a microservice application • Microservice-based applications often use mixture of SQL/NoSQL databases, which called polyglot persistence • Database per Service (Keep a service’s persistent data private) • Private-tables-per-service • Schema-per-service • Database-server-per-service 19
  • 19. How we guarantee data consistency in microservice? 20
  • 20. 21
  • 22. Use 2 Phase Commit to cope (a.k.a distributed transaction) ? 24
  • 24. 2PC is not an option • It’s a BLOCKING protocol • According to CAP theorem, you need to choose between availability and ACID-style consistency • Availability is usually a better choice • Moreover, most NoSQL databases do not support 2PC • BUT maintaining data consistency across services and databases is essential 26
  • 25. Patterns of Data Management in Microservices Databas e per Service CQRS Command Query Responsibili ty Segregation From Chris Richardson 27 Database as a queue (Application events) Event- sourcing Mining Transact ion Log Event- driven Architectu re
  • 26. 28
  • 27. 29
  • 28. 30
  • 29. The Architecture provides that • Each service atomically updates its database and publish events • The message broker guarantees that events are delivered at least once then you can implement business transactions that span multiple services • trading some consistency for availability can lead to dramatic improvements in scalability (1) • Eventual consistency instead of Strong consistency 31
  • 30. use events to maintain materialized views that pre-join data owned by multiple microservices. could be NoSQL like document-based MongoDB
  • 31. We’re not done yet… What if Order service crashes after updating the database but before publishing the event We might be loosing Atomicity (Atomically update state and publish events)
  • 32. lution to transactions that span multiple services and provide eve Enables an application to maintain materialized views ogramming model is more complex than when using ACID transac plement compensating transaction to recover application-level fail Materialized view is not up to date Duplicate event should be detect
  • 33. Patterns of Data Management in Microservices Databas e per Service Event- driven Architectur e CQRS Command Query Responsibili ty Segregation From Chris Richardson 35 Event- sourcing Mining Transact ion Log Database as a queue (Applicatio n events)
  • 34. Publishing Events Using Local Transactions 1. Create EVENT table for storing state of business entities 2. Local transaction for business logic and EVENT table 3. A separate application thread or process queries the EVENT table, publish events to Message Broker, then use local transaction to mark the events as published
  • 35. Patterns of Data Management in Microservices Databas e per Service Event- driven Architectur e CQRS Command Query Responsibili ty Segregation From Chris Richardson 37 Database as a queue (Application events) Event- sourcing Mining Transac tion Log
  • 36. Mining Database transaction Log LinkedIn opensource Stream mechanism in AWS DynamoDB
  • 37. Patterns of Data Management in Microservices Databas e per Service Event- driven Architectur e CQRS Command Query Responsibili ty Segregation From Chris Richardson 39 Database as a queue (Application events) Mining Transact ion Log Event- sourcin g
  • 38. • Different and unfamiliar style of programming • The event store only directly supports the lookup of business entities by primary key • Should use CQRS to implement query
  • 39. Summary • In microservices, each service has its own private datastore which might be different SQL/NoSQL databases • While the database architecture has significant benefits, it also derive distributed data management challenge • How to implement business transaction that maintain consistency across multiple DBs • If adopting Event-driven, how to atomically update state and publish event

Editor's Notes

  1. 因為這個主題的東西實在涉足相當廣泛,今天我只就 data management 在 microservice中如何實作的部分詳細解說,不會對於microservice本身下太多結論
  2. 這張圖是我整理出來在 microservice architecture 裡,與data management相關的 pattern 除了 CQRS 以外 其他我會一一說明
  3. 今天的topic很單純,雖然microservice涉及的東西很廣泛,但我今天只想講一件事情:Distributed data management 怎麼做的,尤其是在 microservice 中常見的 pattern 為何
  4. 一般來說以一個backend常見的架構會是左邊的 monolith。把所有service都集結在一起,無論是develop, deploy, scaling 都是綁在一起的 而當然microservice強調的是每個獨立的service可以獨立做到剛剛講的那些事情
  5. From now on, I’ll use simple online store as an example.
  6. 一個成功的軟體開發可以切成三塊:流程 組織 跟架構 假設我們定義成功的軟體開發要件為:流程要 Agile, 要能實現 continuous delivery等條件;組織要小而快,要能自動自發自我管理且獨立做決策 這些都是成功的要素,那成功的軟體開發在架構上會是什麼樣子呢? 這邊會 focus 在 data management 的部分解說
  7. 成功的application是會成長的,就像 diamond 會愈賣愈多
  8. 隨著application成長且愈來愈複雜 這時候若維持 monolithic 架構 可能會開始失去一些成功因素
  9. 在系統的複雜度較低時,採用microservice後的生產力比使用monolith還要低 一但複雜度到達一定程度後,monolith生產力急速下降,而 microservice 因為降低了系統的耦合度,因此可有效維持一定的生產力
  10. 隨著application成長且愈來愈複雜 這時候若維持 monolithic 架構 可能會開始失去一些成功因素
  11. 如果我們採用了 microservice,在 microservice 的世界裡,什麼是db architecture 的 best practice 呢?
  12. 有一些限制我們必須先講清楚:在micro service的世界裡,每個 service 都要能夠被獨立地開發、部署與擴充
  13. 首先是 Database Per Service
  14. 左邊是常見的 monolith 系統,所有business logic都寫在同一個service裡面,並且共用一個 database,且常見為RDBMS,因此可以輕易滿足ACID的特性,也就是transaction。 但是在microservice的世界裡,由於強調必須service間的獨立性,有一個pattern是keep a service’s persistent data private!! 因此每個service都對應不同的db,相對於monolith,這是很不一樣的架構,那在這前提下要如何做到 ACID 呢? ACID Atomicity – Changes are made atomically 對data的變更要嘛全做要嘛全不做 Consistency – 必須保證資料The state of the database is always consistent Isolation – Even though transactions are executed concurrently it appears they are executed serially Durability – Once a transaction has committed it is not undone
  15. 左邊是 monolith 使用的 database architecture 右邊是 microservice
  16. 假設online store service分別有order, customer services, 在 shared db 架構下要達到 Transaction safe 相當容易,只要使用 db 支援的 transaction 機制便可達到ACID。但是不同service之前的邏輯是緊緊綁在一起的
  17. 以每個service操作不同的db來看,彼此的logic不再綁在一起,符合 microservice的精神 但是你可以想像得到無論在application layer還是 data layer 要處理的事情肯定變複雜了
  18. 在microservice的應用中,不同應用時常使用不同類型的 data store,graph 相關的可以用 neo4j, time series 相關的可以用Riak-TS, influxDB 因此在此架構下,應用並不會限定於某種特定DB,實作上也有更多的彈性 • Private-tables-per-service – each service owns a set of tables that must only be accessed by that service • Schema-per-service – each service has a database schema that’s private to that service • Database-server-per-service – each service has it’s own database server. Private-tables-per-service and schema-per-service have the lowest overhead. Using a schema per service is appealing since it makes ownership clearer. Some high throughput services might need their own database server.
  19. 這是在 microservice裡面最難的問題之一:實作跨多個DB的 business transaction 的複雜度很高
  20. Order service 無法直接 access Customer table,只能透過 Customer API 存取相關資訊,因此假設有一張新的訂單要產生,但是需要確認此訂單的客戶 credit limit 是否超過,在無法使用 local ACID transaction 的前提下,要如何確保 data consistency呢?換句話說,若是有多個訂單在同時 update credit limit 的話,就會出現 inconsistent 的情況
  21. PHASE 1 coordinator送出 query to commit 給所有 cohort 並等待所有人回應 cohort 開始準備執行 transaction相關動作:lock resource, reserve resource, 寫log 每個 cohort 回應 coordinator,準備成功 vote YES, or vote NO PHASE 2: all YES -> coordinator send “commit” 指令; cohort commit 並且回應完成 not all YES -> send “rollback”; cohort release resources 並且回應完成 coordinator commit global transaction postgresql: PREPARE TRANSACTION https://www.postgresql.org/docs/current/static/sql-prepare-transaction.html
  22. 如果不使用2PC的演算法去解決data consistency,要怎麼解決呢?
  23. 在 event-driven 架構中,會多一個 message broker,而他是實現 data consistency的關鍵角色 order 收到指令後 insert Order table並且將此order state標註為pending,接著 publish “OrderCreated” event,
  24. 因為 Customer service 有subscribe 相關 event topic,所以會收到event 接著 Customer會對 customer table 做相關 update,並publish “CreditReserved” event 給 broker
  25. Order service 收到後會 update order state 為 open 完成此流程
  26. Weak (弱一致性):當你寫入一個新值後,讀操作在數據副本上可能讀出來,也可能讀不出來。比如:某些 cache 系統。 Eventually (最終一致性):當你寫入一個新值後,有可能讀不出來,但在某個時間窗口之後保證最終能讀出來。比如: DNS ,電子郵件、 Amazon S3 , Google 搜索引擎這樣的系統。 Strong (強一致性):新的數據一旦寫入,在任意副本任意時刻都能讀到新值。比如:文件系統, RDBMS , Azure Table 都是強一致性的。
  27. 還可以利用 event 實作一個for customer&order 查詢的 service 這個 service 會 query 一個利用order/customer event 所 join 出來且不斷更新 materialized view
  28. update service entity state 跟 publish event 沒有維持 atomic
  29. 當 credit check fail 時可能需要 cancel order 等application需要自行處理inconsistent data 或是materialized view的資料是不同步的 甚至必須detect or ignore duplication event -> idempotent receiver
  30. create 一個專門儲存entity state的EVENT table 在local db application在實作時必須同時對business logic操作及 EVENT table 操作包local transaction, 此時的event state會是 NEW 的狀態 獨立一隻process或thread去 polling EVENT table 內的 state list 並且以 transaction 的方式把對應 EVENT mark 成 published 如此一來即便是order service掛掉了,create order 跟 publish event 都會保證是一起進行
  31. Order service update ORDER table 然後另外起一個 Transaction log miner process 不斷 tail log 來產生 event 問題:要如何知道process到哪?不同DB的log format不一樣 DB版本不同可能也不同
  32. 原本order service是存著 order current state, 完全不同的是,改為存 order 一系列的 state-changing event 然後 application 再透過 replay 這些 event state 去重建 order 的 current state 這完全是不同的設計方式,此時 application 只需要煩惱將 event 存進 event store 而 event store 在這裡扮演的角色有點像是前面提到的 msg broker負責event的pub/sub,並且提供add / get event 的API 可以想像一但採用了event-sourcing 的架構後,實作的方式會大不相同,原本下 query 的方式也會改變,複雜度大大提升