Requirements


Functional Requirements:


  • Create a short URL for a given long URL.
  • Return the long URL associated with a given short URL.



Non-Functional Requirements:


  • List the key non-functional requirements (eg low latency, scalability, reliability, etc.)...
  • high availability
  • low latency
  • horizontal scaling


API Design

Define the APIs expected from the system. This is your chance to analyze and define the read and write paths so that you can come up with the high-level design...


  • API to get shortened URL
  • API to get long URL from shortened URL
  • API to get the web page from shortened URL


High-Level Design

Describe the overall system architecture. Identify the main components needed to solve the problem end-to-end. Use the diagramming tool to create a block diagram.

  • client who will be using the system
  • Load balancers who will receive the requests and redirect to appropriate API servers
  • API servers which will handle the API requests mentioned above
  • In memory cache for fast reading of existing and frequently used tiny URLs. We can use Redis for this.
  • SQL database for storing mapping from long URL to short URL and vice versa. Can set up a MySQL database.


Detailed Component Design

Deep dive into 2-3 key components. Explain how they work, how they scale, discuss tradeoffs, capacity, and any relevant algorithms or data structures.

  • For each URL being input to get the shortened URL, we need to check if it already exists in the system. Most frequently used will be kept in Redis with some set key expiration time.
  • If the URL is not present in cache or DB, the ID will be assigned by API server and base62 of that ID will be used to generate shortened URL. How the ID will be generated is discussed below.
  • API servers will also handle any requests to access shortened URLs by redirecting them to the actual long URL.
  • We can use Apache zookeeper to give a range of IDs to each API server. If a new URL comes, each API server can assign it the required ID and use it to generate shortened URL. This will not allow any collision.
  • For scalability, we will need to have read replicas for DB and multiple Redis nodes. We can use consistent hashing to distribute the keys in Redis.