注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
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
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
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
|