Education logo

Designing a URL Shortener

Make Links Short & Sweet βœ‚οΈπŸ”—

By Sreya SatheeshPublished 9 months ago β€’ 3 min read

The Long Story (Literally)

Your friend sends you a link to their portfolio:

https://www.thisisaverylongurl.com/users/portfolio?userId=9xk34&ref=linkedin

Looks like a digital snake 🐍. You squint at it, sigh, and say:

β€œWhy not shorten that?”

Boom πŸ’₯ β€” now it's:

bit.ly/myfriend

Looks clean. Feels good. And today, you're going to learn how to build something just like that.

Let’s design our own URL shortener β€” a mini Bit.ly β€” from scratch, system design style.

🎯 What Is a URL Shortener?

A URL shortener is a service that takes a long, hard-to-remember link and turns it into a short version.

Think:

πŸ‘‰ Long URL: https://amazon.com/product/deal/123xyz456

πŸ‘‰ Short URL: amzn.to/deal

When someone clicks the short link, the system redirects them to the original long URL.

Services like Bit.ly, TinyURL, and Twitter’s t.co do exactly this.

🧩 Step 1: Define the Requirements

Before jumping into code or architecture, ask:

πŸ’‘ β€œWhat does this system need to do?”

βœ… Functional Requirements

  • Take a long URL, return a short one
  • Redirect short URLs to the original ones
  • Bonus features:

- Custom aliases (short.ly/myportfolio)

- Expiration dates

- Click analytics

🧱 Non-Functional Requirements

  • High availability β€” links should never break
  • Low latency β€” redirections must be fast
  • Scalability β€” must handle billions of links
  • Reliability β€” data should never be lost

πŸ”’ Step 2: Generating Short URLs

This is the core of the system. Here are two popular methods:

✨ Method 1: Hash the URL

Use hashing (like MD5 or SHA-256), and take the first 6–8 characters.

  • βœ… Easy to implement
  • ❌ Risk of collisions
  • ❌ Not ideal for versioning or expiration

πŸš€ Method 2: Counter + Base62 (Recommended)

  • Maintain a global auto-incrementing counter
  • Convert the counter to Base62 (0–9, a–z, A–Z)
  • Use that as the short code!

πŸ’‘ Base62 gives you 62⁢ = 56.8 billion combinations with just 6 characters!

Example:

Counter = 123456 β†’ Base62 = abc123

πŸ—ƒοΈ Step 3: Database Design

We need a place to store the mappings between long and short URLs.

Table: url_mappings

βš™οΈ Step 4: Workflow Breakdown

βœ‚οΈ a. Shortening a URL

  1. User sends a POST request with a long URL
  2. Backend increments counter β†’ converts to Base62
  3. Stores mapping in database
  4. Returns: https://short.ly/abc123

πŸ” b. Redirecting a URL

  1. User visits https://short.ly/abc123
  2. Backend looks up abc123 in DB
  3. If found (and not expired), redirect to long URL

⚑ Step 5: Make It Fast with Caching

Redirection is read-heavy. You don’t want to hit the DB every time.

Use Redis or Memcached:

  • Cache hot short codes like abc123 β†’ original URL
  • On miss: fetch from DB and populate cache
  • On expiration: remove from both cache and DB

🌍 Step 6: Make It Scale

When traffic grows, your system must grow too.

Techniques:

  • Sharding: Split DB into smaller parts
  • Replication: Use read replicas for redirects, master DB for writes
  • Load Balancing: Spread requests across multiple servers
  • CDN: Use edge locations for faster redirect responses
  • Rate Limiting: Stop bots from spamming millions of links

🎁 Bonus Features

1. Expiration Dates

  • Add TTL to each short URL
  • Delete or mark invalid after expiration

2. Custom Aliases

  • Let users choose:
  • short.ly/sreya-portfolio
  • Check if alias already exists before saving.

3. Analytics

Track:

  • Number of clicks
  • Geo-location
  • Browser/device type

Use tools like Kafka, Spark, or BigQuery for scalable analytics pipelines.

🏁 Wrapping It All Up

And that’s a wrap on URL shorteners! Here’s what you’ve learned:

  • Generate Short Links Efficiently with Base62 encoding and a counter for fast, scalable links.
  • Ensure Speed and Reliability by caching popular links and reducing database hits for quicker redirection.
  • Scale for Growth through sharding, replication, and load balancing to handle millions of requests.
  • Add Extra Features like custom aliases, expiration dates, and analytics to make your service stand out.

stemcourses

About the Creator

Sreya Satheesh

Senior Software Engineer | Student

https://github.com/sreya-satheesh

https://leetcode.com/u/sreya_satheesh/

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

Sreya Satheesh is not accepting comments at the moment
Want to show your support? Send them a one-off tip.

Find us on social media

Miscellaneous links

  • Explore
  • Contact
  • Privacy Policy
  • Terms of Use
  • Support

Β© 2026 Creatd, Inc. All Rights Reserved.