Skip to content

andromaqui/urlshortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Url Shortener

The goal of the URL shortener application is to enable users to easily convert long URLs into automatically generated short URLs, streamlining the process of sharing and managing links without the need for custom short URL options. The application efficiently generates unique short URLs for users, simplifying link sharing and enhancing overall usability.

The app does not handle the expiry or deletion of the urls, but this is a step which could be added in following iterations.

Here’s a more concise overview of the features that make your URL shortener application production-ready:

  1. Endpoints: Well-defined RESTful APIs for shortening and retrieving URLs, facilitating easy integration and usability.
  2. Custom Exception Handling: Custom exceptions and a global exception handler provide clear error responses, enhancing user experience during failures.
  3. Spring Actuator: Enables monitoring and management of the application with endpoints for health checks and metrics, ensuring operational reliability.
  4. Spring Profiles: Allows different configurations for various environments (development, testing, production), reducing risks during deployment.
  5. Caching: Improves performance by storing frequently accessed data, reducing database load and enhancing response times for users.
  6. Dockerfile: The Dockerfile defines the steps to build a Docker image for the URL shortener application, specifying the base images, dependencies, and configuration needed to run the application in a containerized environment.

Building Url Shortener

Prerequisites for building this app are Apache Maven 3 and Java 17. Once this is set up run:

mvn clean install

And if you don't want to run the tests

mvn clean install -DskipTests

Development Setup

Docker

This app uses MongoDB as a database to store data. While the url and the port of the database can be altered via the application-dev.properties file, by default the app expects to find a MongoDB instance on localhost:27017. In order to quickly have a MongoDB up and running you can install Docker and run the following command:

docker-compose -f docker-compose-mongo.yml up -d

This will pull the MongoDB version 7.0.0 and start a container on localhost:27017.

Spring Profile Configuration

The are currently 3 different spring profiles defined:

  • Dev profile for which the config for can be found in application-dev.properties
  • Stage profile for which the config for can be found in application-stage.properties
  • Prod profile for which the config for can be found in application-prod.properties

The default profile is set in application.properties to be dev. If you which to switch to a different profile, make sure you adjust the configuration in each of the application-*.properties file.

Architecture

Endpoint Summaries

  1. Shorten URL Endpoint (POST /api/v1/shortUrl):
    • Purpose: This endpoint allows users to convert a long URL into a shorter, more manageable version.
    • Functionality: When a valid long URL is provided in the request body, the service generates a corresponding short URL. The response contains the newly created short URL.
    • Response Status: Returns a 201 Created status with the short URL details.
  2. Redirect to Long URL Endpoint (GET /api/v1/shortUrl/{shortUrl}):
    • Purpose: This endpoint retrieves the original long URL associated with a given short URL.
    • Functionality: When a short URL is provided as a path variable, the service looks up its corresponding long URL. If found, it redirects the user to the long URL. If not found, it returns a 404 Not Found status.
    • Response Status: Returns a 301 Moved Permanently status with a redirect to the long URL if found, or a 404 Not Found status if not.

Url Hashing

The hash or alias is generated by combining the original long URL and the current timestamp, then applying the MurmurHash3 algorithm to ensure a unique and compact representation suitable for use as a short URL. This approach minimizes collisions and provides a reliable way to shorten URLs programmatically.

Caching

Caffeine is a high-performance, in-memory caching library for Java. URL shorteners typically experience a high volume of read operations (retrieving long URLs) compared to write operations. Caffeine's efficient caching mechanisms help reduce latency and improve response times for these frequent reads. By caching long URLs associated with short URLs, Caffeine minimizes database queries, reducing the load on the underlying database and enhancing overall application performance.

Advantages

  1. Performance Improvement: Reduces database load by caching frequently accessed data, leading to faster response times.
  2. Customizable: Allows setting expiration times (6 hours) and a maximum size (1000 entries) to manage memory usage.

Disadvantages

  1. Memory Usage: Cached data occupies memory, which may lead to increased resource consumption, especially with large datasets.
  2. Stale Data: Cached entries may become outdated if not managed correctly, potentially serving stale data to users. Currently there isnt such threat since we are deleting any data, but this is a consideration we should make as we enrich the app with more endpoints.

Database

The NoSQL database MongoDB was chosen for the URL shortener application primarily due to its flexibility and scalability, which are essential for handling the dynamic nature of URL mappings. With NoSQL databases like MongoDB, we can easily adapt the schema to accommodate future enhancements without complex migrations, allowing rapid iteration and development. Additionally, NoSQL provides high-performance read and write operations, which is crucial given the expected high traffic and frequent access to shortened URLs. The document-oriented structure aligns well with the application's data model, making it straightforward to store and retrieve URL mappings. This choice ensures that the application can efficiently scale as the user base grows, making it a robust solution for a URL shortener.

Schema

The schema for the URL shortener application using a NoSQL database (like MongoDB) is represented by the UrlEntity class, which corresponds to the url_mappings collection. Here’s a breakdown of the fields:

  1. id: Unique identifier for each URL mapping (automatically generated by the database).
  2. shortUrl: The shortened URL that maps to the long URL, indexed for quick retrieval and must be unique.
  3. longUrl: The original URL that the shortened version points to.
  4. creationDate: Timestamp indicating when the mapping was created.

Actuator

Actuator exposes the endpoints health, info and prometheus which are crucial for monitoring Spring Boot applications.

  1. The health endpoint (/actuator/health) provides real-time status information, allowing teams to quickly assess application health and troubleshoot issues.
  2. The info endpoint (/actuator/info) exposes metadata about the application, such as its version and build details, which aids in tracking deployments and understanding application context.
  3. The prometheus endpoint (/actuator/prometheus) facilitates the collection of application metrics for performance monitoring, enabling teams to analyze data and optimize resource usage effectively. Together, these endpoints support proactive management and enhance operational visibility .

TODO

  • Implement CI/CD.

About

A tiny url clone built with Spring Boot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published