skip to content

For a short-link service issuing 100 million links a month, how do you pick a code scheme and size the code length?

level: middleimportance: must knowfreq 80%

answer

  1. alphabet size to the power of length
  2. unique by construction versus checked on insert
  3. consecutive codes can be walked
  4. size for lifetime links, not one month
  5. fill fraction equals retry rate

basics

~20 s

A base62 counter gives short codes that are unique by construction but guessable; hash-and-truncate and random codes need an insert-time check. Size the length so lifetime links fill a tiny fraction of the keyspace: seven characters here.

solid answer

~40 s

There are three schemes. A counter written in base62 never collides, but it needs one sequence shared by the fleet and makes codes enumerable. Hash-and-truncate is deterministic, but truncation discards the hash's collision resistance, so it still needs a check and a salted retry. Random codes are unguessable and stateless, but each create needs an `insert-if-absent` write. For length, size for lifetime volume: 100 million a month over ten years is about 12 billion links. Six characters give about 56.8 billion codes, about 21% full by then, so one random draw in five would collide; seven give about 3.52 trillion, about 0.34% full. I would pick random seven-character codes, or a pre-generated pool if the check becomes a cost.

go deeper

for a junior

Know that a code of length n over a 62-character alphabet has 62^n possible values, and that a short-link service looks the code up and redirects to the stored long URL.

for a middle

Compare counter, hash-and-truncate and random codes by uniqueness, guessability and shared state, then size the length from lifetime links: 12 billion against 56.8 billion or 3.52 trillion.

for a senior

Tie the fill fraction to the per-insert retry rate, keep the write conditional, and monitor retries as the early warning for lengthening codes years before it bites.

for a principal

Frame the scheme as a product exposure call: enumerable codes leak volume and unreleased links, while random codes or a pool cost a check or a component to run.

## The decision A short-link service has one write path (turn a long URL into a short code) and one hot read path (look the code up and redirect). The **short code** is the key of the link record and the only part of the short URL the service controls, so two choices are made together: **how a new code is produced** and **how many characters it has**. The scenario here is a public service behind a marketing platform creating about **100 million links a month**. The usual alphabet is **base62**: the digits `0-9`, lower-case `a-z` and upper-case `A-Z`, all safe in a URL path. A code of length *n* can take 62^n values; that set is the **keyspace**. ## Three ways to produce a code (and a fourth that fixes the third) | scheme | how a code is made | uniqueness | guessable from a neighbour? | length over time | |---|---|---|---|---| | **Counter in base62** | take the next integer and write it in the 62-character alphabet | unique by construction, no check | yes, consecutive links get consecutive codes | grows: up to five characters until about 916 million, then six, then seven | | **Hash-and-truncate** | hash the long URL (plus any salt) and keep enough of the digest for *n* characters | must be checked on insert | no, but anyone can compute a URL's first candidate | fixed | | **Random** | draw *n* characters from a secure random source | must be checked on insert | no | fixed | | **Pre-generated pool** | random codes made ahead and handed to servers in batches | made distinct when generated | no | fixed | What each one costs: - **Counter.** No collisions and the shortest possible codes, but the counter must never give one value to two servers. How a fleet shares a sequence is the general distributed-identifier problem; here it is simply one more component to run. The codes are **enumerable**: anyone can walk `...aZ7`, `...aZ8` and visit every link, and the code reveals roughly how many links exist. For a public marketing service whose links may point at unreleased pages, that is usually disqualifying. - **Hash-and-truncate.** Deterministic, which some teams like: the same input always gives the same first candidate. But truncation throws away the hash's collision resistance. Seven base62 characters hold about 41.7 bits, and a 41.7-bit slice of any hash collides like any random 41.7-bit value. It still needs an insert-time check and a retry that changes the input. - **Random.** Not guessable, fixed length, no shared state between servers. Each create needs an **insert-if-absent** write on the code, and a retry when it loses. - **Pre-generated pool.** Random codes generated and de-duplicated ahead of time, then handed out in batches, which takes the uniqueness check and the retry loop off the request path at the price of running a small key service. ## Sizing the length The rule is to size for the links the service will **ever** issue, not this month's: 1. **Lifetime links.** 100 million a month for an assumed ten-year horizon is 100M x 12 x 10 = **12 billion** links (the horizon is a design assumption; state yours). 2. **Keyspace per length.** 62^6 is about **56.8 billion**; 62^7 is about **3.52 trillion**; 62^8 is about **218 trillion**. 3. **Fill fraction** = links issued / keyspace. Six characters reach about **21%** after ten years; seven reach about **0.34%**. 4. **What the fill means.** For a checked scheme (hash or random), a fresh candidate lands on a taken code with probability equal to the fill fraction, so the fill **is** the retry rate. At 21% one create in five retries by year ten; at 0.34% about one in 290. This is a per-insert number, not the chance that *some* pair of codes ever collides, which is a different calculation covered with probability. A shortener checks every insert, so the per-insert rate is the one that sets its cost. A case-insensitive alphabet (digits plus one case, base36) avoids codes that differ only by case when read aloud or typed, but pays in length: 36^7 is about 78.4 billion, roughly 15% full at the ten-year mark. ## What to choose for this service - **Seven-character random codes with an insert-if-absent write.** Fixed length, not guessable, retries vanishingly rare for decades at this volume. - **A pre-generated pool** if the conditional write or retry becomes a measurable cost, or the team wants creates to be a constant-time hand-out. - **A counter** only where guessable codes are acceptable, for example an internal tool whose links are not secret, and where the shortest codes matter more. - **Monitor the retry rate.** It tracks the fill fraction directly and tells you, years ahead, when to start issuing eight-character codes; old codes keep working because lookups key on the whole string. ## What the length does not settle - **Secrecy.** A short code is a public identifier, not a secret; sizing a random value's entropy for a secret token is a separate topic. - **Lookups of codes that do not exist.** Scrapers probing random codes is a caching and penetration concern, not a reason to change the scheme. - **Request arithmetic.** About 40 creates a second on average (derived from the scenario) is light; the length decision is driven by lifetime volume, not rate.

  • Why not start with six characters and lengthen the codes later?
    You can, because lookups key on the whole string and six- and seven-character codes coexist. The cost is a retry rate that climbs toward one create in five before the switch, plus a monitored threshold someone must act on. At seven characters the fill stays under 1% for decades at this volume, so most teams start there and skip the migration.
  • Why can hash-and-truncate not skip the uniqueness check, given that cryptographic hashes are collision resistant?
    Collision resistance belongs to the full digest. Cut to seven base62 characters, about 41.7 bits, the output collides like any random 41.7-bit value, so two different URLs can land on one code at a rate set by the keyspace fill. Only an insert-time check notices, and the retry must change the input, because rehashing the same URL returns the same code.
  • When is a base62 counter the right choice despite enumerable codes?
    When links are not secret and the shortest codes matter: an internal tool, or links whose targets are public anyway. It removes the collision check entirely. You still run a sequence that never repeats across servers, and anyone can count your links and walk every code, so a public marketing service with links to unreleased pages usually rules it out.

  • Truncated hash codes never need a uniqueness check because hashes are collision resistant.
  • Six characters are plenty because 62^6 dwarfs this month's 100 million links.
  • Random codes are effectively unique, so the create path can write without a conditional insert.
  • Writing a counter in base62 hides how many links exist and makes codes unguessable.
  • Checking that a code is free with a read, then inserting, is enough with many servers.