In this lesson · topic 2 of 7
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 /linkswith 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 aLocationheader, 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.