Skip to main content
短.be

URL Shortener Architecture

The internal design of a URL shortening service, covering ID generation, database design, caching strategy, and redirect handling.

Dec 7, 2025 · About 1 min read

URL Shortening

URL shortener architecture describes the overall internal design and technical components of a URL shortening service. It is also a popular system design interview topic and an excellent case study for learning scalable web service principles.

The basic architecture consists of three components. First, the URL shortening engine accepts a long URL, generates a unique short code, and stores the mapping in a database. Second, the redirect engine receives requests for short URLs, looks up the destination in the database, and returns a 301 or 302 redirect response. Third, the analytics engine collects click data and aggregates statistics.

Three main approaches exist for generating short codes. Counter-based generation converts a sequential ID to Base62. Hash-based generation takes the first N characters of an MD5 or SHA-256 hash of the URL. Random generation produces a random string and checks for collisions. The counter-based approach is the simplest, collision-free, and widely adopted by large-scale services.

Caching is the key to scalability. Redirect lookups are overwhelmingly read-heavy (over 100x more reads than writes), making Redis or Memcached highly effective. Keeping popular short URLs in cache drastically reduces database queries and can bring redirect latency below 1 millisecond.

For the database layer, key-value stores such as DynamoDB or Redis are well suited to short URL lookups. The data model is straightforward: the short code is the key, and the original URL plus metadata is the value. At the scale of billions of monthly requests, database sharding (partitioning by the first character of the short code, for example) becomes necessary. You can find related books on Amazon.

Share on XHatena

Was this article helpful?

Related Terms

Related Articles

FAQ

Is it hard to build a URL shortener from scratch?
The core functionality (shorten + redirect) can be implemented in a few hours. However, a production-grade service requires caching, rate limiting, malicious URL detection, and high availability, which add significant complexity.
How are short code collisions prevented?
Counter-based generation is collision-free by design. For hash-based or random generation, the system checks whether the generated code already exists in the database and regenerates if a collision is found.
What database should a URL shortener use?
Key-value stores like DynamoDB or Redis are ideal for the read-heavy workload. PostgreSQL or MySQL work fine at smaller scale. Adding a Redis cache layer in front delivers fast responses regardless of the primary database choice.

Ready to create a short URL?

Shorten a URL for Free