A production-ready URL shortener built with Django, featuring asynchronous click analytics, Redis caching, and Celery-powered background task processing. This application demonstrates modern web development practices and distributed system design patterns.
- URL Shortening — Generate short, unique codes for long URLs
- Analytics Dashboard — Track clicks, referrers, user agents, and timestamps
- Redis Caching — Sub-millisecond redirect performance with cache-aside pattern
- Asynchronous Processing — Non-blocking click tracking using Celery
- Rate Limiting — Protect endpoints from abuse (10 req/min per IP)
- URL Expiration — Optional expiry dates for temporary links
- REST API — Clean JSON API with Django REST Framework
- Docker Support — Full stack containerization with Docker Compose
| Layer | Technology |
|---|---|
| Backend | Django 5.x + Django REST Framework |
| Frontend | React + TypeScript + Vite |
| Task Queue | Celery |
| Message Broker | Redis |
| Cache | Redis (django-redis) |
| Database | PostgreSQL |
| Rate Limiting | django-ratelimit |
| Containerization | Docker + Docker Compose |
url_shortener/
├── docker-compose.yml
├── requirements.txt
├── .env
├── manage.py
├── config/ # Django project settings
│ ├── settings.py
│ ├── urls.py
│ ├── celery.py # Celery app initialization
│ └── wsgi.py
├── shortener/ # Main Django app
│ ├── models.py # ShortenedURL, ClickEvent
│ ├── serializers.py # DRF serializers
│ ├── views.py # API views
│ ├── urls.py # App-level URL routing
│ ├── tasks.py # Celery tasks
│ ├── utils.py # Short code generation
│ └── admin.py # Django admin registration
└── frontend/ # React frontend
├── src/
├── package.json
└── vite.config.ts
- Python 3.9+
- Node.js 16+
- Docker & Docker Compose (optional but recommended)
-
Clone the repository
git clone <your-repo-url> cd url_shortener
-
Set up environment variables
Create a
.envfile in the root directory:SECRET_KEY=your-secret-key-here DEBUG=True DB_NAME=url_shortener_db DB_USER=postgres DB_PASSWORD=postgres DB_HOST=localhost DB_PORT=5432 REDIS_URL=redis://localhost:6379/0
-
Using Docker (Recommended)
docker compose up -d
This will start PostgreSQL, Redis, Django web server, and Celery worker.
-
Manual Setup
Backend:
# Create virtual environment python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate # Install dependencies pip install -r requirements.txt # Start PostgreSQL and Redis (via Docker or locally) docker compose up -d db redis # Run migrations python manage.py migrate # Create superuser (optional) python manage.py createsuperuser # Start Django server python manage.py runserver # In a new terminal, start Celery worker celery -A config worker --loglevel=info
Frontend:
cd frontend npm install npm run dev
POST /api/shorten/
Content-Type: application/json
{
"original_url": "https://example.com/very/long/url"
}Response:
{
"short_code": "abc123X",
"short_url": "http://localhost:8000/abc123X",
"original_url": "https://example.com/very/long/url",
"created_at": "2026-05-27T10:30:00Z"
}GET /<short_code>/Returns HTTP 302 redirect to the original URL.
GET /api/analytics/<short_code>/Response:
{
"short_code": "abc123X",
"original_url": "https://example.com/very/long/url",
"click_count": 42,
"created_at": "2026-05-27T10:30:00Z",
"recent_clicks": [
{
"clicked_at": "2026-05-27T11:00:00Z",
"ip_address": "192.168.1.1",
"user_agent": "Mozilla/5.0...",
"referer": "https://google.com"
}
]
}This project implements several production-grade patterns:
| Pattern | Implementation | Why It Matters |
|---|---|---|
| Cache-Aside | Redis cache for URL lookups | Reduces DB load, ~1ms vs ~10ms response time |
| Cache Warming | Pre-load popular URLs | Proactive performance optimization |
| Async Task Queues | Celery for click tracking | Non-blocking user experience |
| Race Condition Prevention | Django F() expressions |
Thread-safe counter increments |
| Rate Limiting | Per-IP request throttling | Prevent abuse and DoS attacks |
| Database Indexing | Composite indexes on queries | Fast analytics queries |
| At-Least-Once Delivery | Celery retry mechanism | Reliable event processing |
Run the test suite:
python manage.py testThe tests cover:
- Short code generation and uniqueness
- URL validation and serialization
- Redirect logic with caching
- Analytics data aggregation
- Rate limiting enforcement
- Error handling (404, 429, 410)
- Redirect latency: <2ms (with cache hit)
- Throughput: 1000+ redirects/sec (single instance)
- Cache hit rate: ~95% for popular URLs
- Background processing: Asynchronous click recording (no user-facing delay)
- Django Documentation
- Django REST Framework
- Celery with Django
- django-redis Documentation
- Redis Caching Patterns
This project is open source and available under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.