Backend Development 8 min read

Design and Implementation of a Short URL Service in Java

This article explains the benefits of short URLs, outlines the basic workflow of generating and resolving them, and presents a complete Java implementation that uses Redis for fast storage, MySQL for persistence, and various techniques for high concurrency and distributed deployment.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Design and Implementation of a Short URL Service in Java

The interview scenario introduces short URLs and lists their advantages: brevity for SMS and social platforms, cleaner appearance, easy analytics, and security by hiding parameters.

Short URL generation follows four steps: a service maps a long URL to a short one, the short link is embedded in messages, the user clicks it, the browser follows a 301/302 redirect, and the original content is displayed. The article focuses on the first step.

Service design discusses using an auto‑incrementing identifier as a simple allocator, noting that a true one‑to‑one deterministic algorithm is impossible. To ensure persistence across restarts, the mapping is stored in a database (e.g., MySQL) and cached in Redis.

Key Q&A sections cover storage of the mapping, guaranteeing one‑to‑one relationships, representation of short URLs in base‑32, handling high concurrency with caching and batch allocation, and avoiding a single point of failure by distributing the allocator across multiple services.

Implementation details are provided in Java. The utility class defines constants for Redis keys, host URL, and cache settings, and includes methods to retrieve a short URL, check the cache, generate a new identifier, store the mapping, and convert numeric IDs to a custom base using a digit array. The conversion method toOtherBaseString(long n, int base) handles negative numbers and builds the result in a character buffer.

Supporting code defines the digit set for base‑32, an enum Decimal for specifying the target base, and a main method that demonstrates generating short URLs for sample long URLs with both 32‑ and 64‑bit bases.

Overall, the article provides a practical guide to building a scalable short‑URL service, covering algorithmic choices, data storage, caching strategies, concurrency handling, and distributed deployment considerations.

Backenddistributed systemsJavaredishigh concurrencyShort URL
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

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.