Elixir TinyUrl Exercise
How would you design a service like TinyURL?
Setup
Requirements
- Elixir 1.15
- Erlang/OTP 25
- Postgres
- IDE of your choice
Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install Elixir
brew install asdf
asdf install elixir 1.15
Install Postgres
brew install postgres
What is TinyURL
The TinyURL service will take in a URL and will return a shortened version of the URL so when the user navigates to the shortened URL in the browser, it will redirect to the original URL.
Functional Requirements
- Given a URL, the service should generate a shorter and unique alias of it. This is called a
short_link
. This link should be short enough to be easily copied and pasted into applications. Don't worry about creating this link, I have created a dummy hashing function just for implementation purposes. - When users access a
short_link
, the service should redirect them to theoriginal_link
. - It is not required to have a user account to create a
short_link
. (optional) - Links will expire after a standard default timespan. Users should be able to specify the expiration time. (optional)
- Users should optionally be able to pick a custom
short_link
for their URL. (optional) - Registered users can sign in and see all their created
short_links
. (optional)
Non-Functional Requirements
- Shortened links should be deterministic.
- Shortened links should not be guessable (not predictable).
- The system should be highly available. This is required because, if our service is down, all the URL redirections will start failing.
- URL redirection should happen in real-time with minimal latency.
Implementation Details
- Implement storing of URLs in
cache.ex
- Implement the context business logic in
linking.ex
- Implement the APIs in
link_controller.ex
- Implement the JSON response in
link_view.ex
- Implement tests (if time allows)
Topics To Discuss
- Encoding
- Database
- Schema
- Cache
- Security
- Requests
- Availability
Encoding
- How many chars do you want to use for the shortened URL?
- How would you shorten the URL so it is unique?
- Would you return the same URL if the same one is found?
- How long does it need to be?
- If you wanted to have different shortened URLs for the same URL, how would you ensure the same shortened URL is not the same?
- If user is not signed in, how would handle a shortened URL collision?
Database
- What DB would you use? Why?
- How would you shard the DB if a single DB got too large?
Schema
- Define what is needed to accomplish all the requirements.
Cache
- Would you use a cache?
- Which one and why?
- How much cache memory do you think we need?
Security
- How would you handle an automated bot spamming of your service and creating useless URLs?
Requests
- How would you handle your server being overloaded with requests? Maximum connections reached.
Availability
- Assuming this service is accessed all over the world, how would you ensure requests are fast in other countries?