A URL shortening service with complete CRUD operations, Redis caching for performance, and rate limiting for protection against abuse.
Example Usage
Original URL: https://unsplash.com/photos/a-bull-with-large-horns-eating-grass-in-a-field-0XKM4QW5WUg
Shortened URL: http://localhost:5000/M2v33V
๐ฉ Problem
Long URLs are:
- Hard to share on social media
- Difficult to remember
- Ugly in print materials
- Not trackable
Existing solutions often lack:
- Full URL management capabilities
- Performance optimization
- Abuse protection
๐ฏ Goal
Build a URL shortening service that:
- Generates short, memorable URLs
- Redirects quickly with low latency
- Allows full URL management (CRUD)
- Handles high traffic efficiently
- Protects against abuse
โ Solution
I built a performant URL shortening service featuring:
- Fast URL shortening and redirection
- Redis caching for quick lookups
- Rate limiting for abuse prevention
- Full CRUD operations
- Containerized deployment
๐งฑ Core Features
URL Operations
- Shorten URL - Transform long URLs into short, shareable links
- Redirect - Seamlessly redirect to original URL
- Update - Modify the original URL for existing short links
- Delete - Remove short URLs when no longer needed
- Get Original - Retrieve the original URL from a short link
Performance & Security
- Redis Caching - Fast lookups for frequently accessed URLs
- Rate Limiting - Protect against abuse and spam
- Request Logging - Morgan for comprehensive request logs
Infrastructure
- Docker & Docker Compose - Easy containerized deployment
- MongoDB - Persistent URL storage
- Redis - High-speed caching layer
๐ Architecture
โโโโโโโโโโโโโโโ
โ Client โ
โโโโโโโโฌโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโ
โ Express.js โ
โ Server โ
โโโโโโโโฌโโโโโโโ
โ
โโโโโดโโโโ
โผ โผ
โโโโโโโโ โโโโโโโโโ
โRedis โ โMongoDBโ
โCache โ โ DB โ
โโโโโโโโ โโโโโโโโโ
๐ง Tech Stack
Runtime & Framework
- Node.js
- Express.js
Database
- MongoDB (persistent storage)
- Redis (caching layer)
Monitoring
- Morgan (request logging)
Infrastructure
- Docker
๐ Data Flow
Shortening Flow
- User submits long URL
- Generate unique short code
- Store in MongoDB
- Cache in Redis
- Return short URL
Redirect Flow
- User visits short URL
- Check Redis cache
- If miss, query MongoDB
- Update cache
- Redirect to long URL
โ๏ธ Key Technical Challenges
Unique Code Generation
Need short, unique, URL-safe codes.
Solution:
Base62 encoding with collision detection and retry logic.
Low Latency Redirects
Redirects must be fast for good UX.
Solution:
Redis caching with cache-aside pattern.
Abuse Prevention
Service vulnerable to spam and abuse.
Solution:
IP-based rate limiting with configurable thresholds.
Cache Consistency
Cache must stay in sync with database.
Solution:
Write-through caching with TTL-based expiration.
๐ Result
- Sub-10ms redirect latency (cached)
- Handles thousands of requests/second
- Full CRUD API
- Production-ready containerization
๐ก Learnings
- Redis dramatically improves read performance
- Rate limiting is essential for public APIs
- Base62 provides good balance of length and uniqueness
- Docker simplifies development and deployment
๐ฎ Future Improvements
- Click analytics and tracking
- Custom domains
- QR code generation
- Link expiration
- User authentication
๐ Links
๏ฟฝ Need Something Similar?
Need a high-performance API service with caching, rate limiting, or similar backend infrastructure? I can help.