|
| 1 | +package com.inuker.solution.system; |
| 2 | + |
| 3 | +/** |
| 4 | + * Created by liwentian on 2017/10/17. |
| 5 | + */ |
| 6 | + |
| 7 | +public class DesignTinyURL { |
| 8 | + |
| 9 | + /** |
| 10 | + * 这题和#535. Encode and Decode TinyURL类似,不过这题更偏向于系统设计 |
| 11 | + * |
| 12 | + * 1. How many unique identifiers possible? Will you run out of unique URLs? |
| 13 | + * 如果按6个字符计算,一共有62**6种可能,大概三百多亿,越到后面碰撞越来越多,可以通过增加字符数来解决 |
| 14 | + * |
| 15 | + * 2. Should the identifier be increment or not? Which is easier to design? Pros and cons? |
| 16 | + * 不能,那样容易被破解,比如爬虫直接按ID递增的顺序一个个抓即可。 |
| 17 | + * |
| 18 | + * 3. Mapping an identifier to an URL and its reversal - Does this problem ring a bell to you? |
| 19 | + * |
| 20 | + * 4. How do you store the URLs? Does a simple flat file database work? |
| 21 | + * 采用分布式,负载均衡随机分发到一台机器,KV数据库,以短网址为key,长网址为value |
| 22 | + * |
| 23 | + * 5. What is the bottleneck of the system? Is it read-heavy or write-heavy? |
| 24 | + * 读显然比写多,比如用户在微博分享了一个资源,以短链接的形式。很多用户访问这个链接,则读远大于写。 |
| 25 | + * |
| 26 | + * |
| 27 | + * 6. Estimate the maximum number of URLs a single machine can store. |
| 28 | + * |
| 29 | + * 7. Estimate the maximum number of queries per second (QPS) for decoding a shortened URL in a single machine. |
| 30 | + * |
| 31 | + * 8. How would you scale the service? For example, a viral link which is shared in social media could result in a peak QPS at a moment's notice. |
| 32 | + * |
| 33 | + * 9. How could you handle redundancy? i,e, if a server is down, how could you ensure the service is still operational? |
| 34 | + * 一致性哈希,虚拟节点 |
| 35 | + * |
| 36 | + * |
| 37 | + * 10. Keep URLs forever or prune, pros/cons? How we do pruning? |
| 38 | + * |
| 39 | + * 11. What API would you provide to a third-party developer? |
| 40 | + * encode, decode |
| 41 | + * |
| 42 | + * 12. If you can enable caching, what would you cache and what's the expiry time? |
| 43 | + * KV db, key为short url,value为long url |
| 44 | + * |
| 45 | + * |
| 46 | + * 13, 如何防攻击?假如有人大量发相同的url耗光 |
| 47 | + * 限制IP;一台KV服务器做缓存,key为long url,value为短url,如果缓存中有直接返回,cache是LRU的 |
| 48 | + * |
| 49 | + * |
| 50 | + */ |
| 51 | + |
| 52 | + |
| 53 | + /** |
| 54 | + * 可参考https://segmentfault.com/a/1190000006140476 |
| 55 | + */ |
| 56 | +} |
0 commit comments