API and data model
An API is the service's contract with its clients: which requests it accepts and what it answers. The data model is what the service stores to keep that contract. In an interview both parts take about five minutes, and from them the interviewer checks whether you understood the requirements.
It helps to think of a shortener as a cloakroom. You hand in a coat and get a numbered tag; you show the tag and get the coat back. The tag here is the short code, the coat is the long URL, and the numbered rack is the table where a code leads to a URL. The analogy is off in one way: a cloakroom takes tags back and hands them out again, while our code is never handed out a second time.
Four calls
The requirements from the first lesson fit into four calls:
POST /api/links {url, alias?, expiresAt?} → 201 {code, shortUrl}
GET /{code} → 302, Location: long_url
DELETE /api/links/{code} → 204
GET /api/links/{code}/stats?from=&to= → [{day, clicks}]Creating a link
POST /api/links takes a long URL plus an optional alias and expiry date. The 201 answer returns the code and the ready short link. If the alias is taken, the service answers 409, and for a malformed URL, 400. Only a signed-in user can create links.
Redirect
GET /{code} lives at the root of the domain, not under /api: the short link itself is this request. The service answers 302 with the long URL in the Location header, and the browser follows it on its own. If the code does not exist, the link has expired or it was deleted, the answer is 404.
Deletion and statistics
DELETE /api/links/{code} deletes the link and answers 204. There is no call to change the URL: the requirements forbid pointing a link at another address.
GET /api/links/{code}/stats returns the number of redirects per day for the chosen period. Both calls are open only to the link's owner.
The links table
The main table ties a code to a URL:
| Column | Type | What it holds |
|---|---|---|
code | text, key | seven characters or a custom alias |
long_url | text | the long URL |
owner_id | uuid | the owner |
created_at | timestamp | when it was created |
expires_at | timestamp | expiry date, may be empty |
deleted | bool | whether the link was deleted |
The table is split into partitions by a hash of code, and each partition has three replicas. Each redirect is one lookup by key, so no joins are needed.
Aliases live in the same key space as codes. So an alias cannot take a code that was already handed out. And a generated code can match a seven-character alias. So it is written with a conditional INSERT … IF NOT EXISTS: if the key is already taken, the service skips that code and takes the next counter value.
The owner's links
The links table is split by code, and finding all of one person's links in it takes a full scan. So next to it sits owner_links with the columns owner_id, created_at and code, partitioned by owner_id. The code is part of the key to tell apart two links created in the same instant.
Click counts
The click_counts table holds one row per link per day: code, day, clicks. The click aggregator writes to it, and the link service reads the owner's statistics from it.
The code counter
The code_counter table lives in a small database of its own in each region. It has one row: the region and the next free counter value. Short codes are made from this value.
Common mistake
Keeping the redirect count right in links and increasing it on every redirect:
UPDATE links SET clicks = clicks + 1 WHERE code = ? ✗Then every redirect turns into a write: 4,000 writes a second on average, and up to 10,000 into the same row of a viral link. The redirect waits for the database and misses the 50 ms target. Counting is moved off the redirect path: an event goes to a queue, and the aggregator writes to click_counts.
Find this mistake on a diagram and fix it.
The owner needs clicks per day, and a redirect must take under 50 ms at 4,000 redirects a second. Right now the diagram gives neither. Fix it.
Key points
- Four calls: create, redirect with a
302answer, delete and statistics; the URL cannot be changed. linksis a lookup by thecodekey, partitioned by its hash, with three replicas.- Aliases and generated codes share one key space.
- Redirects are counted per day in a separate table, not in the link's row.