Education logo

Designing a URL Shortener

Make Links Short & Sweet โœ‚๏ธ๐Ÿ”—

By Sreya SatheeshPublished 11 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.