Q&A-1Explain the mapping, not the hash and how it changes a production decision.
Answer
The mapping is the product: a row (key, long_url, owner, created_at, expires_at, status) with a unique index on key, plus a read path that resolves it. The hash is a function that can only mint a candidate key: it cannot revoke, re-point, or name a link. That distinction decides four production questions at once: how keys are generated, whether custom aliases are possible, how takedowns work, and how long you can cache.
| verb | path | store op | response |
|---|---|---|---|
| POST | /v1/links | INSERT ... ON CONFLICT DO NOTHING | 201 {key, short_url} |
| GET | /{key} | SELECT long_url WHERE status='active' AND expires_at > now() | 302 Location + Cache-Control |
| DELETE | /v1/links/{key} | UPDATE status='gone' | 204 |
Key generation. Hash the URL and truncate to 6 base62 chars and you get 62^6 ≈ 5.7e10 keys. At 1e9 links the chance a fresh insert collides is n/N ≈ 1.7e-2: about 1.7% of writes hit the unique index and need a retry. At 7 chars (62^7 ≈ 3.5e12) it drops to 2.8e-4. So the hash does not remove the store; it moves the collision check into it. The decision: size the key from your expected key count, not from the digest's width.
Custom aliases. A user wants /acme-launch. A hash cannot produce it; the mapping can, with the same unique index plus a reserved-word denylist (/api, /admin, /health). This is the decision that most often flips a team from hash-derived to random-or-counter keys.
Takedown and re-pointing. A legal takedown or a marketing re-point is an UPDATE on the row. A hash-derived key is immutable by construction, you cannot un-mint it, so you need the mapping anyway.
Failure modes, with detection:
- Collision retry storm. Key length too short means the insert path retries and latency climbs. Detect: count unique-violation errors per minute; alert when retries exceed ~0.1% of writes. Cost: each retry is an extra round trip (~1 ms local, 5–20 ms cross-AZ).
- Alias shadowing. Someone registers
/adminbefore the denylist ships. Detect: a write-time denylist check plus a synthetic probe that GETs reserved paths and expects 404. Cost: a route shadow that can 302 users off-site. - Stale negative cache. A 404 cached for a day means a key created a minute later still 404s. Detect: keep the negative TTL short (10 s) and the positive TTL long, and measure the miss-then-create gap. Cost: a link that works for its creator and 404s for everyone else.
- Expiry not enforced on read. The SELECT must filter
expires_at; a cache serving a 302 past expiry is a legal problem. Detect: a periodic job that GETs expired keys and expects 404.
Trade-off: hash-derived keys are idempotent. The same URL yields the same key, which dedupes and saves rows. Random keys are not, so you either accept duplicates or add a unique index on (long_url, owner). Counter keys are shortest and collision-free but leak volume and need a range allocator (allocate 10k IDs per node) to avoid one hot sequence.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.