系统设计面试题 - 设计 Mint.com

2018-04-26  本文已影响71人  专职跑龙套

引用:
系统设计入门

设计 Mint.com

解答

什么是 Mint.com,一款免费的理财类软件,使用它你可以在一个软件内同时管理多个个人账户,通过这款软件可以追踪你的开支,创建预算,帮助你管理你的钱包。

第一步:通过讨论,明确限制及用例,确定Scope

支持的用例:

不支持的用例:

Constraints and assumptions:

计算规模:
每一笔交易:

每个月: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)

iduser_idcreated_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,用户可以连接到金融账号:

Transaction Extraction Service的主要工作:

Category Service的主要工作:

系统会推荐一个预算:

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
上一篇 下一篇

猜你喜欢

热点阅读