Please enable JavaScript.
Coggle requires JavaScript to display documents.
Decomposing the Database (Pattern: Move Foreign-Key Relationship to Code…
Decomposing the Database
Pattern: The Shared Database
缺乏業務邏輯的內聚性,業務邏輯散在 DB 深處,誰能控制這些資料,不得而知
Coping Patterns 對應方法
Pattern: Database View
Pattern: Database Wrapping Service
Where to Use It
static reference data, EX: postal code
把 DB 當成是一個資料存取的 endpoint,提供多 services 讀取(參考 Pattern: DB-as-a-Service Interface)
Pattern: Database View
可減輕耦合、限制資料可視範圍,隱藏不該訪問的資訊
The Database as a Public Contract
共用 DB,用 Vault 管理密碼
Views to Present
使用 View 解決 schema 問題
Limitations
DB 不見得支援 View
View 是唯讀的
Ownership
把 View 視為一種 service interface 去維護它
Where to Use It
如果切服務需要切 單體式 DB schema 時,通常使用 View
但它有限制的,最好還是依照一些拆解方法,邁出正確的一步
Pattern: Database Wrapping Service
Where to Use It
作者覺得,別想要去拆 schema,難度很高
一家澳洲銀行的故事,作者參加一個短期計畫,改進上版 prod 的路徑(CI/CD ?)。
銀行有個權利(Entitlements)系統,管理哪些人可以看哪些帳戶,例如:會計 可以查看 A, B, C 公司,B公司他們可以在帳戶之間轉帳上限500UDS,C公司可在帳戶之間無限轉帳,但是提款最高250USD。這些權利設定都是由 SP處理。所有存取都是由這些邏輯控制。
隨著銀行規模擴大,資料庫的負載也越來越大,該公司把所有的錢,都投入在 Oracle (XD)。令人擔憂的是,考慮到預期的增長,以及硬體的需求,最終會達到一個超過數據庫能力的地步。
用 Service 去包圍 DB
用 DB Wrapping Service Pattern 去減輕 Central DB 依賴
Pattern: Database-as-a-Service Interface
Implementing a Mapping Engine
Compared to Views
Where to Use It
Transferring Ownership
Pattern: Aggregate Exposing Monolith
Figure 4-8
Where to Use It
Pattern: Change Data Ownership
Pattern: Synchronize Data in Application
Step 1: Bulk Synchronize Data
Step 2: Synchronize on Write, Read from Old Schema
Step 3: Synchronize on Write, Read from New Schema
Where to Use This Pattern
Where to Use It
Pattern: Tracer Write
Data Synchronization
Example: Orders at Square
Where to Use It
Creating the new service
Synchronizing the data
Migrating consumers
Splitting Apart the Database
Physical Versus Logical Database Separation
Splitting the Database First, or the Code?
先拆 DB 還是先拆 Code ?
Split the Database First
無法產生短期利益
PATTERN: REPOSITORY PER BOUNDED CONTEXT
PATTERN: DATABASE PER BOUNDED CONTEXT
Split the Code First
缺點,改寫完後,繼續使用共享資料庫方法
PATTERN: MONOLITH AS DATA ACCESS LAYER
PATTERN: MULTISCHEMA STORAGE
Split Database and Code Together
大刀闊斧,需評估帶來的影響
So, Which Should I Split First?
看情況
Schema Separation Examples
Pattern: Split Table
Where to Use It
直接,但是 需要找到 bounded context (感覺不容易?)
Pattern: Move Foreign-Key Relationship to Code
Moving the Join
帶到
Jaeger
(監控 latency)
Data Consistency
Where to Use It
確保不會把一體的東西拆成兩個,例如:OrderLine table,把購買品項明細拆出去,會有很大的資料整合問題
CHECK BEFORE DELETION
HANDLE DELETION GRACEFULLY
DON’T ALLOW DELETION
SO HOW SHOULD WE HANDLE DELETION?
Transactions
ACID Transactions
Atomicity 原子性
Consistency 一致性
Isolation 隔絕
Durability 耐久性
Still ACID, but Lacking Atomicity?
Two-Phase Commits
Distributed Transactions—Just Say No
Sagas
Saga Failure Modes
SAGA ROLLBACKS
REORDERING STEPS TO REDUCE ROLLBACKS
MIXING FAIL-BACKWARD AND FAIL-FORWARD SITUATIONS
Implementing Sagas
ORCHESTRATED SAGAS
CHOREOGRAPHED SAGAS
MIXING STYLES
SHOULD I USE CHOREOGRAPHY OR ORCHESTRATION?
Sagas VS. Distributed Transactions
Summary
找到 service boundaries,建立接縫
降低拆解服務的成本
Static Reference
PATTERN: DUPLICATE STATIC REFERENCE DATA
PATTERN: DEDICATED REFERENCE DATA SCHEMA
PATTERN: STATIC REFERENCE DATA LIBRARY
PATTERN: STATIC REFERENCE DATA SERVICE
WHAT WOULD I DO?