Skip to content

Instantly share code, notes, and snippets.

@loftwah
Last active March 9, 2024 23:58
Show Gist options
  • Save loftwah/7a50025bc1b2d29a536adf1bc80602b7 to your computer and use it in GitHub Desktop.
Save loftwah/7a50025bc1b2d29a536adf1bc80602b7 to your computer and use it in GitHub Desktop.
URL Shortener Ruby on Rails

URL Shortener Service Design

This comprehensive design document outlines a URL shortener service using Ruby on Rails, detailing each component's role within the system, infrastructure setup on AWS, CI/CD processes with GitHub Actions, and Terraform for infrastructure management. The design aims to ensure scalability, security, and maintainability.

System Architecture Overview

The system is divided into several core components, each designed to handle specific aspects of the service:

  • Frontend: A web interface for user interaction, built with Rails views and Tailwind CSS for styling.
  • Backend: Rails application handling business logic, data processing, and server-client communication.
  • Database: PostgreSQL for data persistence, storing user, URL, and organizational data.
  • Caching/Queue: Redis for caching and managing background jobs with Sidekiq.
  • Infrastructure: Utilizes AWS services, managed via Terraform, with CI/CD pipelines implemented through GitHub Actions.

Component Design

Frontend Design

The URL shortener service's frontend focuses on user interaction, built with Rails views and styled using Tailwind CSS, while StimulusJS adds necessary dynamism without the bulk of a SPA framework.

Homepage & URL Shortening Interface

  1. Navigation Bar:

    • Links to key sections: profile, organization dashboard, sign-out.
    • Displays user identification: name or email for authenticated users, 'Guest' for others.
  2. URL Shortening Form:

    • Titled "Shorten Your URL."
    • An input field for users to enter the URL they want to shorten.
    • Optional input for a custom alias for the shortened URL.
    • A "Shorten" button to trigger the shortening process.
  3. Shortened URL Display:

    • Post-shortening, the interface shows the shortened URL.
    • Provides a clickable link and a copy-to-clipboard button.

User Authentication

  1. Registration Form:

    • Titled "Create Account."
    • Fields for email, password, and password confirmation.
    • A "Register" button to create a new account.
  2. Login Form:

    • Titled "Sign In."
    • Fields for email and password.
    • A "Login" button for user authentication.

Dashboard for Users

  • Lists shortened URLs with original links, shortened versions, and click stats.
  • Offers edit and delete options for each URL.
  • Summarizes user's activity and account details.

Organization and Team Management

  • Interface for creating and managing organizations and teams.
  • Functionality to add or remove members.
  • Displays URLs and analytics specific to the organization.

Responsive Design

  • Utilizes Tailwind CSS for a responsive layout, ensuring usability across various devices.
  • Adopts a mobile-first design approach for optimal user experience on smaller screens.

Interactive Elements

  • StimulusJS enhances form interactions, real-time updates, and user feedback, ensuring a dynamic and responsive user interface.

This frontend structure aims to provide a clear, efficient, and engaging user experience, facilitating straightforward URL shortening and effective management of user and organizational data.

Backend

The backend of the URL shortener service, built with Ruby on Rails, is the powerhouse of the application, orchestrating data management, user interactions, and system logic. Here's a breakdown of its core components and their responsibilities:

Ruby on Rails Framework

  • Routing: Rails manages all incoming web requests and directs them to the appropriate controller actions, ensuring that users' interactions with the frontend seamlessly trigger backend processes.
  • Controllers: Act as intermediaries between models and views, handling the business logic for user requests, processing data, and determining the output or next steps in the workflow.
  • Models: Define the structure of the database and the business logic of the application. They interact directly with the database, retrieving, saving, or updating data as needed.
  • Views: Although primarily handled on the frontend, Rails views can deliver dynamic HTML content, integrating server-side logic with HTML templates through ERB.

User Authentication with Devise

  • Devise is configured to handle user authentication, offering a secure and flexible solution for managing user sessions, registrations, and passwords.
  • It provides out-of-the-box modules for common authentication needs, including password reset, email confirmation, and user tracking.
  • Devise also allows for customization and extension to accommodate unique requirements, such as additional user fields or authentication methods.

Authorization with Pundit

  • Pundit is utilized to define and enforce authorization policies, ensuring users can only access or modify resources they're permitted to.
  • It integrates with the Rails controller layer, allowing for a readable and maintainable approach to implementing authorization rules.
  • Policies are defined for each resource, specifying what actions different types of users can perform, aligning with the business logic and security requirements of the service.

URL Shortening Logic

  • The core feature, URL shortening, involves generating a unique, concise identifier for each original URL.
  • When a user submits a URL, the backend generates this identifier, stores the mapping in the database, and returns the shortened version to the user.
  • Upon visiting a shortened URL, the backend retrieves the original URL using the identifier, redirects the user, and increments any relevant metrics, like click counts.

Database Interaction

  • Rails models communicate with PostgreSQL, handling data persistence and retrieval. This includes operations for creating new shortened URLs, fetching existing ones, user data management, and handling organizational structures.
  • The backend ensures data integrity and consistency, employing validations, transactions, and appropriate indexing to optimize performance and reliability.

By managing these key aspects, the backend ensures the URL shortener service is robust, secure, and efficient, capable of handling user requests accurately and promptly while maintaining high performance and data integrity.

URL Shortening Logic

  • Unique Identifier Generation: When a user submits a URL, the system generates a unique identifier, typically a string or hash. This identifier serves as the path for the shortened URL.
  • Persistence: The original URL and its corresponding identifier are stored in the database. The identifier must be unique to prevent collisions.
  • Redirection: When the shortened URL is accessed, the system uses the identifier to retrieve the original URL from the database and redirects the user to it.

Database Design

  • PostgreSQL: Hosted on AWS RDS, provides reliable storage for user data, URL mappings, and organizational structures.
  • Schema: Includes tables for users, URLs, organizations, and teams, with appropriate relationships and indexes for performance.

Background Processing and Caching

  • Redis & Sidekiq: Redis serves as a cache and a store for Sidekiq's background jobs, such as batch processing or sending emails.
  • Sidekiq: Processes background tasks asynchronously, improving responsiveness and scalability.

Infrastructure and Deployment

AWS Services

  • ECS & Fargate: Hosts the Dockerized Rails application, offering scalability and simplified operations.
  • RDS & Elasticache: Provides managed services for PostgreSQL and Redis, ensuring performance and scalability.
  • ELB: Distributes incoming traffic, enhancing availability and fault tolerance.

CI/CD with GitHub Actions

  • Automation: Implements pipelines for testing, building, and deploying the application, integrated with AWS ECS.
  • Docker Integration: Utilizes Docker for consistent environments across development, testing, and production.

Terraform for IaC

  • AWS Resource Management: Defines and provisions AWS resources, ensuring consistent and reproducible infrastructure setups.
  • Versioning: Tracks infrastructure changes alongside application code, improving maintainability and auditability.

Security Measures

  • HTTPS & TLS: Ensures secure data transmission, with TLS certificates managed through AWS or a third-party provider.
  • Data Security: Adopts best practices for data encryption, secure password handling, and user session management.

Monitoring, Logging, and CDN

  • CloudWatch & Axiom: Utilizes AWS CloudWatch for basic monitoring and Axiom for advanced log management, providing insights into application health and performance.
  • Cloudflare: Integrates Cloudflare for CDN services, improving content delivery speed and offering additional security layers.

Mermaid Diagrams

System Component Interaction

graph TD
    User[User] -->|Interacts with| Frontend[Frontend]
    Frontend -->|Requests data| Backend[Backend]
    Backend -->|Fetches from| Database[PostgreSQL]
    Backend -->|Interacts with| Cache[Redis]
    Backend -->|Enqueues jobs| Queue[Sidekiq]
    Queue -->|Processes jobs| Backend
Loading

CI/CD Pipeline

sequenceDiagram
    participant Dev as Developer
    participant GH as GitHub Actions
    participant AWS as AWS ECS
    Dev->>GH: Push code to repo
    GH->>GH: Run tests
    GH->>GH: Build Docker image
    GH->>AWS: Deploy to ECS
Loading

This detailed design aims to provide a clear and comprehensive overview of the URL shortener service, outlining the interaction between various components, the infrastructure setup, and the deployment workflow to ensure a robust, scalable, and secure application.

URL Shortener Service Implementation Checklist

Frontend Development

  • Set up the Rails environment and install necessary gems including Tailwind CSS and StimulusJS.
  • Develop the homepage with navigation and service information.
  • Create the URL shortening form with fields for URL input and custom alias.
  • Implement the logic for the "Shorten" button to interact with the backend.
  • Design the shortened URL display section with clickable links and a copy-to-clipboard feature.
  • Build the user registration and login forms using Devise.
  • Develop the user dashboard to list, edit, and delete shortened URLs and view statistics.
  • Create the organization and team management interface.
  • Ensure the entire frontend is responsive and follows a mobile-first design approach.
  • Integrate interactive elements with StimulusJS for dynamic content updates without page reloads.

Backend Development

  • Set up the Rails backend structure with appropriate MVC architecture.
  • Configure routing to handle web requests efficiently.
  • Develop controllers to process incoming data and interact with models.
  • Set up models for users, URLs, and organizations, defining relationships and validations.
  • Implement the URL shortening logic to generate and store unique identifiers.
  • Integrate Devise for user authentication and session management.
  • Implement Pundit for handling authorization rules and policies.
  • Establish connections with the PostgreSQL database for data management.
  • Configure Redis for caching and Sidekiq for background job processing.
  • Ensure robust error handling and data validation throughout the backend.

Database Configuration

  • Install and configure PostgreSQL.
  • Design and implement the database schema, including tables for users, URLs, and organizations.
  • Set up indexes and foreign keys to optimize query performance and ensure data integrity.
  • Implement migrations for scalable database changes.

Infrastructure Setup and Deployment

  • Define the AWS infrastructure using Terraform, specifying resources for ECS, RDS, Elasticache, and ELB.
  • Configure ECS with Fargate for scalable container management.
  • Set up RDS for the PostgreSQL database and Elasticache for Redis caching.
  • Implement load balancing with ELB to distribute incoming traffic.
  • Develop CI/CD pipelines using GitHub Actions to automate testing, building, and deployment processes.
  • Ensure secure handling of secrets and credentials using AWS management tools or environment variables.

Security and Monitoring

  • Implement HTTPS and TLS for secure communication.
  • Configure data encryption for at-rest and in-transit data.
  • Set up monitoring with CloudWatch and log management with tools like Axiom.
  • Integrate Cloudflare or a similar CDN for enhanced performance and security.

Testing and Quality Assurance

  • Write unit and integration tests for both frontend and backend components.
  • Perform user acceptance testing to ensure the system meets requirements.
  • Conduct security and performance audits to identify and mitigate potential issues.

Final Steps

  • Review the entire system to ensure compliance with the initial design document.
  • Prepare documentation for the system, including user guides and technical details.
  • Deploy the system to production and monitor its performance and stability.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment