Try
↑ ↓ move↵ openesc close
In this lesson · topic 2 of 7
  1. 1. A short link is a tiny key-value store
  2. 2. Requirements, numbers, API and data model
  3. 3. Make codes and follow them in the lab
  4. 4. Random codes meet the birthday bound
  5. 5. Counter ranges, random codes or hashes
  6. 6. Hot links, caches and 301 against 302
  7. 7. The URL shortener in review

Topic 2

Requirements, numbers, API and data model

5 min read

Ask before you draw

Start with what the service must do. Short links for long addresses, redirect on request, a code that is short and hard to guess, links that can expire, and counts of clicks. Then ask what it need not do. Custom codes and editing a link after it is made can wait.

Ask the quality questions too. A link must never point to the wrong place. A redirect should be fast. A brief outage of link creation hurts less than an outage of redirects, because old links live in many places.

Back of the envelope

These are assumptions for the exercise. Say them out loud and let the interviewer change them.

  • 100 million new links a month. A month has about 2.6 million seconds, so that is about 40 creations a second.
  • Reads outnumber writes about 100 to 1. That is about 4,000 redirects a second on average. Plan for 10 times that at peak.
  • Keep links for five years. That is 6 billion links. At roughly 500 bytes a record, the table is about 3 terabytes.
  • Six characters from 62 symbols give 56 billion codes, which covers 6 billion links with room to spare. Seven characters give 3.5 trillion.

The numbers tell you the shape. Writes are small and a single database could take them. Reads are bigger and want a cache. Storage is large enough that you will want to split the table.

The API

Two calls cover the core.

  • POST /links with the long address in the body. It returns the short code. Give it an idempotency key if clients may retry, so a retry returns the same code. See idempotency and the outbox.
  • GET /{code} answers with a redirect and a Location header, or a not found.

The data model

One table does the work: code, long address, created time, expiry, owner. The code is the primary key. Lookups are always by code, so there is no need for joins or range scans.

Click counts are a different workload. They are many small increments. Keep them in a separate counter store or an event stream, not in the link row. See queues as buffers.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms