Skip to content

VivekYadav-77/URL-Shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

44 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Shortly β€” Secure & Fault-Tolerant URL Shortener

A production-inspired URL shortener built with Node.js, Express, MongoDB, Redis, and React, focused on security, performance, and fault-tolerant backend design.

Unlike typical URL shorteners, this system is designed to continue functioning even if Redis (cache layer) is unavailable, ensuring reliability under real-world failure conditions.


🧠 Core Engineering Principles

  • Fault Tolerance First β†’ System works even if Redis fails
  • MongoDB as Source of Truth β†’ No dependency on cache correctness
  • Non-blocking Request Flow β†’ Fast redirects, async updates
  • Security + Performance Balance β†’ Avoid over-engineering

πŸ—οΈ System Architecture

Client
  ↓
Express API (Render)
  ↓
Redis (optional cache)
  ↓
MongoDB (primary database)

πŸš€ Features

πŸ‘€ User Features

  • Shorten long URLs
  • Optional custom alias (unique enforced)
  • Expiry control (default: 5 days, max: 7 days)
  • URL history with metadata
  • Click tracking
  • Disable & delete URLs
  • Automatic expiry handling

πŸ›‘οΈ Security Features

  • URL risk analysis (phishing & suspicious pattern detection)
  • Google Safe Browsing integration
  • VirusTotal scanning
  • Cached scan results (avoid re-scanning safe URLs)
  • IP-based rate limiting (Redis-backed, fault-tolerant)
  • Admin-enforced blocking (disabled/deleted URLs)

πŸ§‘β€πŸ’Ό Admin Features

  • View security logs
  • Detect high-risk URL attempts
  • Disable or delete malicious URLs
  • Manual moderation controls

πŸ” Redirect Flow (Optimized & Reliable)

1. User hits /:shortCode
2. Try Redis cache
   β†’ If hit β†’ validate β†’ redirect
3. Cache miss β†’ query MongoDB
4. Validate (active / not expired / not deleted)
5. Redirect immediately
6. Cache result (non-blocking)
7. Increment clicks asynchronously

πŸ’₯ Failure Handling (Real-World Thinking)

Redis Failure

  • System falls back to MongoDB
  • No broken redirects
  • Only performance degradation

High Traffic

  • Rate limiter prevents abuse
  • Cache reduces database load

Expired / Invalid URLs

  • Returns static error page (410 Gone)

πŸ” URL Security Pipeline

User Input URL
   ↓
Validation (Zod)
   ↓
Admin Flag Check
   ↓
Risk Score Analysis
   ↓
Recent Safe Scan? β†’ Skip
   ↓
Google Safe Browsing
   ↓
VirusTotal Scan
   ↓
Security Logs Stored
   ↓
URL Created

🧠 Smart Scan Optimization

  • Safe URLs are not re-scanned within 2 weeks
  • Security logs stored in SecurityLog collection
  • High-risk URLs blocked early
  • Reduces API cost and improves performance

⚑ Redis Usage (Optimized)

  • URL caching β†’ url:{shortCode}
  • Rate limiting β†’ rl:{ip}
  • Minimal Redis operations per request
  • Redis treated as non-critical dependency

🧱 Tech Stack

Backend

  • Node.js + Express
  • MongoDB (Mongoose)
  • Redis (Upstash)
  • Zod (validation)
  • Helmet, HPP

Frontend

  • React + Vite
  • Redux Toolkit
  • Tailwind CSS

πŸ“Š Performance Considerations

  • Indexed lookup on shortCode
  • Lean queries (.lean())
  • Reduced Redis calls per request
  • Async DB updates for clicks
  • Cache TTL optimization (24h)

πŸ•’ Background Jobs

Job Purpose
Expiry Job Marks expired URLs
(Optional) Redis Sync Can sync stats if needed

πŸ“‚ Project Structure

URL-SHORTENER/
β”‚
β”œβ”€β”€ url-shortener-backend/            # Express.js Server Logic
β”‚   β”œβ”€β”€ config/                       # Database & Service configs (e.g., redis)
β”‚   β”œβ”€β”€ controllers/                  # API Logic (Auth, URL, Admin, Security)
β”‚   β”œβ”€β”€ crons/                        # Scheduled Background Tasks
β”‚   β”œβ”€β”€ jobs/                         # Worker processes or specific task logic
β”‚   β”œβ”€β”€ middleware/                   # Security, Auth, and Rate Limiting
β”‚   β”œβ”€β”€ models/                       # Mongoose Schemas (User, URL, Logs)
β”‚   β”œβ”€β”€ public/                       # Static Assets & Error Pages
β”‚   β”œβ”€β”€ routes/                       # API Route Definitions
β”‚   β”œβ”€β”€ security/                     # Advanced Security Guards & Analyzers
β”‚   β”œβ”€β”€ utils/                        # Shared Helpers (ApiError, tokens, etc.)
β”‚   β”œβ”€β”€ app.js                        # App initialization
β”‚   β”œβ”€β”€ index.js                      # Server Entry Point
β”‚   └── tredish.js                    # Redis client initialization
β”‚
β”œβ”€β”€ url-shortener-frontend/           # React + Vite Frontend
β”‚   β”œβ”€β”€ public/                       # Assets (logo.svg, Vivek.png)
β”‚   β”œβ”€β”€ src/                          # Application Source Code
β”‚   β”‚   β”œβ”€β”€ App/                      # Core App wrappers
β”‚   β”‚   β”œβ”€β”€ components/               # Global reusable UI (Navbar, Inputs)
β”‚   β”‚   β”œβ”€β”€ Features/                 # Business logic modules
β”‚   β”‚   β”œβ”€β”€ Pages/                    # View Components (Dashboard, Profile)
β”‚   β”‚   β”œβ”€β”€ utils/                    # API clients and Formatters
β”‚   β”‚   β”œβ”€β”€ App.jsx                   # Root Component
β”‚   β”‚   β”œβ”€β”€ index.css                 # Global Styles
β”‚   β”‚   └── main.jsx                  # React Entry Point
β”‚   β”œβ”€β”€ index.html                    # Main HTML Shell
β”‚   β”œβ”€β”€ vercel.json                   # Deployment configuration
β”‚   └── vite.config.js                # Vite build settings
β”‚
β”œβ”€β”€ .gitignore                        # Root git ignore
└── README.md                         # Main Project Documentation

πŸ§ͺ Validation Rules

  • Custom alias must be unique
  • Expiry limited to 7 days
  • Safe handling of empty fields
  • Strict schema validation using Zod

πŸ“Έ Screenshots

πŸ§‘β€πŸ’Ό Admin Dashboard

Admin Dashboard

πŸ‘€ User Dashboard

User Dashboard

βš™οΈ Environment Variables

Backend

PORT=5000
MONGO_URL=your_mongodb_uri
REDIS_URL=your_upstash_url
REDIS_TOKEN=your_upstash_token
JWT_ACCESS_SECRET=your_secret
JWT_REFRESH_SECRET=your_secret
VIRUS_TOTAL_API_KEY=your_key
GOOGLE_SAFE_BROWSING_KEY=your_key

Frontend

VITE_B_LOCATION=http://localhost:5000

▢️ Run Locally

# Backend
npm install
node index.js

# Frontend
npm install
npm run dev

🎯 Why This Project Stands Out

This project demonstrates:

  • Fault-tolerant backend design
  • Real-world caching strategy (Redis as optional)
  • Security-first URL handling
  • Clean architecture & separation of concerns
  • Practical trade-offs instead of overengineering

πŸ‘€ Author

Vivek Focused on building reliable, scalable, real-world systems


About

Production-grade URL shortener with security-first design. Built using Node.js, Express, MongoDB, Redis, and React, featuring URL risk analysis, Google Safe Browsing & VirusTotal scanning, abuse detection, Redis caching, rate limiting, expiry enforcement, and admin moderation. Designed to handle real-world threats, performance constraints, and edge

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages