Git Product home page Git Product logo

tiny-url's Introduction

Tiny URL

Functional Requirements

  • Encoded url into a short URL and that the short URL can be decoded back into the original URL
  • Only accept JSON as body input and response JSON format following JSONAPI standard
  • The shortened value must be less than 6 characters not including special ones.
  • Provide tests
  • The service will serve continuously for a few numbers of users (max 2 - 5 requests per second)

Design

  • TinyURL is a read-heavy service. To prevent data collision, I have decided to utilize a PostgreSQL database with a unique index created on the slug column. Additionally, we can establish replicated databases to enhance read performance.
  • In order to ensure readability, I have chosen to use Base58 encoding, which avoids special characters as well as others that might confuse the client (eliminating the likes of '0' 'O' 'I' 'l'). The total number of short URLs possible is ( 58^6 ) (maximum characters) which equals 38,068,692,544 URLs. Assuming clients generate new short URLs at a rate of 50 requests per second, for 10 years, the total number of URLs created would be (50*3600*24*365*10 = 15,768,000,000) URLs. Therefore, the service should remain adequate for at least the next decade.
  • To avoid duplication and the need for re-rolling, I have decided on two methods to generate short URLs:
    • The first approach involves using a UUID to create a URL record in the database and then converting this to hex.
    • The second approach is employed if the first method fails to generate a URL. In such cases, the system will inform the user that it is processing their request and will asynchronously re-roll the short URL in a background job to improve user experience. For this method, I will use SecureRandom to generate a hex.
  • Redirection API: To improve performance, I am implementing low-level caching with Redis to store frequently accessed shortened URLs.
  • Future enhancements:
    • To safeguard the system, we can impose a rate limiter to ensure that a single user can only make 5 requests per second.
    • Another strategy for performance improvement is to set up an auxiliary service with an additional database to pre-generate unique slug entries. When client create new shorted url, we can get from service pre-generate quickly

Docs & Demo

  • Visit: https://tiny-url.fly.dev
  • Docs: Import file insomnia_tiny_url.json to Insomnia

Install for development

  • Run rake secret
  • Run rails credentials:edit, add key secret_key_jwt with value from rake secret
  • Copy file env.example to .env and setup your env variable
  • Run rails db:create
  • Run rails db:migrate
  • Run rails server
  • Make sure redis server is installed, run rails sidekiq

Deploy for production use fly.io

  • Run rails credentials:edit --environment production and remember add key secret_key_jwt
  • Ensure RAILS_MASTER_KEY is content of config/credentials/production.key
  • Run fly deploy

Enhancements

  • Rate limiter
  • Pre-generate short URLs service

tiny-url's People

Contributors

th0j avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.