تدريب Shadowing: How Does a URL Shortener Work? - تعلم التحدث بالإنجليزية عبر الفيديو
جارٍ إنشاء الدرس...
1
Everyone wonder what happens when you click a tiny URL?
2
You know, those short links like bit.ly and tinyurl, they somehow know exactly where to take you.
3
Today, we're going to reverse engineer how URL shorteners actually work.
4
And trust me, there's way more going on behind the scenes than you might think.
5
So what exactly does a URL shortener do?
6
Simple, it takes a massive URL like this Amazon link that goes on forever and turns it into something short and clean.
7
Click the short version and boom, you end up at the same place.
8
But here's where it gets interesting.
9
Let's say you're building the next bit.ly.
10
How many URLs do you think you need to handle?
11
Try 100 million new URLs every day.
12
That's over a thousand new short links created every single second.
13
And since people click links way more than they create them, you're looking at over 10,000 clicks per second.
14
Now think about storage.
15
Over 10 years, that's 365 billion URLs you need to keep track of.
16
Just storing the URLs themselves would take 36 terabytes.
17
So the real question becomes, how do you even generate these short URLs?
18
This is where the math gets fun.
19
Short URLs can use numbers and letters.
20
That's 62 possible characters, 0 through 9, lowercase A through Z, and uppercase A through Z.
21
But how short can we make them?
22
Let's work backwards.
23
We need 365 billion unique combinations.
24
With one character, you get 62 possibilities.
25
With two characters, 62 squared.
26
That's about 3000.
27
With three characters, 62 cubed. About 238,000.
28
Keep going you get to 7 characters, 62 to the 7th power.
29
That's 3.5 trillion possible combinations, way more than we need.
30
So 7 characters it is, but how do we actually create them?
31
There are two approaches, and they couldn't be more different.
32
The first approach, just hash the long URL.
33
Take any hash function like md5, run the long URL through it, and you get back a long string of random characters.
34
The problem?
35
That string is way too long.
36
Even the shortest hash gives you 32 characters when you only want 7.
37
So you take the first 7 characters and call it a day.
38
But wait, what happens when two different URLs give you the same first 7 characters?
39
You've got a collision.
40
Now you're stuck.
41
You have to try again with some variation of the original URL until you find 7 characters that nobody else is using.
42
Every time you want to create a short URL, you have to check if those 7 characters are already taken.
43
That's a lot of database lookups.
44
The second approach is way more elegant.
45
Instead of hashing, you just count.
46
Every time someone wants to shorten a URL, give you the next number in sequence.
47
URL number 1, number 2, number 3, and so on.
48
Then convert that number to what's called base 62.
49
Here's how that works.
50
Let's say you're on URL number 11157.
51
To convert to base 62, you divide by 62 over and over, keeping track of the remainders.
52
11157 divided by 62 is 179, remainder 59.
53
179 divided by 62 is 2, remainder 55.
54
2 divided by 62 is 0, remainder 2.
55
Now read the remainders backwards.
56
2, 55, 59.
57
In base 62, 2 stays as 2.
58
55 becomes T.
59
59 becomes X.
60
So URL number 11157 becomes 2TX.
61
Your short URL is tinyurl.com slash 2tx.
62
No collisions, no database lookups to check if it's taken, just clean math.
63
To trade off, you need a way to generate unique numbers across multiple servers.
64
That's its own engineering challenge.
65
And there's a security issue.
66
If someone figures out your pattern, they can guess the next short URL.
67
But for most cases, this approach is much cleaner.
68
Now, generating the short URL is just half of the problem.
69
The other half is what happens when someone clicks it.
70
When you click a short URL, the system needs to look up the original URL and redirect you there.
71
And this happens a lot more often than creating new short URLs.
72
So speed matters.
73
First, check the cache.
74
If the mapping is there, redirect immediately.
75
If not, hit the database, cache the result for next time, then redirect.
76
The redirect itself uses what's called a 301 status code.
77
That tells your browser this URL has permanently moved to the other location.
78
Your browser remembers this, so it might skip the URL shortener entirely next time.
79
But here's what makes this really interesting a scale.
80
A single database can handle 10,000 lookups per second, so you need multiple database replicas to spread the load.
81
Eventually, you will need to split the data across multiple databases entirely.
82
This is called sharding, and it's another whole engineering problem.
83
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.
84
And that's just the beginning.
85
In the real world, you also need to think about rate limiting so people can't spam your service.
86
You need analytics to track how many people click each link.
87
You need security to block malicious URLs.
88
What started as make this URL shorter becomes a lesson in distributed systems, caching, database scaling, and performance optimization.
89
Every major tech company has built some version of this.
90
Twitter shortens URL in tweets.
91
Slack does it in messages.
92
Even your company's internal tools probably do this.
93
And the techniques we talk about, unique ID generation, caching strategies, database sharding, these patterns show up everywhere.
94
Instagram uses similar ID generation for photos.
95
Netflix uses similar caching for video metadata.
96
Uber uses similar database splitting for trip data.
97
The next time you click a shortened link, you will know there's a host system working in milliseconds to get you where you're going.
98
And you will start recognizing these same patterns in every app you use.
99
Ready to ace your next technical interview?
100
Join our community where we offer comprehensive courses on system design, coding, behavioral questions, machine learning, and object-oriented design.
101
Learn more at bytebytego.com.
📺 نفس القناة
✨ فيديو موصى به
حول هذا الدرس
أنت تتدرب على اللغة الإنجليزية باستخدام "How Does a URL Shortener Work?" مع تقنية الـ Shadowing.
ما هي تقنية التظليل الصوتي؟
التظليل الصوتي (Shadowing) تقنية تعلم لغة مدعومة علمياً، طُورت أصلاً لتدريب المترجمين الفوريين المحترفين. الطريقة بسيطة لكنها قوية: تستمع لصوت إنجليزي أصلي وتكرره فوراً بصوت عالٍ — كظل يتبع المتحدث بتأخير 1-2 ثانية. تُظهر الأبحاث تحسناً كبيراً في دقة النطق والتنغيم والإيقاع وربط الأصوات والاستماع والطلاقة.

























