🎓 A1Learner
🔍
0 of 42 lessons completed

Design a URL Shortener: The Ask

A URL shortener is a service that stores a long link under a short code and sends each visitor from the code to the long link. This lesson clarifies the ask, the first step of the method.

The two calls

The shorten call stores a long link under a new short code. The redirect call looks the code up and returns the long link.

store = {}

def create(long_url):
    code = "s" + str(len(store) + 1)
    store[code] = long_url
    return code

def redirect(code):
    return store.get(code, "not found")

print("create:", create("https://example.com/a-very-long-article"))
print("create:", create("https://example.com/another-long-page"))
print("redirect s1:", redirect("s1"))
print("redirect s2:", redirect("s2"))

A redirect answers with a status

A redirect call answers with a status and a destination. This design works with status 302, and an unknown code answers 404, the not-found status.

destinations = {"s1": "https://example.com/a-very-long-article", "s2": "https://example.com/another-long-page"}

def redirect(code):
    if code in destinations:
        return 302, destinations[code]
    return 404, "not found"

print("s1:", redirect("s1"))
print("s9:", redirect("s9"))

Questions become decisions

Clarifying records decisions, not guesses. The printout shows each question with the answer the design will use.

decisions = [
    ("reads or writes more?", "reads"),
    ("custom codes allowed?", "yes, when the code is free"),
    ("links that end allowed?", "asked now, settled later"),
    ("redirect status?", "302 as the working choice"),
]

for question, answer in decisions:
    print(question, "->", answer)

Example

store = {}

def create(long_url):
    code = "s" + str(len(store) + 1)
    store[code] = long_url
    return code

create("https://example.com/first-long-article")
create("https://example.com/second-long-page")

visits = ["s1", "s2", "s1"]
for code in visits:
    print("visit", code, "->", store[code])
print("links stored:", len(store))
print("redirects served:", len(visits))

Key points

  • Short code: the short string that stands for one long link.
  • Redirect: the response that sends a visitor from a short code to its long link.
  • Shorten call: the API call that stores a long link and returns a short code.
  • Redirect call: the API call that looks up a short code and answers with a redirect.
  • Custom code: a short code the visitor chooses instead of an assigned one.
  • Status 301: a permanent redirect that browsers may store and reuse.
  • Status 302: a temporary redirect that browsers ask the service about again.
  • Open question: whether a link may end after a set time is asked now and decided in the last shortener lesson.

In this example, 2 links are created and 3 redirects are served. Each redirect returns the long link stored under its code, so the short code is the whole link between the two calls.

Note

this design uses 302 as its working choice, so visits stay countable, and the full choice is weighed in the last shortener lesson.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Why does this design work with status 302 for redirects?