Backend Development 12 min read

Design and Implementation of a Short URL Service

This article explains the value, underlying HTTP redirect mechanism, and detailed design approaches—including hash‑based and distributed‑ID generation, base‑62 encoding, database storage, caching, indexing, sharding, and rate‑limiting—to build a scalable short‑link service.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Design and Implementation of a Short URL Service

Short URLs provide three main benefits: they are concise, easier to use (especially on platforms with length limits or when converting to QR codes), and can reduce costs such as SMS fees.

The core principle behind a short link is an HTTP 302 redirect: the short‑link server receives the short URL, looks up the corresponding long URL, and returns a 302 response that directs the browser to the original address.

System workflow includes two key steps: (1) user submits a long URL to a generation page and receives a short URL; (2) user accesses the short URL, the service returns a 302 redirect to the long URL.

The design must solve two problems: how to generate a unique short link from a long URL, and how to store the mapping persistently (typically in a MySQL table, optionally with a cache layer).

Generation methods :

Hash‑algorithm based short link

Apply a non‑cryptographic hash function such as MurmurHash to the long URL to obtain a numeric hash value. Resolve collisions using re‑hashing (adding a special character and re‑hashing) until a unique value is found.

https://mp.weixin.qq.com/s?__biz=MzA4MjIyNTY0MQ==∣=2647743787&idx=1&sn=1cabkkec8eb1b81d6ee5dd7ba7fa05ac0f1&chksm=87ad0dad5546b0da84bb7beb5e4373a14e89fba1130c1bd2a51f4baa8021ebiboiic0abe496ce94603b6b4&token=894028224⟨=zh_CN#rd
  ↓
29541341303115543223957290326355

Convert the decimal hash to a base‑62 string (using characters 0‑9, a‑z, A‑Z) to obtain a compact short code, e.g., cgSqq .

Distributed ID based short link

Generate a globally unique ID (e.g., via a Snowflake‑style ID generator) for each long URL, then encode the numeric ID into base‑62 to produce the short code.

First request URL → generate short link: https://dwz.com/1021000001
Second request URL → generate short link: https://dwz.com/1021000002

Both methods store the short_code ↔ long_url mapping in MySQL.

Performance optimizations :

Create a unique index on the short‑code column to speed up lookups.

Introduce a cache (e.g., Redis) for hot short‑code entries to reduce database load.

Apply read‑write separation since reads far outnumber writes.

Implement sharding (e.g., by the base‑62 numeric value) for massive data volumes.

Add simple rate‑limiting (e.g., per‑IP limits) to mitigate abuse.

In summary, the article covers the motivations for short URLs, the HTTP redirect mechanism, two concrete generation strategies with collision handling and base‑62 encoding, and a set of scalability and security enhancements for a production‑grade short‑link service.

backendperformance optimizationDatabaseHTTP redirecthash algorithmShort URL
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login 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.