引用:
系統設計入門
設計 Mint.com
什么是 Mint.com,一款免費的理財類軟件,使用它你可以在一個軟件內同時管理多個個人賬戶,通過這款軟件可以追蹤你的開支,創建預算,幫助你管理你的錢包。
第一步:通過討論,明確限制及用例,確定Scope
支持的用例:
- 用戶可以連接到金融賬號
- 系統從賬號中獲取每一筆交易
- 每天更新
- 為每一筆交易分類,允許用戶手動設置分類(例如,吃飯,交通,房租等等)
- 系統會推薦一個預算
- 允許用戶手動設置預算
- 當交易累計接近或者超過預算時提醒用戶
- 系統高可用 high availability
不支持的用例:
- 系統不進行額外的統計
Constraints and assumptions:
- 訪問不均勻
- 添加及刪除金融賬號不會頻繁發生
- 預算的通知不需要是實時的
- 10 million用戶
- 每個用戶大約10個分類,大約3個金融賬號
- 每月5 billion次交易需要記錄寫入,每秒2000次
- 每月500 million次讀取,每秒200次
- 寫讀比例 10 :1
計算規模:
每一筆交易:
- user_id:8 bytes
- created_at:5 bytes
- seller:32 bytes
- amount:5 bytes
- 總共: ~50 bytes
每個月:50 bytes * 5 billion = 250G
三年:9TB
第二步:高層次設計
設計 Mint.com
第三步:設計核心組件
使用關系型數據庫存儲賬號信息,創建表 accounts
:
id int NOT NULL AUTO_INCREMENT
created_at datetime NOT NULL
last_update datetime NOT NULL
account_url varchar(255) NOT NULL
account_login varchar(32) NOT NULL
account_password_hash char(64) NOT NULL
user_id int NOT NULL
PRIMARY KEY(id)
FOREIGN KEY(user_id) REFERENCES users(id)
在 id
,user_id
,created_at
三個字段上創建索引,加快查詢的效率。
創建表 transactions
來存儲交易信息:
id int NOT NULL AUTO_INCREMENT
created_at datetime NOT NULL
seller varchar(32) NOT NULL
amount decimal NOT NULL
user_id int NOT NULL
PRIMARY KEY(id)
FOREIGN KEY(user_id) REFERENCES users(id)
創建表 monthly_spending
來存儲花費信息:
id int NOT NULL AUTO_INCREMENT
month_year date NOT NULL
category varchar(32)
amount decimal NOT NULL
user_id int NOT NULL
PRIMARY KEY(id)
FOREIGN KEY(user_id) REFERENCES users(id)
提供一個REST API,用戶可以連接到金融賬號:
- 往表
accounts
中插入一條記錄
Transaction Extraction Service的主要工作:
- 從任務隊列中獲取金融賬號,并使用用戶名及密碼等從對應的賬號獲取交易記錄,將結果存放在Object Store中。
- 使用 Category Service 來為每一筆交易分類。
- 使用 Budget Service 來統計每一個分類上的花費。
- 如果交易累計接近或者超過預算,使用 Notification Service 來通知用戶
- 更新
transactions
表及monthly_spending
表 - 使用 Notification Service 來通知用戶該賬號已同步完畢。
Category Service的主要工作:
- 維護 seller 與 category 的一個對應關系,可以使用 Map。
系統會推薦一個預算:
- 使用 MapReduce 來處理所有的交易記錄:
- 為每一筆交易分類
- 統計每一個分類上的花費
class SpendingByCategory(MRJob):
def __init__(self, categorizer):
self.categorizer = categorizer
self.current_year_month = calc_current_year_month()
...
def calc_current_year_month(self):
"""Return the current year and month."""
...
def extract_year_month(self, timestamp):
"""Return the year and month portions of the timestamp."""
...
def handle_budget_notifications(self, key, total):
"""Call notification API if nearing or exceeded budget."""
...
def mapper(self, _, line):
"""Parse each log line, extract and transform relevant lines.
Argument line will be of the form:
user_id timestamp seller amount
Using the categorizer to convert seller to category,
emit key value pairs of the form:
(user_id, 2016-01, shopping), 25
(user_id, 2016-01, shopping), 100
(user_id, 2016-01, gas), 50
"""
user_id, timestamp, seller, amount = line.split('\t')
category = self.categorizer.categorize(seller)
period = self.extract_year_month(timestamp)
if period == self.current_year_month:
yield (user_id, period, category), amount
def reducer(self, key, value):
"""Sum values for each key.
(user_id, 2016-01, shopping), 125
(user_id, 2016-01, gas), 50
"""
total = sum(values)
yield key, sum(values)
第四步:擴展設計
設計 Mint.com
- 為了服務不同區域的用戶,加快訪問的速度,使用CDN加速。
- 為了同時響應更多請求,對服務器水平擴展,并使用Load Balancer做負載均衡。
- 為了加快讀取效率,使用Memory Cache,避免頻繁訪問數據庫。
- 采用主從復制的數據庫模式。主庫同時負責讀取和寫入操作,并復制寫入到一個或多個從庫中,從庫只負責讀操作。
- Transaction Extraction Service 計算量和吞吐量比較大,可以創建多個實例,并行處理。