Cloud Native Application과 Microservice과 관련된 주제를 꾸준히 본 밋업에서 다루고 있는데요, 이번에는 Microservice 구현 패턴 중 독립성 확보와 확장성 관점에서 클라우드 시대에 적절한 모델인 CQRS와 Event Sourcing에 대해서 설명하고, 단계별 샘플 소스를 통해서 구현체의 모습과 메커니즘을 알아봅니다.
7. User Interface
Application
Datastore
Infrastructure
Resulting SoftwareTypical Enterprise Organization Structure
Head of IT
Head of
Operation
Head of DBAs
Head of
Infrastructure
Head of App
Dev
Head of UI
Head of
Development
An Enormous Monolith
Conway’s Law: Software reflects the structure of the
organization that produced it
8. Build small product-focused teams – strict one team
to one microservice mapping
Many Small Microservices
Resulting SoftwareMicroservices Organization Structure
API
Application
Datastore
Infrastructure
API
Application
Datastore
Infrastructure
API
Application
Datastore
Infrastructure
API
Application
Datastore
Infrastructure
Product Lead
Project Manager Sys Admin DBA
JavaScript
Developer
Developer
Developer
Sys Admin
Storage Admin
Graphic ArtistNoSQL Admin
Product Lead
Project Manager Sys Admin DBA
JavaScript
Developer
Developer
Developer
Sys Admin
Storage Admin
Graphic ArtistNoSQL Admin
Product Lead
Project Manager Sys Admin DBA
JavaScript
Developer
Developer
Developer
Sys Admin
Storage Admin
Graphic ArtistNoSQL Admin
Product Lead
Project Manager Sys Admin DBA
JavaScript
Developer
Developer
Developer
Sys Admin
Storage Admin
Graphic ArtistNoSQL Admin
13. Online Shopping Mall
Order Customer
ID CUSTOMER_ID STATUS TOTAL ID CREDIT_LIMIT …….
Order Table Customer Table
14. BEGIN TRANSACTION
…
SELECT ORDER_TOTAL FROM ORDERS WHERE CUSTOMER_ID = ?
…
SELECT CREDIT_LIMIT FROM CUSTOMERS WHERE CUSTOMER_ID = ?
…
INSERT INTO ORDERS …
…
COMMIT TRANSACTION
Traditional
First Challenge - ACID
User Interface
Application
Datastore
Infrastructure
One Application
Transaction
15. Consistency across multiple services
Order Customer
API
Application
Datastore
Infrastructure
Order
Microservice
API
Application
Datastore
Infrastructure
Customer
Microservice
First Challenge - ACID
17. Rules of Distributed Computing
Consistency
Each node shows the same data at all times
Availability
Each node is available for writes at all times
Partition Tolerance
Able to handle network outages
CAP Theorem
C
A P
Theory Practice
Pick One
Partition Tolerance is non-negotiable because
we have networks that can always fail
Enterprise IT Systems: Often CP
Microservice Systems: Often AP
Each microservice can be CP, AP or CA but the
system as a whole is always CP or AP
Pick
Any
Two
18. First Challenge - BASE
Basic Availability
• The database appears to work most of the time.
Soft-state
• Stores don’t have to be write-consistent, nor do different replicas have to
be mutually consistent all the time.
Eventual consistency
• Stores exhibit consistency at some later point (e.g., lazily at read time).
19. Traditional
Second Challenge - Query
User Interface
Application
Datastore
Infrastructure
One Application
SELECT *
FROM CUSTOMER c, ORDER o
WHERE
c.id = o.ID
AND o.ORDER_TOTAL > 100000
AND o.STATE = 'SHIPPED'
AND c.CREATION_DATE > ?
Join Query
20. Event Driven Architecture
Order
Customer
Message
Broker
(Pub/Sub)
Order
Created Event
Order
Created Event
Credit
Reserved
ID CUSTOMER_ID STATUS TOTAL
900 202 NEW 1000
ID CREDIT_LIMIT …….
202 5,000,000
Order Table
Customer Table
CUST_ID ORDER_ID AMOUNT
202 900 1000
Reserved_Credit Table
Credit
Reserved
1
2
3
4
Credit Limit
Exceeded
APPROVED
CANCELLED
Eventual Consistency
21. Order
Service
Customer
Service
Event Driven Architecture
Message Broker
Customer Order
View Query Service
Customer Created
Customer Cancelled
Customer Shipped
……
Order Created
Order Cancelled
Order Shipped
…..
Customer
Order
View
Update
Find
Customer & Order Query
23. ACCOUNT_ID ACCOUNT_TYPE BALANCE
12345 A 5,000,000
23456 B 250,000
34567 C 3,500,000
… …. ….
Account Table
SEQ ACCOUNT_ID … UPDATE_DATE
1 12345 .. ….
2 23456 … …
3 34567 … …
4
5
Account History Table
입금
출금
최종 시점의 상태 정보만을 보관
잔고 정보가 맞지 않는 경우?
원인 파악은???
24. Event Sourcing = 데이터 저장의 새로운 Pattern
Order
event #1 @ T1
event #2 @ T2
event #3 @ T3
event #4 T4
“Capture all changes to an application
state as a sequence of events”
”어플리케이션의 모든 상태 변화를
발생순서에 따라 이벤트로 보관한다.
30. 장점
Event Driven Architecture를 구현하면서 Atomic한 데이터 Publish가 가능
– Event 자체가 Atomic
Domain Event(Aggregate) 를 적용함으로써 Object–Relation 의 Impedance
Mismatch 문제를 회피
특정 시점의 상태값을 조회할 수 있다.
단점
상대적을 낯선 스타일 – Learning Curve
Event Store기반의 쿼리에 한계점으로 인해 CQRS가 필수
적용 분야 예시
추천(장바구니), 분석(Debugging), 감사(Audit)
Netflix Download Service License
31. CQRS (Command and Query Responsibility Segregation)
명령과 쿼리의 역할 구분
CQS
Bertrand Meyer가 “Object Oriented Construction”이란 책에서
거론한 개념으로, 모든 메소드는 한번의 액션에서 1) 상태를 변경하는
커맨드든 2) 데이터를 반환하는 쿼리든 한가지 액션만 취해야 한다는
것이다. 다시 말하자면 질문을 할때 대답을 변경하지 말라는 것이다.
이 개념이 Object로 확대되어, 상태를 변경하는 Command(CUD)와
Query(Read)를 분리해야 한다는 원칙
33. CQRS (Command Query Responsibility Segregation)
Problem :
최근 일주일간 장바구니 아이템 추가 중간에 주문을 취소한 주문만 조회
Solution
- 명령과 조회의 책임을 분리
- 상태변경을 처리하는 Command Model과
데이터를 조회하는 Query Model을 분리 구현
- 조회에 위한 Materialized View을 미리 생성
69. Axonframework.org / AxonIQ.com
Since 2010, 8 years
Version : 3.x
Many systems in production
Focus on Messaging in Commands,
Events and Queries
Plug in Event Store
JPA, JDBC and MongoDB OOTB
Scalability with JGroups
Eventuate.io vs RBMH Eventuate?
Since 2015
Version : Still 0.x
Focus of Event Sourcing and Event
Publication
Own Event Store implementation
Scalability with Akka
https://stackoverflow.com/questions/43136291/axon-framework-vs-eventuate-comparison
70. • Chris Richardson : http://Micoservice.io
• http://www.slideshare.net/RichardHarvey7/micro-services-and-containers
• Thought Works - Real Case : http://vvgomes.com/microservices-event-
sourcing/
• Mastering Chaos : https://www.slideshare.net/JoshEvans2/mastering-chaos-
a-netflix-guide-to-microservices
• Scaling Event Sourcing for Netflix Downloads :
https://www.infoq.com/presentations/netflix-scale-event-sourcing
71. CQRS is not a silver bullet...CQRS is not Event Sourcing...CQRS is not a
good wife...but...CQRS can open many doors. - Greg Young