Skip to content

⚡ Scalable URL shortener built with Node.js, Express, MongoDB & Redis — handles 150+ req/sec (~540k/hr, ~13M/day) with caching, rate limiting & load balancing

Notifications You must be signed in to change notification settings

Stitipragyanbarik/MakeUrlShort

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

# URL Shortener App A high-performance, production-ready URL shortening service built with Node.js, Express, MongoDB, and Redis. Designed to handle 150+ requests per second with built-in rate limiting, caching, and load balancing capabilities. ## 🚀 Features - **High Performance**: Optimized for 150+ requests per second - **URL Shortening**: Generate short URLs with customizable nanoid-based identifiers (random or user-defined) - **Custom Short IDs**: Users can specify their own short ID (3-20 characters, alphanumeric + hyphens/underscores) - **Caching**: Redis-based caching for fast URL resolution - **Rate Limiting**: Built-in rate limiting to prevent abuse - **CORS Support**: Configurable CORS for frontend integration - **Docker Ready**: Complete Docker setup for production deployment - **Load Balancing**: Connection pooling and request queuing - **Monitoring**: Health checks and performance metrics endpoints - **Security**: Helmet.js security middleware and input validation ## 🛠 Tech Stack ### Backend - **Node.js** - Runtime environment - **Express.js** - Web framework - **MongoDB** - Primary database for URL storage - **Redis** - Caching layer - **Docker** - Containerization - **Artillery** - Load testing ### Frontend - **React/Vite** - Modern frontend framework - **Axios** - HTTP client with interceptors - **Docker** - Containerization ## 📋 Prerequisites - Node.js 18+ - Docker & Docker Compose - MongoDB Atlas account (or local MongoDB) - Redis (local or cloud) ## 🔧 Installation ### Local Development 1. **Clone the repository** ```bash git clone cd UrlShortnerApp ``` 2. **Backend Setup** ```bash cd BACKEND npm install cp .env.example .env # Configure your environment variables npm run dev ``` 3. **Frontend Setup** ```bash cd frontend npm install npm run dev ``` ### Docker Production Setup 1. **Build and run with Docker Compose** ```bash # From the frontend directory (contains docker-compose.yml) cd frontend docker-compose up --build ``` 2. **Access the application** - Frontend: http://localhost:5173 - Backend API: http://localhost:3000 - Health Check: http://localhost:3000/health ## 📖 Usage ### Web Interface 1. Open the frontend application 2. Enter a long URL in the input field 3. Click "Shorten URL" 4. Copy the generated short URL ### API Usage #### Create Short URL ```bash POST /api/create Content-Type: application/json { "originalUrl": "https://example.com/very/long/url", "customShortId": "mylink" // Optional: 3-20 characters, alphanumeric + hyphens/underscores } ``` **Response:** ```json { "shortUrl": "http://localhost:3000/mylink", "originalUrl": "https://example.com/very/long/url", "custom": true } ``` **Custom Short ID Rules:** - Length: 3-20 characters - Allowed characters: Letters, numbers, hyphens (-), underscores (_) - Reserved words: api, health, metrics, admin, create, shorten - Must be unique #### Redirect to Original URL ```bash GET /:shortId ``` Redirects to the original URL ## 🔍 API Endpoints | Method | Endpoint | Description | |--------|----------|-------------| | POST | `/api/create` | Create a short URL | | GET | `/:shortId` | Redirect to original URL | | GET | `/health` | Health check | | GET | `/metrics` | Performance metrics | | POST | `/metrics/reset` | Reset metrics | ## ⚙️ Configuration ### Environment Variables Create a `.env` file in the BACKEND directory: ```env PORT=3000 HOST=127.0.0.1 MONGO_URI=mongodb://localhost:27017/urlshortener REDIS_HOST=localhost REDIS_PORT=6379 ALLOWED_ORIGINS=http://localhost:5173,http://localhost:5174 NODE_ENV=production ``` ### Docker Environment For Docker deployment, configure these in your docker-compose.yml: ```yaml environment: - MONGO_URI=mongodb://mongodb:27017/urlshortener - REDIS_HOST=redis - ALLOWED_ORIGINS=http://localhost:5173 ``` ## 🧪 Load Testing The application is optimized and tested for 150 requests per second using Artillery. ### Run Load Test ```bash # Install Artillery globally npm install -g artillery # Run the load test artillery run load-test.yml ``` ### Sample Load Test Configuration ```yaml config: target: 'http://localhost:3000' phases: - duration: 60 arrivalRate: 150 scenarios: - name: 'Create short URL' weight: 80 requests: - post: url: '/api/create' json: originalUrl: 'https://example.com/test' - name: 'Access short URL' weight: 20 requests: - get: url: '/abc123' ``` ## 🚀 Deployment ### Production Checklist - [ ] Configure production MongoDB (Atlas recommended) - [ ] Set up Redis (Redis Cloud ) - [ ] Configure ALLOWED_ORIGINS for your domain - [ ] Set NODE_ENV=production - [ ] Enable SSL/TLS - [ ] Set up monitoring and alerts - [ ] Configure load balancer if needed ### Docker Production Deployment ```bash # Build production images docker-compose -f docker-compose.prod.yml up --build -d # Scale the backend service docker-compose up -d --scale backend=3 ``` ## 📊 Performance Metrics The application includes built-in performance monitoring: - **Rate Limiting**: 150 requests/second per IP - **Connection Pooling**: MongoDB connection pool size 200 - **Caching**: Redis-based URL caching - **Request Queuing**: Handles burst traffic up to 300 concurrent requests - **Health Checks**: Real-time system health monitoring ## 🔒 Security Features - **Helmet.js**: Security headers - **Rate Limiting**: Prevents abuse - **CORS**: Configurable cross-origin requests - **Input Validation**: URL validation and sanitization - **HTTPS Ready**: SSL/TLS support ## 🤝 Contributing 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Run tests and load tests 5. Submit a pull request ## 📞 Support For support and questions: - Create an issue in the repository - Check the health endpoint: `/health` - Review logs for debugging --- **Built with ❤️ for high-performance URL shortening** # MakeUrlShort

About

⚡ Scalable URL shortener built with Node.js, Express, MongoDB & Redis — handles 150+ req/sec (~540k/hr, ~13M/day) with caching, rate limiting & load balancing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published