How to Build a High‑Performance Short URL Service: Architecture, Algorithms, and Scaling Tips
This article explores the benefits of short URLs, explains their redirection mechanics, compares hash‑based and auto‑increment generation methods, and presents a high‑throughput architecture using OpenResty, Redis, and MySQL to handle massive traffic while ensuring reliability and scalability.
Introduction
We discuss how to design a high‑performance short‑link system, a common interview design question, and share insights from a production service that has been stable for over two years.
Why Use Short URLs?
Short links save character space on platforms with length limits (e.g., micro‑blogs, SMS), improve visual layout, generate smaller QR codes, and are more reliably recognized as hyperlinks on certain apps.
Long URLs often exceed display limits and can cause extra costs when split across multiple messages.
Basic Redirection Principle
A short URL returns HTTP 302 (temporary redirect) with a Location header pointing to the long URL; the browser then follows the redirect to fetch the final content.
302 is preferred over 301 because it allows the server to count each click.
Methods to Generate Short URLs
1. Hash‑Based Algorithm
Map a long URL to a fixed‑length hash value (e.g., MurmurHash) and optionally encode it in base‑62 to shorten the string.
MurmurHash provides high speed and low collision probability; a 32‑bit hash yields up to ~4.3 billion values, sufficient for most services. Converting the decimal hash to base‑62 reduces the length further (e.g., 3002604296 → 3hcCxy).
Hash collisions are handled by checking a storage table (MySQL) and, if a conflict occurs, appending a custom suffix (e.g., "DUPLICATE") before re‑hashing.
CREATE TABLE `short_url_map` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`lurl` varchar(160) DEFAULT NULL COMMENT 'long URL',
`surl` varchar(10) DEFAULT NULL COMMENT 'short URL',
`gmt_create` int(11) DEFAULT NULL COMMENT 'creation time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;2. Auto‑Increment ID Algorithm
Generate a monotonically increasing integer (via MySQL AUTO_INCREMENT, Redis, Snowflake, or UUID) and encode it in base‑62 to form the short path.
Using MySQL AUTO_INCREMENT as the ID source, a dedicated “sender” table reserves a range of IDs for each application server to avoid contention.
When the allocated range is exhausted, the server requests a new range from the sender table.
CREATE TABLE `short_url_map` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'short URL id',
`lurl` varchar(10) DEFAULT NULL COMMENT 'long URL',
`md5` char(32) DEFAULT NULL COMMENT 'long URL MD5',
`gmt_create` int(11) DEFAULT NULL COMMENT 'creation time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;High‑Performance Architecture
To handle spikes of hundreds of thousands of QPS, the service uses OpenResty (Nginx + Lua) as a front‑end that directly accesses Redis cache and MySQL, eliminating an extra business‑logic layer. Deploying at least two instances avoids a single point of failure.
Conclusion
The article presents several design options for short‑link services, covering hash‑based and auto‑increment generation, Bloom filter optimization, and a high‑throughput OpenResty‑based architecture, encouraging readers to explore the mentioned technologies further.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
