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.

Architecture Digest
Architecture Digest
Architecture Digest
Design and Implementation of a Short URL 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==&mid=2647743787&idx=1&sn=1caec8eb1b81d6ee5dd7ba7fa05ac0f1&chksm=87ad0dadb0da84bb7beb5e4373a14e89fba1130c1bd2a51f4baa8021ec0abe496ce94603b6b4&token=894028224&lang=zh_CN#rd
http://dwz.cn/iijg
原有长链:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5dd7b<br/>↓↓<br/>发生哈希冲突<br/>↓↓<br/>补上特殊字符:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5dd7b[SPECIAL-CHARACTER]<br/>↓↓<br/>再次进行哈希
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<br/>↓↓<br/>生成短链:https://dwz.com/1021000001<br/><br/>第一次请求:https://mp.weixin.qq.com/s1caec8eb1b81d6ee5ff7b<br/>↓↓<br/>生成短链: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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

cachingHTTP redirectdistributed-iddatabase indexinghash algorithmshort URL
Architecture Digest
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.