查看: 4768| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

[经验总结] 设计tinyUrl详细总结

   
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 JerryYang1120 于 2021-9-27 10:51 编辑

TinyURL 系统设计纲要
(如果本文对你有用,请点赞加米。)
# Requirement 需求
  • * Each URL can only be mapped to a unique alias
  • * Each alias can be mapped back to a unique URL easily
  • * http://tinyurl.com/<alias_hash> and alias_hash is a fixed length string containing [A-Z, a-z, 0-9]


# Constraints
  • * Amount of data to store
  • * Amount of traffic – QPS or the query-per-second rate
  • * How many new URLs per second
  • * Peak time / Hot spot
  • * Projection (10x times) Scalability


# Neccessary for 1 million daily users
  • * Insert


Per day : 1,000,000 * 1% (function usage) * 10 (function frequency) = 100,000
Per year : 100,000 * 365 = 36,500,000
Per second : 100,000 / 86400 = 1.2

  • * Lookup


Per day : 1,000,000 * 100%(function usage) * 3 (function frequency) = 3,000,000
Per second: 3,000,000 / 86400 = 35

# Interface
REST API, Refer : https://bitly.com/
* CREATE:/shorten
* GET:/{short_url}

# Storage

Avg size of longUrl = 100 bytes
Avg size of shortUrl = 4 bytes (int)
State = 4 byte
Daily new URL = 3,000,000 * 108 = 324 MB
Yearly new URL = 324MB * 365 = 118 GB

## SQL vs NoSQL
  • * 是否需要丰富的 SQL Query? NoSQL的SQL Query丰富度不如SQL,不过目前差距正在缩小,比如 MongoDB。此题不需要,该因素可以忽略。
  • * 是否追求效率?大多数Web Framework与SQL数据库兼容得很好(自带ORM),意味着可以少些很多代码。此题没太多业务逻辑,该因素可以忽略。
  • * 是否需要支持Transaction? NoSQL不支持Transaction,但是可以在业务层做处理保证。
  • * 对QPS要求高不高? NoSQL性能高,MondoDB可以到10k级别,MySQL只能在k这个级别。
  • * 对Scalability的要求有多高?sharding,replica。


## Storage with NoSQL
  • * Randomly generate 6 characters containing [A-Z, a-z, 0-9]
  • * Use consistent hashing for partition
  • * Use NoSQL’s distributed key-value store
  • * Eventually consistent
  • * Short URL already exists? What to do with conflicts? Retry


# Scale
  • * Partition By Key Or
  • * Consistent Hashing
# Write Optimization* Write Optimization 1:
  • Use centralized “Counter” to generate unique URL
  • Similar to auto-increment id in SQL
  • Uniqueness guaranteed

* Write Optimization 2:
  • Round robin to different URL generators, each governing a dedicate key space
  • Partition key spaces such as “AXXXXX”, “BXXXXX” and etc.
  • Similar to sharing - avoid conflicts


# Other factors to consider
  • * High availability
  • * How to cache? Preload / Replace
  • * Reuse popular short urls
  • * Custom URLs
  • * Analytics (usage of URLs)
  • * Automatic link expiration
  • * Manual link removal
  • * UI v.s. API
  • * User’s historical short urls


## How to cache

- First use sharding to partition different regions
You may have China DB and US DB for example
Use redis / memcached LRU cache for repeated query



## Reuse popular shortUrl
  • Popular urls such as www.google.com
  • Store those popular urls based on clicks
  • There is no need to generate different short urls for it. Use the same short url


## Customer URLs

- (1) Use another table for custom URLs (SQL)
  > Need to look up two tables now for read/write
  > Atomicity not guaranteed in NoSQL
- (2) Use the same table with additional boolean column (NoSQL)
  > Same look up, need to query the boolean column
  > Atomicity still guaranteed
  > Not working well for SQL solution, which is auto id based













评分

参与人数 6大米 +7 收起 理由
高渐离击筑高歌 + 2 很有用的信息!
shawn1776 + 1 很有用的信息!
duqiyanerer + 1 给你点个赞!
moveingsun + 1 赞一个
earthmount + 1 给你点个赞!

查看全部评分


上一篇:大家是怎么食用Designing Data-Intensive Applications的?
下一篇:系统设计Step By Step

本帖被以下淘专辑推荐:

推荐
yyz20002008 2021-9-28 23:58:55 | 只看该作者
全局:
请问什么公司会考设计tinyURL?
回复

使用道具 举报

推荐
 楼主| JerryYang1120 2021-9-29 02:15:16 | 只看该作者
全局:
yyz20002008 发表于 2021-9-28 08:58
请问什么公司会考设计tinyURL?

这个很多公司会问,具体可以搜索地理棉巾
回复

使用道具 举报

全局:
ymsfd007 发表于 2021-9-28 03:02
这个题的重点应该不在于scale up,从需求上来看,读35QPS, 写1.2QPS,几乎就是单机单数据库模式就搞定了。 ...

memcache, redis, hazelcast这种分布式的key value DB都可以实现快速查找短url到长url。我个人觉得更好的选择是为短url的key设置expire strategy,比方说一个月,如果在内存中查不到,再继续到下级存储比如mongodb去查找。
回复

使用道具 举报

🔗
ymsfd007 2021-9-28 15:02:50 | 只看该作者
全局:
这个题的重点应该不在于scale up,从需求上来看,读35QPS, 写1.2QPS,几乎就是单机单数据库模式就搞定了。
需求之一是怎么在海量的数据里快速查找到record,上面有提到解决办法吗?
回复

使用道具 举报

🔗
david_smith 2021-9-28 21:13:24 | 只看该作者
全局:
我有一个疑问,这个设计需要考虑  Use consistent hashing for partition 吗?

Partition 其实是数据库帮你解决了, 设计人要考虑的是怎么设计Data Model Schema,这么设置Partition Key,现实工程中,你肯定不会自己实现consistent hashing来解决Partition的问题,如果非得在这个问题中深究这个问题的话,不如直接面试如何设计一个分布式数据库。
回复

使用道具 举报

🔗
shawn1776 2021-9-29 01:10:10 | 只看该作者
全局:
新手请问:具体应该怎么回答?

1)是以后端为主吗?
2)还是要全栈?
3) ## SQL vs NoSQL? 这个是要怎么解答的?
回复

使用道具 举报

🔗
 楼主| JerryYang1120 2021-9-29 02:21:43 | 只看该作者
全局:
shawn1776 发表于 2021-9-28 10:10
新手请问:具体应该怎么回答?

1)是以后端为主吗?

系统设计是open ended question,要你跟面试官的沟通。通常是后端,当然你要往你熟悉的方向drive,不熟悉你也不用硬磕. SQL vs NoSQL 对于这个单机都能解决的问题来说没有太大区别,当然扩展一下需要多机的情况就需要考虑sql和nosql的tradeoff了

评分

参与人数 1大米 +1 收起 理由
shawn1776 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表