Requirements


Functional Requirements:


  • Create a short URL for a given long URL.
  • Return the long URL associated with a given short URL.
  • shortened URL should be a readable slug e.g. https://shrt.com/urls/host-slug-article-hybrid



Non-Functional Requirements:


  • low latency
  • high availability



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...


POST /urls/:url


GET /urls


Optional header cookie allows url to be scoped to user.



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.


API Gateway

  • handles user sessions - calls user service to verify the user's session cookie is valid, issue new one
  • handles requests to shortened URLs, requesting the longer version from the URL DB Service and redirecting the user if a match is found


User service

  • generates short-TTL session tokens and saves to user table
  • returns user object for session token


URL generation service

  • generates a unique URL combining slugify lib with a random int generator.
  • Sends to DB service


URL DB service

  • saves shortened urls to DB
  • returns shortened urls on request


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.


URL DB Service

SQL DB handling reading from and writing to the shortened URL table.


Schema:

full_url

shortened_url

user_id


Additional notes:

  • Read-heavy so use read replicas
  • Cache requests to prevent load - prioritise by most recently added (LIFO) as well as most-used
  • As system scales, can be sharded by popular hosts


URL generation service

  • Checks if URL DB service already has URL for user
  • If not, generates a shortened version
  • Calls URL DB service to insert shortened URLs with shortened version