AI Hints
Home›System Design Tutorial›Shortener Design and More
System Design · Lesson 94 of 120

URL Shortener: The Design

Putting it together.

⏱ 2 min read▶ Video at 1:53:29🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

We have requirements, estimates, and short codes. Now step three, the high level design. Two paths matter, creating a link, and following one.

URL Shortener: The Design
From the video: URL Shortener: The Design
  • Requirements, estimates, codes
  • Now the design
  • Create and follow

To create a link, the client posts the long URL. The API server gets a new unique number from an ID generator, and turns it into a short code. It saves the code and long URL in the database. And it returns the short link.

  1. POST long URL
  2. Get unique ID
  3. Base 62 code
  4. Save to DB
  5. Return short link
Creating a link
From the video: Creating a link

To follow a link, the browser requests the short code. The server checks the cache first. On a miss, it reads the database, and fills the cache. Then it answers with a redirect, status three oh one or three oh two, and the browser goes to the long URL.

  1. GET /abc123
  2. Check cache
  3. Miss: read DB
  4. Fill cache
  5. Redirect 302

Deep-dive choices

Some deep dive choices. A three oh one redirect is cached by browsers, which saves load, but hides clicks from our statistics. A three oh two is counted every time. The database is a key value store, sharded by short code. And popular links stay hot in a Redis cache.

keyvalue
301browser caches it, fewer requests
302every click is counted
Databasekey-value, sharded by code
CacheRedis for hot links

Summary

The shortener uses an ID generator and base sixty two to create links, and a cache in front of a sharded key value store to follow them. Next, a rate limiter as a shared service.

Key takeaways
  • ID → base 62
  • Cache, then DB
  • 302 to count clicks
Next up: Rate Limiter Service — a shared limiter