Related: http://massivetechinterview.blogspot.com/2015/06/how-to-design-tiny-url-or-url-shortener.html
https://www.youtube.com/watch?v=fMZMm_0ZhK4
N00tc0d3r
tinyurl is a URL service that users enter a long URL and then the service return a shorter and unique url such as "http://tiny.me/5ie0V2". The highlight part can be any string with 6 letters containing [0-9, a-z, A-Z]. That is, 62^6 ~= 56.8 billions unique strings.
On Single Machine
Suppose we have a database which contains three columns: id (auto increment), actual url, and shorten url.
Intuitively, we can design a hash function that maps the actual url to shorten url. But string to string mapping is not easy to compute.
Notice that in the database, each record has a unique id associated with it. What if we convert the id to a shorten url?
Basically, we need a Bijective function f(x) = y such that
The basic process can be:
Insert
https://www.youtube.com/watch?v=fMZMm_0ZhK4
N00tc0d3r
tinyurl is a URL service that users enter a long URL and then the service return a shorter and unique url such as "http://tiny.me/5ie0V2". The highlight part can be any string with 6 letters containing [0-9, a-z, A-Z]. That is, 62^6 ~= 56.8 billions unique strings.
http://www.careercup.com/question?id=5185808560553984
On Single Machine
Suppose we have a database which contains three columns: id (auto increment), actual url, and shorten url.
Intuitively, we can design a hash function that maps the actual url to shorten url. But string to string mapping is not easy to compute.
Notice that in the database, each record has a unique id associated with it. What if we convert the id to a shorten url?
Basically, we need a Bijective function f(x) = y such that
- Each x must be associated with one and only one y;
- Each y must be associated with one and only one x.
e.g. 0-0, ..., 9-9, 10-a, 11-b, ..., 35-z, 36-A, ..., 61-Z.On Multiple Machine
The basic process can be:
Insert
- Hash an input long url into a single integer;
- Locate a server on the ring and store the key--longUrl on the server;
- Compute the shorten url using base conversion (from 10-base to 62-base) and return it to the user.
- Convert the shorten url back to the key using base conversion (from 62-base to 10-base);
- Locate the server containing that key and return the longUrl.