Skip to content

Short links: identifiers, redirects and data placement

11 min reading

A baseline short-link design with collision and redirect requirements

Page details

System Design · Compose complete systems

11 min reading · 26 min with practice

Sources

Published · Last edited:

Prerequisite: Checkpoint: review a production-ready design. For basics, use the independent System Design foundations.

More
20px

Hypothetical scenario. Northwind’s marketing team requests short links. Dev draws a token-to-destination table. Maya asks for owner creation and withdrawal; Priya adds a public visitor following the link. The same mapping now participates in actions with different authority.

Dev: “The table can locate the destination.”

Priya: “What may an owner change, and what does a visitor’s redirect promise?”

Build a coherent baseline from a declared brief, identifiers, lifecycle and selected workload. This is a new case, not another version of the catalogue rates. Leave preview fetching and geographically customized destinations outside the initial scope rather than quietly designing for them.

Owners create a link for a destination, inspect their links and withdraw future resolution. Visitors follow an active link without inheriting owner permissions. The initial policy treats destination edits as a separate authorized change requiring an expected mapping version; it does not silently merge competing edits.

The service controls its own future lookup responses. It cannot make a previously delivered destination disappear from a visitor’s memory. That limitation belongs in the product promise before a cache or redirect status is selected. Reporting begins as a separately described derived path; if events later become billable, the accounting contract must be redesigned explicitly.

One table does not answer every contract

A table with token and destination is a reasonable beginning. It becomes insufficient when the design silently assumes that a read replica is current, every cache honors immediate withdrawal, and every redirect event reaches analytics exactly once.

Split requirements by consequence. Losing an analytics event changes reporting; assigning one token to two destinations breaks the link's identity. A stale destination after withdrawal has a different risk from a delayed dashboard count. These requirements should not share an accidental best-effort policy.

Begin with a modular service and one authoritative database if it meets the chosen workload. Separate mapping, authorization and analytics boundaries in the design without requiring independently deployed microservices. Extract a component when isolation, ownership or measured scaling requirements justify the distributed recovery burden.

Trace creation into an authoritative redirect

Hypothetical scenario. A tenant creates a short link under business operation K. The service authenticates the owner, checks destination policy and atomically claims a unique token with its mapping and operation result. The redirect path resolves that token, checks active state and returns the selected HTTP redirect behavior. Creation and redirect have different authority and workload needs.

Trace creation into an authoritative redirect — selected design evidence
Record / keyExample fieldsTransition owner
Create operation / tenant + Kfingerprint, token, resultCreation transaction
Mapping / tokendestination, owner, active, versionMapping authority
Allocation range / range IDowner epoch, next, endAllocator when used
The write path establishes a recoverable mapping; the read path applies active-state and redirect policy.The write path establishes a recoverable mapping; the read path applies active-state and redirect policy. Client to Link service: 1 · create with K; Link service to Mapping store: 2 · unique token + mapping; Mapping store to Link service: 3 · recoverable result; Visitor to Link service: 4 · resolve token.ClientLink serviceMapping storeVisitor1 · create with K2 · unique token + mapping3 · recoverable result4 · resolve token
The write path establishes a recoverable mapping; the read path applies active-state and redirect policy. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

A random token needs collision detection and retry at a uniqueness boundary. Allocated ranges reduce coordination on each allocation but need exclusive range ownership, exhaustion handling and recovery after restart. Neither token order nor a timestamp proves that a creation intention was accepted once.

A lost creation reply is recovered by K, returning the same mapping even after routing changes. Withdrawals advance mapping version and carry invalidation intent. Browser and intermediary caches remain separate boundaries; a service-side tombstone cannot retract an already cached permanent redirect. Select redirect and caching semantics against the withdrawal promise before optimizing lookup traffic.

Trace creation, redirect and withdrawal

Creation authenticates the owner, validates destination policy, assigns a candidate token and atomically inserts a unique mapping. A repeated creation attempt carries a stable operation identity. A conflicting token candidate is retried under a bounded allocation policy; the uniqueness constraint decides ownership rather than an optimistic precheck.

Separate authoritative mapping from derived redirect reportingOwners create or withdraw mappings through an authorized service and authoritative store. Visitors use a bounded cache and mapping lookup. Redirect events flow asynchronously to aggregate reporting, whose failure must not change token identity.Owner operationAuthorizedmapping APIUnique mappingVisitor redirectCache / lookupEvent reportingderived stateWithdrawals require an explicit end-to-end visibility contract.

Redirect lookup checks the mapping's active state and returns the selected HTTP redirect semantics. RFC 9110 defines redirect status behavior; the service must choose intentionally rather than assuming all redirects have identical method and caching consequences.

Withdrawal changes authoritative state and records invalidation intent. Internal cache version checks prevent an older refill from replacing a newer withdrawn state. Browser or intermediary caching is a separate boundary governed by the actual response and cache semantics described in RFC 9111. Promise immediate withdrawal only if the complete served path can enforce it.

Northwind’s first mapping authority is a Postgres table with a unique token index. A Redis cache serves permitted reusable lookups; the creation transaction binds owner, request identity, destination and token. The public redirect path uses a narrow token lookup rather than the owner’s search interface. Administrative creation and withdrawal require authorization even when following an active public link does not.

Random token allocation needs both candidate generation and atomic uniqueness. A process cannot reserve a token by observing that it was unused a moment earlier. Let the unique insertion decide, then select another candidate after a confirmed collision within a bounded retry budget. The same request identity distinguishes collision recovery from a second user-created link when a response goes missing.

Derive the first capacity model

Hypothetical scenario. Choose 100,000 new mappings per day and ten million redirect requests per day. Mean creation demand is 100,000 ÷ 86,400 ≈ 1.16 requests per second; mean redirect demand is 10,000,000 ÷ 86,400 ≈ 115.74 per second. Choose a planning peak multiplier of ten for redirects, yielding approximately 1,157.4 per second. That multiplier is an assumption to validate, not an industry constant.

With a chosen logical mapping payload of 300 bytes and 365 days of retention, payload is 100,000 × 365 × 300 = 10,950,000,000 bytes, or 10.95 GB. Three full copies require 10.95 × 3 = 32.85 GB of that payload. Indexes, WAL, backups, allocation, deleted records and metadata are additional.

Choose a hypothetical cache hit fraction of 0.9. Using that rounded planning peak, lookup misses are 1,157.4 × (1 − 0.9) = 115.74 per second. During an empty-cache recovery they can approach the full 1,157.4, before retries and refill amplification. Size and admission decisions must consider that path, not only the warm-cache mean.

A ten-character token over an alphabet of 62 symbols has 62¹⁰ = 839,299,365,868,340,224 possible strings. This namespace size does not guarantee collision-free random allocation or confidentiality. For a separate toy namespace of 100 values with ten already assigned, a uniform next candidate collides with probability 10 ÷ 100 = 0.1. Always enforce uniqueness; use a suitable unpredictable identifier when enumeration is a concern.

Finally, choose 100 bytes of logical analytics payload per redirect. Ten million events produce 10,000,000 × 100 = 1 GB per day before stream and storage overhead. The reporting path can exceed mapping growth even though it is not authoritative business state.

Bitly’s official tutorial distinguishes shortening, custom domains and customized back-halves. It makes owner and group context relevant to creation. This is evidence about a public product contract, not a claim about Bitly’s internal storage implementation.

Hypothetical failure scenario. PostgreSQL accepts a unique mapping and its operation result. The creation reply is lost. A client starts another creation with a new operation identity and receives a different token for the same destination. Both tokens are valid, but the client intended one creation.

Retain the original logical identity and inspect its recorded result. A candidate-token collision is different: select another candidate within the same bounded creation operation. The unique insertion establishes token ownership, while the operation record establishes which token belongs to the repeated intention.

Use one indexed mapping authority, a cache whose reuse matches the withdrawal contract and asynchronous reporting with declared completion semantics. Separate those responsibilities in the design without requiring independently deployed services. The selected payload and rate estimates do not establish a hardware limit or prove that sharding is needed.

Measure source lookup cost, write contention, cache eligibility and reporting progress. Owner listings use a different access pattern from exact public-token lookup. If either becomes a bottleneck, optimize that path against its actual contract. Do not burden every redirect with a search index merely because owners need a searchable dashboard.

The next page keeps this baseline and changes its pressure conditions. A hot link, cold cache and abusive destination will test whether the chosen boundaries remain coherent. The target is an explained evolution, not another unrelated infrastructure diagram.

Challenge the proposed mechanism

Withdrawn state is retained evidence; the redirect path cannot infer activity from token existence alone.Withdrawn state is retained evidence; the redirect path cannot infer activity from token existence alone. Create K pending to Mapping accepted: unique claim + result; Mapping accepted to Withdrawn version: owner-authorized change.Create K pendingMapping acceptedWithdrawn versionunique claim + resultowner-authorized change
Withdrawn state is retained evidence; the redirect path cannot infer activity from token existence alone. Enlarge the diagram to inspect its labels; the surrounding records and explanation give the text alternative.

Creation times out and the client switches regions. What prevents two tokens for the same accepted intention?

Reveal the design reasoning

Resolve K at its authoritative tenant scope before allocating again. A per-region random generator prevents neither duplicate business intentions nor conflicting ownership. Document regional forwarding, unavailable behavior and recovery of the original result.

Decision and reconsideration: Begin with conditional mapping creation and tenant-scoped operation identity. Add range allocation only when its ownership and recovery cost is justified.

  1. A candidate token already exists. Should the caller create a new operation identity?

    Keep allocation inside the same intention

    No. A different candidate can be tried within the bounded creation operation. A collision does not mean the owner requested another link.

  2. May public redirect callers edit the destination because they know its token?

    Separate lookup from ownership

    No. Public lookup and owner modification are different capabilities. Authenticate and authorize the edit against the mapping’s owner scope and version.

  3. Does a ninety-percent warm cache hit fraction establish source capacity?

    Inspect the cold path

    No. The selected miss estimate omits cold-cache recovery, refill duplication, retries and writes. Size or admit that work separately before relying on the warm fraction.

The baseline explains normal creation and resolution. Page 42 keeps those identities fixed while a single link becomes hot, caches restart and an owner withdraws it after an abuse report.

Keep token ownership and creation identity at one authoritative boundary.
Choose redirect and withdrawal semantics before cache reuse.
Next, evolve this design under hot-key pressure, abuse and recovery.