การออกแบบบริการย่อลิงก์
เรียนรู้การออกแบบระบบของบริการย่อลิงก์ โดยพิจารณาการรองรับการขยายระบบ การจัดเก็บข้อมูล และความพร้อมใช้งาน
การออกแบบบริการย่อลิงก์ เป็นบทเรียน System Design Basics for Backend Developers ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน System Design Basics for Backend Developers และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Intro to URL Shorteners
Welcome! In this lesson, we'll design a URL shortening service, similar to Bitly or TinyURL. These services take a long, complex URL and convert it into a much shorter, more manageable one.
URL shorteners are incredibly useful for sharing links on social media, in emails, or anywhere space is limited. They also often provide analytics, tracking how many times a shortened link is clicked.
Core Functionality: Shorten & Redirect
A URL shortener primarily performs two key functions:
- Shorten: Takes a long URL as input and generates a unique, short code. This code is then used to construct the short URL.
- Redirect: When a user accesses a short URL, the service looks up the corresponding long URL and redirects the user's browser to it.
These two operations form the backbone of the entire system.
Generating Unique Short Codes
The heart of a URL shortener is its ability to generate unique, short, and often human-readable codes. Common approaches include:
- Sequential IDs + Base62 Encoding: Use an auto-incrementing database ID and convert it to a Base62 string. Base62 uses 0-9, a-z, A-Z (62 characters), allowing for shorter codes than Base10.
- Hash Functions: Apply a hash function (like MD5 or SHA256) to the long URL. Take a portion of the hash to form the short code. This requires collision handling.
- Random String Generation: Generate a random string of a fixed length. This also requires checking for uniqueness to avoid collisions.
Base62 Encoding Example
Let's look at a simple Python example of Base62 encoding, which is a popular method for generating short codes from sequential IDs. This helps ensure uniqueness while keeping codes compact.
BASE62_CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def encode_base62(num):
if num == 0:
return BASE62_CHARS[0]
result = []
while num > 0:
result.append(BASE62_CHARS[num % 62])
num //= 62
return "".join(reversed(result))
# Example usage:
if __name__ == "__main__":
test_id = 12345
short_code = encode_base62(test_id)
print(f"ID: {test_id} -> Short Code: {short_code}")
test_id_large = 9876543210
short_code_large = encode_base62(test_id_large)
print(f"ID: {test_id_large} -> Short Code: {short_code_large}")Database Schema for URLs
To store our URL mappings, we'll need a database. A simple schema could look like this (using a relational database like PostgreSQL):
- id: Primary key (auto-incrementing integer)
- short_code: VARCHAR(10) - The unique short string
- long_url: TEXT - The original, long URL
- created_at: TIMESTAMP - When the short URL was created
- user_id: INT (optional) - If users can create accounts
- click_count: INT (optional) - For basic analytics
A NoSQL database could also work, offering flexibility for schema evolution.
The Redirection Service
When a user clicks on a short URL (e.g., https://tiny.url/abcde), the redirection service takes over. It performs these steps:
- Extracts the
short_code(e.g.,abcde) from the URL. - Queries the database to find the corresponding
long_url. - Sends an HTTP 301 (Moved Permanently) or 302 (Found) redirect response to the user's browser, pointing to the
long_url.
301 vs 302: 301 is for permanent redirects and is cached by browsers, 302 is temporary. For shorteners, 301 is often preferred for performance after the initial creation.
Handling Collisions & Uniqueness
Ensuring each generated short code is unique is critical. If we use hash functions or random strings, collisions (two different long URLs getting the same short code) are possible, though rare with longer codes.
Strategies to handle collisions:
- Database Check: Always attempt to insert the new mapping and catch a unique constraint violation. If a collision occurs, regenerate the code and retry.
- Pre-check: Before inserting, query the database to see if the code already exists. This can lead to race conditions under high concurrency, so database-level unique constraints are preferred.
- Distributed ID Generation: For sequential IDs, use a distributed ID generator (e.g., Snowflake ID) to ensure globally unique IDs that can then be Base62 encoded.
Scalability Considerations
A popular URL shortener needs to handle millions of requests. Key scalability points:
- Database: The database will be a hotspot. Consider sharding the database by
short_codeor using a distributed key-value store. Read replicas are essential for the redirection service. - Caching: Cache frequently accessed short URL to long URL mappings (e.g., using Redis or Memcached) to reduce database load, especially for the redirection path.
- Asynchronous Processing: For click analytics, instead of incrementing a counter synchronously, send click events to a message queue for asynchronous processing.
- Load Balancers: Distribute incoming traffic across multiple instances of your shortening and redirection services.
Basic Click Analytics
Beyond just shortening, many services offer basic analytics. To track clicks:
- When a short URL is accessed, increment a
click_countin the database for that specific mapping. - For high traffic, this counter update can become a bottleneck. A more scalable approach is to send a message to a queue (e.g., Kafka, RabbitMQ) and have a separate worker process asynchronously update the counts or store detailed click logs.
- Detailed analytics might involve storing referrer, user agent, IP address, etc., in a separate analytics database (e.g., a data warehouse).
URL Shortener Challenge
When designing the redirection service for a URL shortener, what is the most critical HTTP response code to send to the user's browser, and why?
Recap: URL Shortener Design
We've walked through the core components of designing a URL shortener!
- We covered the two main functions: shortening and redirection.
- Explored methods for generating unique short codes, like Base62 encoding.
- Discussed database schema for storing mappings and the mechanics of the redirection service.
- Addressed crucial aspects like collision handling, scalability with caching and sharding, and basic click analytics.
This case study illustrates how various system design principles come together to build a functional and scalable service.
คำถามที่พบบ่อย
บทเรียน “การออกแบบบริการย่อลิงก์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การออกแบบบริการย่อลิงก์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส System Design Basics for Backend Developers ให้อัปเกรดเป็น CoddyKit PRO คอร์ส System Design Basics for Backend Developers มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบบริการย่อลิงก์”
เรียนรู้การออกแบบระบบของบริการย่อลิงก์ โดยพิจารณาการรองรับการขยายระบบ การจัดเก็บข้อมูล และความพร้อมใช้งาน คุณปฏิบัติ System Design Basics for Backend Developers ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน System Design Basics for Backend Developers หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน System Design Basics for Backend Developers บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การออกแบบบริการย่อลิงก์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน System Design Basics for Backend Developers นี้ได้ไหม
ได้ บทเรียน System Design Basics for Backend Developers ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การออกแบบบริการย่อลิงก์
- การสร้างฟีดโซเชียลมีเดีย
- การขยายแพลตฟอร์มอีคอมเมิร์ซ
- การออกแบบระบบสนทนาแบบเรียลไทม์