Skip to content

sivasubramanim/ShortUrl

Repository files navigation

Build Status

ShortUrl

Application to create short Urls and implement forwarding of short Urls to original ones

Problem Summary:

Most of us are familiar with seeing URLs like bit.ly or t.co on our Twitter or Facebook feeds. These are examples of shortened URLs, which are a short alias or pointer to a longer page link.

Mandatory Requirements

Design and implement an API for short URL creation
Implement forwarding of short URLs to the original ones
There should be some form of persistent storage
The application should be distributed as one or more Docker images
The application should be readable, maintainable, and extensible where appropriate.

Additional Requirements

Design and implement an API for gathering different statistics

Solution Overview

The below diagram depicts a high level system view of the project.
Architecture Diagram

Component View of the Backend Short Url Service:

  • ShortUrlController - Manages all the Rest API endpoints of the application
  • ShortUrlService - Manages the core functionalities such as short id creation and retrival of long url
  • ShortUrlRepository - Manages all the data and builds the order of priority between redis cache and mongo db for data
  • ShortUrlMongoRepository - Manages Mongo CRUD
  • ShortIdUtilities - Manages all the application utilities

The solution is built in such a way that we can spin multiple instances of the backend service connected to the Distributed Redis cache server and MongoDB to serve multiple requests. The Redis cache server is introduced as a middle layer to improve the performance of the application.

Technology Stack:

  • Spring Boot for creating the RESTful Web Services. API for short URL creation and API for forwarding of short URLs to original ones
  • Redis Cache used as a distributed in-memory data store. Application is mostly served by the cache server for high performance.
  • MongoDB used as a persistent storage for the application. It can be used as a distributed database server. The application is built in a such a way that it can be switched to different databases.
  • Google Guava used for hashing functions. The short key generation in the application uses a combination of hash function and counter to generate unique short keys easily.
  • Spring Actuator used for monitoring and managing your Spring Boot application. Using Actuator endpoints for gathering different statistics of the application. Currently the application is configured to gather statistics from health, info and metrics API. Introduced 4 new custom metrics as part of the implementation. We can extend this furthure more based on the application need.
  • Maven for building the projects
  • Junit for writing unit, integration tests for the application.
  • Mockito used for writing test cases for service layer with Repository mocks.
  • Embeded Redis Server used in the test layer. All the application integration tests run using embeded redis server and does not require any installation on the test environment.
  • Embeded MongoDB used in the test layer. All the application integration tests run using embeded mongodb server and does not require any installation on the test environment.
  • Docker for building and managing the application as images. The build and deploy phases are completed part of Docker.
  • TravisCI for continous integration.

Build & Run Steps

Download

$ git clone https://github.com/sivasubramanim/ShortUrl.git

Build

$ Navigate to downloaded directory
$ docker-compose build

Deploy & Run

$ docker-compose up

With the default setup, the application should be available at http://localhost:8080/

API Documentation

Short Url APP APIs

URI HTTP Method Description Content-Type Sample Input(Body) Sample Output
/shorturl POST API to find/create a short url application/json {"url":"https://www.google.com/"} {"url": "http://localhost:8080/42d20732"}
/{Id} Get API to forward short URLs to original ones - - Response will be redirected to the original one with Http status as 301

Statistics APIs

URI HTTP Method Description Content-Type Sample Input(Body) Sample Output
/actuator GET API to list all the statistics API configured - - Currently in this app configured info, health and metrics endpoint
/actuator/info GET API to get general info about the application - - { "app": {"name": "urlshortener","description": "Application to create short Urls and implement forwarding of short Urls to original ones","version": "1.0.0-RELEASE","encoding": "UTF-8","java": { "version": "1.8.0_171" } }}
/actuator/health GET API to get application health - - { "status": "UP", "components": { "diskSpace": { "status": "UP", "details": { "total": 255157833728, "free": 7633149952, "threshold": 10485760 } }, "mongo": { "status": "UP", "details": { "version": "4.2.1" } }, "ping": { "status": "UP" }, "redis": { "status": "UP","details": { "version": "2.8.19" } } }}
/actuator/metrics GET API to get different application metrics/stats - - Metrics section contains more 30 metrics. each metric can be individually seen
/actuator/metrics/{metricsname} GET API to get details about a specific metric - - -
Custom Metrics Name :
  • shorturl.success - Metrics to capture number of successful short url creation
  • shorturl.failure - Metrics to capture number of failed scenarios in short url creation
  • redirect.success - Metrics to capture number of successful forwarding
  • redirect.failure - Metrics to capture number of failed scenarios in forwarding

Salient Features

  • Short URL backend service is highly independent service. The application can be tested independently without mongo db server and redis server installation
  • In runtime, if Distributed Cache server or Database goes down, the application wil not fail. It will log the connection failure and continue to serve the users using only database or only cache server.
  • Docker containers are used for build, test, deploy and run scenarios. Only Docker is required to be present in the nodes.

Next Steps

I have taken care of the MVP requirements for this project. The project has scope for future enhancements and improvements such as:

  • Enabling HTTPS for the application.
  • Implementing Authentication and Authorisation for the service
  • Implementing other APIs for the service