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