How Does a URL Shortener Work?

ByteByteGo · Beginner ·🏗️ Systems Design & Architecture ·11mo ago

Key Takeaways

The video explains how URL shorteners work, including generating short URLs and handling redirects, using techniques such as hashing, base62 conversion, caching, and database scaling.

Full Transcript

Ever wonder what happens when you click a tiny URL? You know those short links like Bitly and Tiny URL that somehow know exactly where to take you? Today, we're going to reverse engineer how URL shorteners actually work. And trust me, there's way more going on behind the scenes than you might think. So, what exactly does a URL shortener do? Simple. It takes a massive URL like this Amazon link that goes on forever and turns it into something short and clean. Click the short version and boom, you end up at the same place. But here's where it gets interesting. Let's say you're building the next Bitly. How many URLs do you think we'll need to handle? Try 100 million new URLs every day. That's over a thousand new short links created every single second. And since people click links way more than they create them, you're looking at over 10,000 clicks per second. Now think about storage. Over 10 years, that's 365 billion URLs you need to keep track of. Just storing the URLs themselves would take 36 terabytes. So the real question becomes, how do you even generate these short URLs? This is where the math gets fun. Short URLs can use numbers and letters. There's 62 possible characters 0 through 9, lowerase A through Z, and uppercase A through Z. But how short can we make them? Let's work backwards. We need 365 billion unique combinations. With one character, you get 62 possibilities. With two characters, 62 squared. That's about 3,000. With three characters, 62 cub about 238,000. Keep going, you get to seven characters. 62 to the 7th power. That's 3.5 trillion possible combinations. Way more than we need. So, seven characters it is. But how do we actually create them? There are two approaches and they couldn't be more different. The first approach just hash the long URL. Take any hash function like MD5, run the long URL through it and you get back a long string of random characters. The problem that string is way too long. Even the shortest hash give you 32 characters when you only want seven. So you take the first seven characters and call it a day. But wait, what happened when two different URLs give you the same first seven characters? You've got a collision. Now you're stuck. You have to try again with some variation of the original URL until you find seven characters that nobody else is using. Every time you want to create a short URL, you have to check if those seven characters are already taken. That's a lot of database lookups. The second approach is way more elegant. Instead of hashing, you just count. Every time someone wants to shorten a URL, give it the next number in sequence. URL number one, number two, number three, and so on. Then converted number to what's called base 62. Here's how that works. Let's say you're on URL number 11,157. To convert to base 62, you divide by 62 over and over, keeping track of the remainders. 11157 / 62 is 179, remainder 59. 179 / 62 is 2, remainder 55. 2 / 62 is zero, remainder 2. Now read the remainders backwards. 2 55 59. In base 62, two stays as two. 55 becomes t 59 becomes x. So URL number 11157 becomes 2tx. Your short URL is tinyurl.com/2tx. No collisions, no database lookups to check if it's taken, just clean math. To trade off, you need a way to generate unique numbers across multiple servers. That's its own engineering challenge and there's a security issue. If someone figures out your pattern, they can guess to next short URL. But for most cases, this approach is much cleaner. Now, generating the short URL is just half of the problem. The other half is what happens when someone clicks it. When you click a short URL, the system needs to look up the original URL and redirect you there. And this happens a lot more often than creating new short URLs. So speed matters. First check the cache. If the mapping is there, redirect immediately. If not, hit the database. Cache the result for next time. Then redirect. The redirect itself uses what's called a 301 status code. That tells your browser this URL has permanently moved to the other location. Your browser remembers this, so it might skip the URL shortener entirely next time. But here's what makes this really interesting. the scale. A single database can handle 10,000 lookups per second. So you need multiple database replicas to spread the load. Eventually you will need to split the data across multiple databases entirely. This is called shorting and is another whole engineering problem. You need to figure out how to distribute the data evenly, how to route requests to the right database, what happens when one database goes down, how to rebalance when you add more servers. And that's just the beginning. In the real world, you also need to think about rate limiting so people can't spam your service. You need analytics to track how many people click each link. You need security to block malicious URLs. What started as make this URL shorter becomes a lesson in distributed systems, caching, database scaling, and performance optimization. Every major tech company has built some version of this. Twitter shortens URL in tweets. Slack does it in messages. Even your company's internal tools probably do this. And the techniques we talk about, unique ID generation, caching strategies, database sharding, these patterns show up everywhere. Instagram uses similar ID generation for photos. Netflix uses similar caching for video meta data. Uber uses similar database splitting for trip data. The next time you click a shorten link, you will know there's a whole system working in milliseconds to get you where you're going. And you will start recognizing these same patterns in every app you use. Ready to ace your next technical interview? Join our community where we offer comprehensive courses on system design, coding, behavioral questions, machine learning, and object-oriented design. Learn more at bybico.com.

Original Description

We just launched the all-in-one tech interview prep platform, covering coding, system design, OOD, and machine learning. Launch sale: 50% off. Check it out: https://bit.ly/bytebyego-yt-desc
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from ByteByteGo · ByteByteGo · 0 of 60

← Previous Next →
1 What happens when you type a URL into your browser?
What happens when you type a URL into your browser?
ByteByteGo
2 System Design: Why is Kafka fast?
System Design: Why is Kafka fast?
ByteByteGo
3 System Design: How to store passwords in the database?
System Design: How to store passwords in the database?
ByteByteGo
4 Big Misconceptions about Bare Metal, Virtual Machines, and Containers
Big Misconceptions about Bare Metal, Virtual Machines, and Containers
ByteByteGo
5 FAANG System Design Interview: Design A Location Based Service (Yelp, Google Places)
FAANG System Design Interview: Design A Location Based Service (Yelp, Google Places)
ByteByteGo
6 Scan To Pay in 2 Minutes
Scan To Pay in 2 Minutes
ByteByteGo
7 Consistent Hashing | Algorithms You Should Know #1
Consistent Hashing | Algorithms You Should Know #1
ByteByteGo
8 System Design: Why is single-threaded Redis so fast?
System Design: Why is single-threaded Redis so fast?
ByteByteGo
9 HTTP/1 to HTTP/2 to HTTP/3
HTTP/1 to HTTP/2 to HTTP/3
ByteByteGo
10 What Is REST API? Examples And How To Use It: Crash Course System Design #3
What Is REST API? Examples And How To Use It: Crash Course System Design #3
ByteByteGo
11 The Secret Sauce Behind NoSQL: LSM Tree
The Secret Sauce Behind NoSQL: LSM Tree
ByteByteGo
12 Bloom Filters | Algorithms You Should Know #2 | Real-world Examples
Bloom Filters | Algorithms You Should Know #2 | Real-world Examples
ByteByteGo
13 Back-Of-The-Envelope Estimation / Capacity Planning
Back-Of-The-Envelope Estimation / Capacity Planning
ByteByteGo
14 How To Choose The Right Database?
How To Choose The Right Database?
ByteByteGo
15 How Does Live Streaming Platform Work? (YouTube live, Twitch, TikTok Live)
How Does Live Streaming Platform Work? (YouTube live, Twitch, TikTok Live)
ByteByteGo
16 Latency Numbers Programmer Should Know: Crash Course System Design #1
Latency Numbers Programmer Should Know: Crash Course System Design #1
ByteByteGo
17 What Are Microservices Really All About? (And When Not To Use It)
What Are Microservices Really All About? (And When Not To Use It)
ByteByteGo
18 How Does Apple/Google Pay Work?
How Does Apple/Google Pay Work?
ByteByteGo
19 Proxy vs Reverse Proxy (Real-world Examples)
Proxy vs Reverse Proxy (Real-world Examples)
ByteByteGo
20 What is API Gateway?
What is API Gateway?
ByteByteGo
21 What Is GraphQL? REST vs. GraphQL
What Is GraphQL? REST vs. GraphQL
ByteByteGo
22 What Is Single Sign-on (SSO)? How It Works
What Is Single Sign-on (SSO)? How It Works
ByteByteGo
23 What Is A CDN? How Does It Work?
What Is A CDN? How Does It Work?
ByteByteGo
24 What is RPC? gRPC Introduction.
What is RPC? gRPC Introduction.
ByteByteGo
25 SSL, TLS, HTTPS Explained
SSL, TLS, HTTPS Explained
ByteByteGo
26 FANG Interview Question | Process vs Thread
FANG Interview Question | Process vs Thread
ByteByteGo
27 What is OSI Model | Real World Examples
What is OSI Model | Real World Examples
ByteByteGo
28 CAP Theorem Simplified
CAP Theorem Simplified
ByteByteGo
29 Kubernetes Explained in 6 Minutes | k8s Architecture
Kubernetes Explained in 6 Minutes | k8s Architecture
ByteByteGo
30 CI/CD In 5 Minutes | Is It Worth The Hassle: Crash Course System Design #2
CI/CD In 5 Minutes | Is It Worth The Hassle: Crash Course System Design #2
ByteByteGo
31 Why Is System Design Interview Important?
Why Is System Design Interview Important?
ByteByteGo
32 8 Key Data Structures That Power Modern Databases
8 Key Data Structures That Power Modern Databases
ByteByteGo
33 System Design Interview: A Step-By-Step Guide
System Design Interview: A Step-By-Step Guide
ByteByteGo
34 Top 5 Redis Use Cases
Top 5 Redis Use Cases
ByteByteGo
35 Debugging Like A Pro
Debugging Like A Pro
ByteByteGo
36 But What Is Cloud Native Really All About?
But What Is Cloud Native Really All About?
ByteByteGo
37 Everything You Need to Know About DNS: Crash Course System Design #4
Everything You Need to Know About DNS: Crash Course System Design #4
ByteByteGo
38 The Most Beloved Burger for Developers
The Most Beloved Burger for Developers
ByteByteGo
39 10+ Key Memory & Storage Systems: Crash Course System Design #5
10+ Key Memory & Storage Systems: Crash Course System Design #5
ByteByteGo
40 Cache Systems Every Developer Should Know
Cache Systems Every Developer Should Know
ByteByteGo
41 Top 7 ChatGPT Developer Hacks
Top 7 ChatGPT Developer Hacks
ByteByteGo
42 How ChatGPT Works Technically | ChatGPT Architecture
How ChatGPT Works Technically | ChatGPT Architecture
ByteByteGo
43 10 Key Data Structures We Use Every Day
10 Key Data Structures We Use Every Day
ByteByteGo
44 Top 7 Most-Used Distributed System Patterns
Top 7 Most-Used Distributed System Patterns
ByteByteGo
45 Secret To Optimizing SQL Queries - Understand The SQL Execution Order
Secret To Optimizing SQL Queries - Understand The SQL Execution Order
ByteByteGo
46 Amazon Prime Video Ditches AWS Serverless, Saves 90%
Amazon Prime Video Ditches AWS Serverless, Saves 90%
ByteByteGo
47 Top 6 Most Popular API Architecture Styles
Top 6 Most Popular API Architecture Styles
ByteByteGo
48 Top 5 Most-Used Deployment Strategies
Top 5 Most-Used Deployment Strategies
ByteByteGo
49 How Discord Stores TRILLIONS of Messages
How Discord Stores TRILLIONS of Messages
ByteByteGo
50 Uncovering Stack Overflow's Shocking Architecture
Uncovering Stack Overflow's Shocking Architecture
ByteByteGo
51 OAuth 2 Explained In Simple Terms
OAuth 2 Explained In Simple Terms
ByteByteGo
52 Demystifying the Unusual Evolution of the Netflix API Architecture
Demystifying the Unusual Evolution of the Netflix API Architecture
ByteByteGo
53 1 Year Of YouTube | Best System Design Series
1 Year Of YouTube | Best System Design Series
ByteByteGo
54 DevOps vs SRE vs Platform Engineering | Clear Big Misconceptions
DevOps vs SRE vs Platform Engineering | Clear Big Misconceptions
ByteByteGo
55 Top 7 Ways to 10x Your API Performance
Top 7 Ways to 10x Your API Performance
ByteByteGo
56 Why Google and Meta Put Billion Lines of Code In 1 Repository?
Why Google and Meta Put Billion Lines of Code In 1 Repository?
ByteByteGo
57 Git MERGE vs REBASE: Everything You Need to Know
Git MERGE vs REBASE: Everything You Need to Know
ByteByteGo
58 Top 6 Load Balancing Algorithms Every Developer Should Know
Top 6 Load Balancing Algorithms Every Developer Should Know
ByteByteGo
59 Algorithms You Should Know Before System Design Interviews
Algorithms You Should Know Before System Design Interviews
ByteByteGo
60 Top 5 Most Used Architecture Patterns
Top 5 Most Used Architecture Patterns
ByteByteGo

The video teaches how URL shorteners work, including generating short URLs and handling redirects, and explores the systems design and scalability challenges involved.

Key Takeaways
  1. Understand the problem of generating short URLs
  2. Learn about hashing and base62 conversion
  3. Implement caching and database scaling
  4. Consider distributed system trade-offs
💡 URL shorteners require careful consideration of system design and scalability to handle high volumes of requests and ensure efficient redirects.

Related Reads

📰
Simple is Simple, Complex is Possible
Learn to balance simplicity and complexity in programming and system design
Medium · Programming
📰
Unlocking Modern Development: How the Odin Programming Language is Redefining Systems-Level App Innovation
Learn how Odin, a high-performance programming language, is redefining systems-level app innovation with its unique features and practical applications.
Dev.to · Tamiz Uddin
📰
Every System Design Interview Is Secretly About These 16 Companies.
System design interviews focus on real-world company examples, not just theoretical concepts, to assess problem-solving skills
Medium · Programming
📰
Skill Bodies Should Load on Demand
Learn how to optimize skill systems by loading skill bodies on demand, improving performance and scalability
Dev.to · Manuel Bruña
Up next
What is API and How it Works? | API Explained for Beginners | Tamil | Karthik's Show
Karthik's Show
Watch →