itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op
TRANSCRIPT
![Page 1: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/1.jpg)
Chia sẻ kinh nghiệm:Thiết kế hệ thống, Tối ưu, Domain Driven Design, CQRS,Event Sourcing
Lê Minh Nghĩa,Toong, 8 Tràng Thi, Hà Nội
2015/09/09
![Page 2: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/2.jpg)
Giới thiệu• Lê Minh Nghĩa• Phone: 0936 073 986• Email: [email protected]• Facebook: http://www.facebook.com/nghialeminh• Quá khứ: Doko.VN – Cty Mimas
![Page 3: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/3.jpg)
Muốn giao lưu chia sẻ về• Enterprise Software Architecture• Domain Driven Design• CQRSCommand and Query Responsibility
Segregation (CQRS)• Service Oritented Architecture (SOA)• Event Driven Architecture• High Performance Database• Data Mining• Cost Optimization
![Page 4: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/4.jpg)
Các công việc đã, đang phụ trách
• Tối ưu hóa hệ thống• Xây dựng giải pháp xử lý distributed transaction• Triển khai Enterprise Service Bus để tích hợp hệ thống• Triển khai kiến trúc hệ thống theo mô hình CQRS–ES.• Xây dựng process manager cho việc xử lý đơn hàng• Triển khai xử lý đơn hàng bất đồng bộ
![Page 5: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/5.jpg)
Enterprise Software và các phương pháp
phân tích thiết kế phần mềm
![Page 6: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/6.jpg)
Enterprise Software là gì• Là các phần mềm phục vụ quản lý các doanh nghiệp
như: công ty, cửa hàng, siêu thị, ngân hàng…• Không bao gồm các phần mềm về viễn thông, game,
video…• Có rằng buộc dữ liệu chặt chẽ• Thường có luồng nghiệp vụ phức tạp• Không bị gắn chắt vào phần cứng
![Page 7: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/7.jpg)
Demo đơn giản• Xây dựng một site hiển thị danh sách sản phẩm• Cho phép đặt đơn hàng• Đơn hàng có thể xác nhận hoặc hủy• Môt đơn hàng có tên sản phẩm và số lượng
![Page 8: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/8.jpg)
“Hello” Enterprise Software• CRUD phương pháp đầu tiên để làm• Phân tích yêu cầu• Thiết kế CSDL• Cài đặt
![Page 9: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/9.jpg)
Nhìn lại, có gì không ổn nhỉ• Gắn chắt với thiết kế CSDL• Không gần với đời sống• Dữ liệu không đóng gói• Khả năng kế thừa hạn chế• Khả năng bảo trì hạn chế
![Page 10: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/10.jpg)
Chúng ta nghĩ gì và CRUD nói gì
• Chúng ta nói tới các đối tượng, các thực thể• Phần mềm chúng ta nói tới các lệnh, các chức năng• Phần mềm chúng ta không mô hình hóa cái chúng ta
hình dung
![Page 11: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/11.jpg)
Domain Driven Design• Chúng ta hình dung ra sao phần mềm cần phải xây dựng
như vậy• Cùng một model• Cùng một bộ ngôn ngữ• Dữ liệu và hành vi phải đi liền với nhau
![Page 12: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/12.jpg)
Vấn đề khi xây dựng Enterprise Software
![Page 13: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/13.jpg)
Vấn đề khi xây dựng Enterprise Software
![Page 14: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/14.jpg)
Ubiquitous Language
![Page 15: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/15.jpg)
Domain là gì• An area of knowledge or activity
![Page 16: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/16.jpg)
Domain Model
![Page 17: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/17.jpg)
Bounded Context
![Page 18: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/18.jpg)
Bounded Context
![Page 19: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/19.jpg)
Context Map
![Page 20: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/20.jpg)
Domain Model & Bounded Context
![Page 21: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/21.jpg)
Aggregate – Consistent Boundary
![Page 22: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/22.jpg)
Phân tách tầng lưu trữ ra riêng rẽ
![Page 23: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/23.jpg)
Cài đặt ra sao?• Đóng gói cao nhất• Không public set• Quên persistent, thiết kế model trước• Thiết kế data layer tách biệt domain• Thiết kế domain service• Thiết kế application service
![Page 24: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/24.jpg)
Vậy lợi ích là gì?
• Hiệu quả với các Bussiness phức tạp• Đáp ứng với sự thay đổi liên tục của Bussiness Logic• Các bên (dev, architect, domain expert) làm việc với
nhau hiệu quả• Chi phí phát triển sau khi đã có core domain giảm
![Page 25: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/25.jpg)
Còn có thể làm tốt hơn được không?
• Đọc dữ liệu nhanh hơn• Đọc dữ liệu đơn giản hơn• Ít rằng buộc hơn• Sử dụng các schema khác nhau• Đồng thời ghi dữ liệu vẫn phải đúng nghiệp vụ
![Page 26: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/26.jpg)
CQRS• Viết tắt: Command And Query Responsibility Segregation• Phân tách hai luồng riêng biệt: ghi dữ liệu và đọc dữ liệu• Sử dụng hai schema khác nhau
![Page 27: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/27.jpg)
CQRS hình thù thế nào?
![Page 28: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/28.jpg)
Make it Works• Command: một lệnh làm hệ thống thay đổi dữ liệu• Event: một sự thay đổi đã xảy ra• Command Bus: đường truyền các lệnh• Event Bus: đường truyền các event
![Page 29: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/29.jpg)
Có cái gì đó xuyên suốt? – Event!
• Mỗi sự thay đổi đều phát sinh event• Trạng thái của một đối tượng là một chuỗi các sự kiện xảy ra• Chạy lại event để tái tạo đối tượng
![Page 30: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/30.jpg)
Event Sourcing• Lưu trữ nguồn event của một đối tượng• Xây dựng event store lưu trữ toàn bộ event của một
object• Khi lưu một event đồng thời publish một event
![Page 31: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/31.jpg)
Lưu trữ thế nào – Event Store• Xây dựng event store• Một bảng dữ liệu dùng để append event
![Page 32: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/32.jpg)
CQRS - ES• CQRS kết hợp với ES dễ dàng• Mỗi command sẽ phát sinh một hoặc nhiều event• Các event được đánh version• Version của một object là version của event
![Page 33: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/33.jpg)
Process Manager Pattern• Mô tả luồng nghiệp vụ là luồng các message• Cần một manager để điều hướng các luồng message
![Page 34: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/34.jpg)
Event as the Core
![Page 35: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/35.jpg)
State Machine Pattern• Lưu trữ state của hệ thống• Xây dựng luồng chuyển trạng thái• Tách phần quản lý trạng thái ra khỏi aggregate
![Page 36: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/36.jpg)
State Matrix• Mô tả việc chuyển trạng thái dưới dạng ma trận• Dễ dàng quản lý• Logic rõ ràng
![Page 37: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/37.jpg)
State Matrix
![Page 38: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/38.jpg)
Thiết kế hệ thống phân tán có hiệu năng cao
• Tránh tranh chấp dữ liệu• Tính toán trước• Sử dụng tài nguyên tính toán hợp lý
![Page 39: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/39.jpg)
Nguyên lý CAP
![Page 40: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/40.jpg)
Một case đơn giản mà không đơn giản
• Xây dựng web service update CSDL đảm bảo strong consistency data
![Page 41: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/41.jpg)
Distributed Transaction• Giao dịch với nhiều nguồn dữ liệu• Cần phải đảm bảo consistency• Giải pháp là gì
![Page 42: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/42.jpg)
Two Phase CommitThưc hiện hai phase:- Write- Commit
Nếu lỗi: Undo tất cả
Đòi hỏi: lock tất cả các resources
![Page 43: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/43.jpg)
Eventually Consistency
• Thay thế consistency bằng eventually consistency• Eventually consistency khắp mọi nơi• Không thể eventually tất cả nhưng hãy tối đa có thể
![Page 44: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/44.jpg)
Hạn chế của kiến trúc hướng dịch vụ theo mô hình request - response
![Page 45: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/45.jpg)
SOA theo mô hình Event Driven
![Page 46: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/46.jpg)
Bất đồng bộ và bài toán tối ưu về hiệu năng và kinh tế• Bất đồng bộ tối đa có thể• Không cần scale tất cả các node• Tập trung tài nguyên tính toán cho node đòi hỏi hiệu
năng cao
![Page 47: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/47.jpg)
Sử dụng Message Bus để phân tách hệ thống
![Page 48: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/48.jpg)
Các vấn đề cần chú ý
• Khi nào CQRS phù hợp?• Hệ thống collobrative sysytem• Phải có nền tảng message ổn định• Cần phải giải quyết transation giữa message bus và database• Xác định tính chất Idempotent• Chú ý về thứ tự event• Xây dựng event store để không bị thắt cổ trai
![Page 49: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/49.jpg)
Các giải pháp
• Phân tích luồng nghiệp vụ thật cẩn thận• Chọn lựa nền tảng message bus có khả năng persistent và
scale out tốt cũng như có khả năng điều hướng message tốt• Tránh tình trạng bị trùng lặp message hoặc có cơ chế phòng trừ• Xây dựng giải pháp partition và sharding data cho event store
![Page 50: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/50.jpg)
Kết luận“Không thể scale được nếu không chia tách được”
![Page 51: Itlc hn 20150909 - le minh nghia - crud, ddd, cqrs, cqrs-es and op](https://reader031.vdocuments.site/reader031/viewer/2022013123/58f101981a28ab66618b459d/html5/thumbnails/51.jpg)
Thank you.Q&A