One of the key considerations in our URL Shortener is the handling of duplicate long URLs. We've made a deliberate choice to prioritize unique short URLs for each distinct long URL. While reusing short URLs for identical long URLs may seem like a storage optimization, we believe that maintaining uniqueness serves our project goals better.
- Analytics Accuracy: Reusing short URLs for the same long URL can lead to inaccurate analytics. By assigning a unique short URL to each long URL, we ensure that analytics data accurately represents individual user interactions with distinct content.
- User Experience: Unique short URLs contribute to a better user experience. Users can rely on the consistency of short URLs, knowing that each one corresponds to a specific long URL.
- Security: Unique short URLs add an additional layer of security. They prevent potential abuse or confusion that might arise from the reuse of short URLs for different long URLs.
- Use Hashing Techniques
- Use Hashing Techinique with Collision Handling mechanism.
- UUID's
- Use ID Generator
3.1 Ticket Server (similar to flicker)
3.2 Twitter Snowflake
-
Hashing Techniques:
- Pros: Provides a deterministic and unique output for each input. Fast and efficient.
- Cons: Possibility of hash collisions.
-
Hashing Technique with Collision Handling Mechanism:
- Pros: Maintains the speed of hashing while addressing collision issues.
- Cons: Requires additional logic to handle collisions. Increased complexity in implementation.
-
UUIDs (Universally Unique Identifiers):
- Pros: Globally unique without the need for a central authority. Simple to implement.
- Cons: Longer representation compared to traditional hashes. difficult to index.
-
ID Generator: 4.1. Ticket Server (Similar to Flickr):
- Pros: Scalable and distributed. Low collision probability.
- Cons: Dependency on a central server.
4.2. Twitter Snowflake:
- Pros: Distributed and scalable. No need to central server.
- Cons: Limited by the snowflake size.
we are going to use Snowflake approach as they are easy to scale, maintain and implement.
- Make sure you are at root path of this project.
- Configure env file from database.
env file path: ./db/.env To understand format of .env, please refer to ./db/example.env or just copy paste the env varibles from example.env to .env.
- Run mvnw clean and install command to generate jar file.
./mvnw clean install
- once jar file is created, now just run docker compose up.
docker compose up -d
-
POST Request for generating short urls.
end-point: 127.0.0.1:8080/v1/api/url/shorten body: { "fullUrl": "https://www.google.com" }
-
GET Request for full url.
end-point: 127.0.0.1:8080/v1/api/url/<short-url-code> eg. 127.0.0.1:8080/v1/api/url/70POGa20
- Database : Postgres as main db.
- Cache : Redis as cache with persistent enabled. (AOF)
- CDC : AirBytes used as CDC.