Acortador de URLs

TinyURL / bit.ly

Escrito por Darjus Kazakevic · revisado el

A unique ID in base62, and a cache in front of a key-value store.

Users submit a long URL and get a short link, such as sho.rt/2crtgcW. Anyone who opens the short link gets an HTTP redirect to the long URL. Common extras are a custom alias, an expiry date and a click count per link.

A redirect must answer in under 20 ms at p99, and links must keep working when parts of the system fail, because a broken link breaks someone else's page. Codes should be short and hard to guess. Reads outnumber writes about 100 to 1, so you design the read path first.

En esencia — Give each URL a unique number, write it in base62 as a short code, and serve redirects from a cache, because reads outnumber writes 100 to 1.

Cuándo recurrir a él

Recurre a él cuando

  • You map a short key to a value, and reads outnumber writes 100 to 1
  • You need short, unique, public IDs, like invite codes or share links
  • A lookup must answer in milliseconds, and a cache can hold the hot keys

Reconsidéralo cuando

  • Links must stay private: short codes can be scanned, so use long tokens
  • Writes are as common as reads, so a read cache helps very little
  • You store a few thousand links: one Postgres table is the whole design

The components

ID generator and encoder (uses unique ID generation)
Hands out a unique number for each new link, without one central lock. The encoder writes that number in base62 (digits plus both letter cases), which gives the short code.
Links store (uses database sharding)
A key-value table from code to long URL, plus the owner and an expiry date. Every lookup is by code, so it shards cleanly on the code: DynamoDB, Cassandra, or Postgres split by code range.
Redirect cache (uses a distributed cache)
Redis in front of the store, filled on a miss (cache-aside). Clicks follow a power law: about 20% of links get 80% of the traffic, so a cache of about 10 GB answers most redirects.
Redirect service (uses a load balancer)
Stateless servers behind a load balancer. POST /links creates a code. GET /{code} looks it up and answers 302 (temporary) or 301 (permanent), with the long URL in the Location header.
Click pipeline (uses a message queue)
Each redirect drops a small click event (code, time, country, referrer) on a queue such as Kafka. A separate consumer counts clicks, so analytics never slows the redirect.

Key decisions

Encode a unique ID; do not hash the URL
A hash cut to 7 characters collides once you store millions of links, so every write needs a check and a retry. A unique ID in base62 cannot collide, and 7 characters hold 3.5 trillion codes.
Use 302 when you need click counts
Browsers cache a 301 for as long as they like, so repeat clicks never reach you. A 302 costs one request per click but lets you count, expire and change links. Choose per product.
Keep analytics off the redirect path
Put a click event on a queue and return the redirect at once. Counting inside the request adds a database write to every click, and a slow analytics store becomes slow redirects.
Cache the hot links, not all of them
About 20% of links get 80% of clicks. An LRU cache (least recently used links are evicted first) of about 10 GB holds that 20%. The store only sees the rest.

Errores habituales

  • Hashing the URL and keeping the first 7 characters. It passes every test and starts to collide at a few million links, so two users get each other's redirects.
  • Encoding a raw counter. Codes that differ by one let anyone walk through every link. Scramble the ID with a one-to-one shuffle before you encode it. For example, multiply by a large number that shares no factor with 62, modulo 62^7.
  • Counting clicks inside the redirect. The analytics database sets the redirect's latency, and when it is slow, links feel broken.
  • Promising analytics on 301 redirects. Browsers cache a 301, so repeat clicks never reach the server and the counts are too low.