
Both the app server checks if the same short URL exists in DB or not and both of them insert mapping into the table. However both of them got the same random number, as a result, generate same 7 char short URL, i.e we have same short URL for different long URL. 0 for Chinese websites, 1 for American websites.What if you have two different app servers and at one point in time they convert long URL A and long URL B to a short URL. Use geographical information as the sharding key, e.g. Put Chinese DB in China, American DB in the United States. Short_url request -> get the sharding key (first byte of the short_url) -> search in the corresponding machine based on sharding key -> return long_urlĮach time we add a new machine, put half of the range of the most used machine to the new machine. Write long_url -> hash(long_url)%62 -> put long_url to the specific machine according to hash value -> generate short_url on this machine -> return short_url Each machine is responsible for the service in the part of the cycle. It doesn't matter how many pieces because there probably would not be over 62 machines (it could be 360 or whatever).
Tinyurl system design pro#
The pro way is put the sharding key as the first byte of the short_url.Īnother way is to use consistent hashing to break the cycle into 62 pieces.

So, we do not use global auto_increment_id. Here comes another question: How could multiple machines share a global auto_increment_id? Horizontal shardingĬurrently table structure is (id, long_url). What if we need one more MySQL machine? Issues: All the areas share a DB used to match the users to the closest web server (through DNS) when they have a miss on the cache. Improve the response speed between web server and user's browserĭifferent locations use different web server and cache server. We could put 90% read request on the cache. When getting long_url, search in the cache first, then database.
Tinyurl system design how to#
String shorturl = base10ToBase62(COUNTER) įor (int i = 0 i = '0' & c = 'a' & c = 'A' & c Web Core DB O: optimize How to improve the response speed? Improve the response speed between web server and database It could be the auto_increment_id in SQL database.Įlements = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 6 bits could represent 62^6=57 billion.Įach short_url represent a decimal digit.


Service like Netflix may have storage issues. Storage is not the problem for this kind of system.

N: Need (Assume the system is not massive if you are not sure) QPS (queries per second)
