美文网首页
设计一个mint.com

设计一个mint.com

作者: MontyOak | 来源:发表于2018-06-05 19:36 被阅读9次

    设计一个mint.com 原文链接

    1.描述使用场景和约束

    使用场景:

    • 用户访问金融账户
    • 解析账户中的交易信息
      • 每日定期更新
      • 交易分类,允许用户自定义分类
      • 月度账务汇总分析
    • 系统推荐预算值
      • 允许用户修改预算
      • 消费接近或超过预算时提示用户

    假设和约束:

    • 流量不均衡
    • 只为30天内的活跃用户进行自动更新账户的操作
    • 用户添加和修改账户的操作相对较少
    • 预算接近或者超出时的提示信息不要求高实时
    • 1000万用户量
      • 每个用户10个预算分类,1亿的预算信息
      • 商家来确定交易分类
    • 3000万金融账户信息
    • 每月50亿笔交易
    • 读写比10:1

    容量估算:
    每笔交易:

    • user-id 8字节
    • created_at 5字节
    • seller 32字节
    • amount 5字节
      总计50字节
    • 每月250GB的交易数据信息
    • 平均每秒2000笔交易
    • 平均每秒200读请求

    2.创建系统设计图

    系统总体设计图

    3.设计关键组件

    使用场景:用户连接金融账户
    设计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这些字段上创建索引。

    使用场景:服务从账户解析交易信息
    一般在下面情况下需要从账户解析交易信息:

    • 用户第一次添加账户时
    • 用户手动刷新账户信息时
    • 30天内有活跃动作的用户每天定时解析交易信息

    主要服务动作为:

    • 账户服务用job来处理mq中的信息
      • 解析账户交易信息是一个网络阻塞的任务,可以利用mq做异步执行
    • 交易解析服务做以下事情:
      • 从mq中拉取账户和相关信息并解析账户的交易信息,同步写到日志文件中
      • 分类服务把交易信息分类存储
      • 预算服务定期计算月度分类开销
        • 如果开销接近或者超过预算,异步通知用户
      • 更新transactions表信息
      • 按月度分类数据更新monthly_spending

    下面是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)
    

    在分类服务中,可以为热点卖家创建交易分类词典:

    class DefaultCategories(Enum):
    
        HOUSING = 0
        FOOD = 1
        GAS = 2
        SHOPPING = 3
        ...
    
    seller_category_map = {}
    seller_category_map['Exxon'] = DefaultCategories.GAS
    seller_category_map['Target'] = DefaultCategories.SHOPPING
    ...
    
    
    class Categorizer(object):
    
        def __init__(self, seller_category_map, self.seller_category_crowd_overrides_map):
            self.seller_category_map = seller_category_map
            self.seller_category_crowd_overrides_map = \
                seller_category_crowd_overrides_map
    
        def categorize(self, transaction):
            if transaction.seller in self.seller_category_map:
                return self.seller_category_map[transaction.seller]
            elif transaction.seller in self.seller_category_crowd_overrides_map:
                self.seller_category_map[transaction.seller] = \
                    self.seller_category_crowd_overrides_map[transaction.seller].peek_min()
                return self.seller_category_map[transaction.seller]
            return None
    

    交易部分的代码:

    class Transaction(object):
    
        def __init__(self, created_at, seller, amount):
            self.timestamp = timestamp
            self.seller = seller
            self.amount = amount
    

    使用场景:服务推荐预算
    最开始时可以根据用户收入按照固定比例划分预算,如果用户手动设置的某一分类的预算值,就把它存放在budget_overrides表中。

    class Budget(object):
    
        def __init__(self, income):
            self.income = income
            self.categories_to_budget_map = self.create_budget_template()
    
        def create_budget_template(self):
            return {
                'DefaultCategories.HOUSING': income * .4,
                'DefaultCategories.FOOD': income * .2
                'DefaultCategories.GAS': income * .1,
                'DefaultCategories.SHOPPING': income * .2
                ...
            }
    
        def override_category_budget(self, category, amount):
            self.categories_to_budget_map[category] = amount
    

    可以利用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)
    

    4. 完善设计

    最终设计图

    相关文章

      网友评论

          本文标题:设计一个mint.com

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