Design and Implementation of a Short URL Service
This article explains the value of short URLs, the HTTP redirection principle behind them, and provides detailed design and implementation strategies—including hash‑based and distributed‑ID approaches, base‑62 encoding, database storage, caching, indexing, and security measures—to build a scalable short‑link service.
Short URLs simplify long web addresses, reduce costs for SMS and social media, and improve usability; the article first outlines these benefits and shows a typical long URL and its shortened form.
Short URLs work by directing the client to a short‑link server, which looks up the original long URL and returns a 302 HTTP redirect response, causing the browser to request the long URL.
The core design challenges are generating a unique short code from a long URL and storing the mapping efficiently, typically in a relational database with a unique index.
Two main generation methods are discussed:
Hash‑based generation : Use a fast, non‑cryptographic hash function such as MurmurHash to produce a numeric hash, resolve collisions with re‑hashing, and then convert the decimal hash to a base‑62 string using the characters 0‑9, a‑z, A‑Z. Example code snippets: https://mp.weixin.qq.com/s?__biz=MzA4MjIyNTY0MQ==∣=2647743787&idx=1&sn=1caec8eb1b81d6ee5dd7ba7fa05ac0f1&chksm=87ad0dadb0da84bb7beb5e4373a14e89fba1130c1bd2a51f4baa8021ec0abe496ce94603b6b4&token=894028224⟨=zh_CN#rd http://dwz.cn/iijg 原有长链:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5dd7b ↓↓ 发生哈希冲突 ↓↓ 补上特殊字符:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5dd7b[SPECIAL-CHARACTER] ↓↓ 再次进行哈希 29541341303115543223957290326355 http://dwz.com/29541341303115543223957290326355 cgSqq
Distributed ID generation : Assign a globally unique numeric ID (e.g., via a Snowflake‑style generator) to each long URL, store the mapping, and optionally convert the ID to base‑62 to shorten it. Example: 第一次请求:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5dd7b ↓↓ 生成短链:https://dwz.com/1021000001 第一次请求:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5ff7b ↓↓ 生成短链:https://dwz.com/1021000002
After generating short links, the service should optimize performance by creating a unique index on the short‑code column, adding a cache layer for hot data, employing read‑write separation, and planning for sharding (分库分表) when the dataset reaches billions of records.
Security considerations include rate limiting per IP (e.g., 10 requests per minute for unauthenticated users) and overall request caps to mitigate abuse.
In summary, the article covers short‑link value, HTTP redirect mechanics, two practical generation techniques, and a suite of operational optimizations to build a robust, scalable short URL service.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.