A high-performance leaderboard API for gaming applications, built with Django REST Framework and Redis. Designed to handle hundreds of concurrent users with sub-second response times.
SpeedRank-API provides a blazing-fast leaderboard system optimized for real-time gaming scenarios. It uses Redis Sorted Sets for lightning-fast queries while maintaining SQLite persistence for data durability.
- ⚡ Ultra-Fast Queries: Top 10 players and rank lookups in < 1ms using Redis
- 📊 Real-Time Leaderboards: Handle 500+ concurrent users with 200+ req/s
- 🔄 Dual Persistence: Redis for speed, SQLite for durability
- ✅ Input Validation: Points range (0-1M), automatic player creation
- 📈 Load Tested: Verified with Locust under production-like conditions
- 🔧 Management Commands: Easy SQL-to-Redis synchronization
┌─────────────┐
│ Client │
└──────┬──────┘
│
▼
┌─────────────────────────────┐
│ Django REST Framework │
│ ┌──────────────────────┐ │
│ │ API Endpoints │ │
│ │ - POST /api/score/ │ │
│ │ - GET /api/top-10/ │ │
│ │ - GET /api/rank/ │ │
│ └──────────────────────┘ │
└─────┬────────────────┬──────┘
│ │
▼ ▼
┌─────────┐ ┌──────────┐
│ Redis │ │ SQLite │
│ (Speed) │ │ (Persist)│
└─────────┘ └──────────┘
Data Flow:
- Reads (top-players, my-rank): Redis only → O(log N) performance
- Writes (submit-score): SQLite + Redis → Dual persistence
- Sync: Management command populates Redis from SQLite
Tested with Locust at 500 concurrent users:
| Metric | Value | Target |
|---|---|---|
| Peak RPS | 200-220 req/s | ✅ |
| 50th Percentile | 200-1,000ms | ✅ |
| 95th Percentile | 1-4 seconds | ✅ |
| Failure Rate | ~0% | ✅ |
| Concurrent Users | 500+ | ✅ |
| Operation | SQL (Before) | Redis (After) | Improvement |
|---|---|---|---|
| Top 10 Players | 5-10 seconds | < 1ms | 🚀 5000x faster |
| Get User Rank | 5-60 seconds | < 1ms | 🚀 5000x faster |
| Submit Score | 1-2 seconds | 1-4 seconds |
POST /api/score/
Content-Type: application/json
{
"username": "player123",
"points": 95000
}Response:
{
"message": "Score submitted"
}Validations:
- Points: 0 - 1,000,000
- Username: auto-creates player if new
- Only keeps highest score per player in Redis
GET /api/top-players/Response:
[
{
"rank": 1,
"username": "player123",
"points": 95000
},
{
"rank": 2,
"username": "speedrunner",
"points": 89500
},
...
]GET /api/my-rank/?username=player123Response:
{
"username": "player123",
"rank": 1,
"points": 95000
}Error Response (404):
{
"message": "Player not found in leaderboard."
}- Python 3.12+
- Redis Server 7.x
- pip (Python package manager)
-
Clone the repository
git clone <repository-url> cd SpeedRank-API
-
Create virtual environment
python3 -m venv .venv source .venv/bin/activate # Linux/Mac # or .venv\Scripts\activate # Windows
-
Install dependencies
pip install -r requirements.txt
-
Run migrations
python manage.py migrate
-
Start Redis server
redis-server # Or use your system's service manager: sudo systemctl start redis -
Sync existing data to Redis (if any)
python manage.py sync_redis
-
Run development server
python manage.py runserver
The API will be available at http://localhost:8000/api/
# Submit a score
curl -X POST http://localhost:8000/api/score/ \
-H "Content-Type: application/json" \
-d '{"username": "testplayer", "points": 50000}'
# Get top 10 players
curl http://localhost:8000/api/top-players/
# Check user rank
curl "http://localhost:8000/api/my-rank/?username=testplayer"-
Install Locust (already in requirements.txt)
pip install locust
-
Run load test
cd benchmarks locust -
Open browser
- Navigate to
http://localhost:8089 - Set number of users (e.g., 500)
- Set spawn rate (e.g., 10 users/second)
- Set host:
http://localhost:8000 - Start swarming
- Navigate to
-
Monitor metrics
- RPS (Requests Per Second)
- Response time percentiles
- Failure rates