This tutorial shows how to build a URL shortener similar to TinyURL, covering both design and implementation.
Technologies
- Golang
- Gin Framework
- GORM
- Redis
- PostgreSQL
High-Level Design
API Endpoints
API endpoints enable communication between clients and servers. We design these APIs using REST principles.
If you are new to RESTful APIs, see: restfulapi
A URL shortener mainly needs two endpoints:
- URL shortening:
POSTrequest with the original long URL. - URL redirecting:
GETrequest with the short URL.
URL Shortening
For URL shortening, we need a hash function that maps a long URL to a short URL. Conceptually, we store <shortURL, longURL> pairs.

URL Redirecting
The API supports two redirect status codes:
- 301 (permanent redirect): browser caches response, so future requests can bypass shortener service.
- 302 (temporary redirect): future requests continue passing through shortener service first.

To reduce server load, 301 is often a good default when mappings are stable.
Low-Level Design
Hash Algorithm
Like any hash strategy, collisions can happen. Checking the database on every collision is expensive.
A common approach is Base62 encoding, using characters:
0-9a-zA-Z

In this project, I use Twitter Snowflake to generate a unique numeric ID, then convert it with Base62.
URL Shortening Flow

- Check whether
longURLalready exists in cache.
If not, check database, cache result, and return it.
If still not found, treat as new URL. - Generate new unique ID (Snowflake), then convert with Base62.
- Store
<shortURL, longURL>in database and cache, then return short URL.
URL Redirecting Flow

- User requests
shortURL - Load balancer routes request to web server
- If short URL exists in cache, return long URL
- Otherwise fetch from database
- Return
301withLocation: longURL
Development
Step 1: Set Up the Project
Create a new project directory and initialize a Go module:

Step 2: Define the URL Struct
Create a URL struct for mapping shortURL and longURL:

Step 3: Implement URL Shortening
Create GenerateShortURL to:
- handle
POSTrequests - generate short URLs
- return JSON response

Step 4: Implement URL Redirecting
Create RedirectShortURL to:
- handle
GETrequests - resolve short URL
- redirect to long URL

Notes:
- Caching is handled in service layer logic for each function call.
- Snippets above are illustrative. Full source code is available here.
Conclusion
That is all for this article.
I hope this tutorial is useful. If you need help, feel free to reach out.
Let's connect on Twitter and LinkedIn.
Thanks for reading. See you next time.