Designing a Scalable Short URL Service with Spring Boot
This article explains the principles, system design, key generation, encoding strategies, HTTP redirection techniques, and step‑by‑step Spring Boot project setup needed to build a reliable short‑link service for marketing and internal applications.
What Is a Short Link Service
Short link services convert long URLs into short ones, making them easier to remember and share on social platforms.
For a simple marketing campaign, the workflow is:
Convert a long landing‑page URL (e.g., https://www.glo.com/2021/09/15/ddd/tactics/introduction/#more) into a short URL (e.g., http://glo.com/s) using a short‑link service.
Distribute the short link to target users via channels such as SMS.
Users click the short link, the service receives the request and redirects to the original long URL.
The user finally accesses the target website via the long URL.
The core process of a short‑link service includes creating a short link and accessing a short link.
System Design Points
The service revolves around a Key and a Map :
Create short link: generate a key, store the long URL as the value in the map, concatenate the domain with the key to form the short URL, and return it.
Access short link: extract the key from the URL, look up the target URL in the map, and perform an HTTP redirect.
The map can be implemented with MySQL and a cache, raising two questions:
How is the key generated and maintained?
How to perform HTTP‑based request redirection?
2.1 Key Generation
Key length is a critical metric for short‑link services.
The key must be unique and as short as possible, so distributed algorithms like UUID are unsuitable; an auto‑incrementing number is preferred.
Conclusion: a number‑based auto‑increment key generator is needed.
2.2 Key Encode/Decode
If we use a numeric key, can we further compress its length?
Higher numeral bases produce shorter representations (e.g., base‑16 is shorter than base‑10, which is shorter than base‑8).
Therefore, we can encode the numeric key using a high base to reduce its length.
2.3 Request Redirection
Redirection is part of the HTTP protocol; Java EE’s HttpServletResponse and Spring MVC both provide support.
Using HttpServletResponse:
public void redirect(@PathVariable String code, HttpServletResponse response) throws IOException {
String url = getTargetUrl(code);
// Call sendRedirect to perform redirection
response.sendRedirect(url);
}Using Spring MVC:
public ModelAndView redirect(@PathVariable String code){
String url = getTargetUrl(code);
// Use RedirectView for redirection
RedirectView redirectView = new RedirectView();
redirectView.setUrl(url);
return new ModelAndView(redirectView);
}Project Setup
The project uses Spring Boot as the main framework.
Dependencies:
spring-boot-starter-web – Web
flyway – Database migration
Junit – Testing
lombok – Auto‑generate getters/setters
Generate the project at https://start.spring.io/ with Maven, Java, Spring Boot 2.1.1, group com.geekhalo, artifact tinyurl, and the listed dependencies, then import the downloaded project into an IDE.
Core Components
Based on the design analysis, the system requires the following core components.
4.1 NumberGenerator
Generates auto‑increment numeric keys.
public interface NumberGenerator {
/**
* Generate an auto‑increment key
* @return
*/
Long nextNumber(NumberType type);
}4.2 NumberEncoder
Encodes and decodes numbers to further shorten keys.
public interface NumberEncoder {
/**
* Encode a number
* @param id
* @return
*/
String encode(Long id);
/**
* Decode a string back to a number
* @param str
* @return
*/
Long decode(String str);
}4.3 TargetUrlRepository
Handles persistence of target URLs.
public interface TargetUrlRepository {
/**
* Save a link
* @param targetUrl
*/
void save(TargetUrl targetUrl);
/**
* Retrieve a link by ID
* @param id
* @return
*/
TargetUrl getById(Long id);
}Core Process
The core process includes creating short links and accessing them.
5.1 Create Short Link
Creates a short link for internal systems by submitting a long URL and receiving a short URL.
Creation flow diagram:
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
