Sub topic 1
Clarifying Requirements and Scope
Prerequisites
Assume you understand basic HTTP methods, status codes, and headers. You should also know the basics of relational or key-value databases, hashing, number bases, and simple Python.
Clarifying Requirements and Scope
A URL shortener maps a long URL to a short code. A client can then request the short URL and receive a redirect to the original address.
Start by separating functional requirements from non-functional requirements.
Functional requirements:
- Create a short link from a valid long URL.
- Resolve a short code to its long URL.
- Support optional expiration times.
- Reject duplicate custom aliases.
- Record basic click information if analytics are required.
Non-functional requirements describe system behavior:
- Reads are much more frequent than writes.
- Redirects should have low latency.
- Short codes must be unique.
- The service should remain available during a database replica failure.
- Abuse controls must limit automated creation and unsafe destinations.
For a first version, assume seven-character generated codes, no user accounts, optional expiration, and asynchronous analytics. These assumptions keep the core redirect path small. Change them when product requirements become clearer.
Estimating Traffic, Storage, and Code Length
Suppose the service creates 100 links per second and receives 100 redirects for every creation. That produces:
- Writes: 100 requests per second.
- Reads: 10,000 requests per second.
- Daily writes: 8.64 million links.
- Daily reads: 864 million redirects.
A seven-character Base62 code has 62^7, or about 3.5 trillion, possible values. This is more than enough for billions of links, provided that deleted or expired codes are not reused carelessly.
Estimate storage by multiplying rows per day by average row size. If each row uses about 500 bytes, 8.64 million new rows require roughly 4.3 GB per day before indexes and replication. Retention rules can reduce this total.
Designing the API
Use separate operations for creation and resolution:
| Operation | Method and path | Result |
|---|---|---|
| Create link | `POST /v1/links` | Returns a short URL |
| Resolve link | `GET /{short_code}` | Returns an HTTP redirect |
A creation request might contain a JSON body with long_url, custom_alias, and expires_at. The server should validate the URL, enforce length limits, and return 201 Created with the generated link. Resolution should return 404 Not Found for an unknown or expired code.
Base62 Encoding and Decoding
Base62 uses ten digits, 26 uppercase letters, and 26 lowercase letters. It represents an integer compactly by repeatedly dividing by 62. Decoding reverses the process.
ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def encode(n: int) -> str:
if n == 0:
return ALPHABET[0]
digits = []
while n:
n, remainder = divmod(n, 62)
digits.append(ALPHABET[remainder])
return "".join(reversed(digits))
def decode(code: str) -> int:
value = 0
for character in code:
value = value * 62 + ALPHABET.index(character)
return value
code = encode(123456)
print(code, decode(code))Exercise
Why can a seven-character Base62 code support more than seven billion links?
Show answer
It has 62^7 possible combinations, which is about 3.5 trillion.