How URL Shortener works
Loading the interactive diagram…
URL Shortener, as text
Turns long links into short ones and redirects fast, with a cache in front of the database.
Clients
- Browser
- Client · Hypothetical. Someone creating a short link, or clicking one.
Gateways
- Load balancer
- Gateway · Hypothetical. Spreads requests across the service instances.
Services
- Shorten API
- Service · Hypothetical. Creates a short code for a long URL.
- ID generator
- Service · Hypothetical. Hands out unique numbers so two links never share a code.
- Redirect service
- Service · Hypothetical. Looks up a short code and redirects the browser.
Queues
- Click events
- Queue · Hypothetical. Records each click for analytics without slowing the redirect.
Caches
- Link cache
- Cache · Hypothetical. Keeps popular short codes in memory.
Databases
- Link database
- Database · Hypothetical. The durable record of every short code and its long URL.
Flow: Create a short link
From pasting a long URL to getting a short link back.
- Browser → Load balancer (Shorten this URL). Someone pastes a long URL and presses Shorten.
- Load balancer → Shorten API (Shorten this URL). The load balancer sends the request to one of the shorten API instances.
- Shorten API → ID generator (Next id?). The shorten API asks for the next unique number.
- ID generator → Shorten API (1048576). It gets a number no one else will get, and encodes it in base62 as a short code like aZ3k9.
- Shorten API → Link database (Save aZ3k9). It saves the short code together with the long URL.
- Link database → Shorten API (Saved). The database confirms the link is stored.
- Shorten API → Load balancer (Short link). The shorten API answers with the new short link.
- Load balancer → Browser (Short link). The browser shows the short link, ready to copy.
Flow: Follow a short link
From clicking a short link to landing on the long URL.
- Browser → Load balancer (GET /aZ3k9). Someone clicks a short link.
- Load balancer → Redirect service (GET /aZ3k9). The load balancer sends the click to one of the redirect service instances.
- Redirect service → Link cache (Look up aZ3k9). The redirect service checks the cache first.
- Link cache → Redirect service (Long URL). Most of the time the cache already has it, so no database call is needed.
- Redirect service → Load balancer (301 redirect). The redirect service answers with a redirect to the long URL. It also drops a click event on the queue for analytics, without waiting for it.
- Load balancer → Browser (301 redirect). The browser follows the redirect and opens the long page.
HowItScales is not affiliated with the companies shown. Components marked Assumption or Hypothetical are not confirmed by the company.