美文网首页
系统设计面试题 - 设计 Mint.com

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

作者: 专职跑龙套 | 来源:发表于2018-04-26 18:47 被阅读71次

引用:
系统设计入门

设计 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)

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

  • 往表 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 计算量和吞吐量比较大,可以创建多个实例,并行处理。

相关文章

网友评论

      本文标题:系统设计面试题 - 设计 Mint.com

      本文链接:https://www.haomeiwen.com/subject/dnqmlftx.html