Requirements

Functional

NonFunctional

Entities

API

  1. POST /url
    input:
{
   original url,
   customized code (optional),
   expiration date (optional)
}

output:

{
   short url
}
  1. GET /{shortcode}

302 redirect to original url

High level design

image

Deep dive

Uniqueness of url

Use hash function or random string, if worry about conflict, we can either regenerating random number or adding salt when generating hash, or we can add username in the url (like namespace).

For short url, we should use B62 encoding, and truncate the string generated to a small number of characters.

The global counter may work but adds extra service in the system, and it may fail and add more complexity there.

Fast redirect

First the DB query should be fast, we can add index for short code, besides we can cache the short code in memory, and use LRU cache policy. We can also utilize CDN, some CDN providers provide some basic computing at the Edge so we can just redirect there.

scale 100M users

Assume each user uses the redirect service 10 times per day, and create 1 short url per day:

QPS:

Split into read/write services

record size:

storage (1 year)

we can use nosql db like DynamoDB which can:

image