IITM Backend API: Building Scalable Microservices
APIs are the backbone of modern applications. This backend service for IIT Madras demonstrates how to build robust, scalable APIs that can handle thousands of concurrent users.
Architecture
Client → Nginx (Load Balancer)
→ API Gateway (Rate Limiting)
→ Django REST Framework
→ PostgreSQL (Database)
→ Redis (Cache + Sessions)
Authentication & Security
JWT Token Implementation:
from rest_framework_simplejwt.tokens import RefreshToken
def get_tokens_for_user(user):
refresh = RefreshToken.for_user(user)
return {
'refresh': str(refresh),
'access': str(refresh.access_token),
}
# In views
class LoginView(APIView):
def post(self, request):
username = request.data['username']
password = request.data['password']
user = authenticate(username=username, password=password)
if user:
tokens = get_tokens_for_user(user)
return Response(tokens)
return Response({'error': 'Invalid credentials'}, status=401)
Performance Optimization
Redis Caching Strategy:
from django.core.cache import cache
def get_course_data(course_id):
cache_key = f'course_{course_id}'
data = cache.get(cache_key)
if data is None:
data = Course.objects.get(id=course_id)
cache.set(cache_key, data, 3600) # 1 hour
return data
Result: 95% cache hit rate, 200ms → 15ms response time.
Rate Limiting
Prevent abuse with throttling:
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/hour',
'user': '1000/hour'
}
}
API Documentation
Auto-generated docs with drf-spectacular:
from drf_spectacular.utils import extend_schema
@extend_schema(
request=CourseSerializer,
responses={200: CourseSerializer},
description="Create a new course"
)
def create_course(request):
# Implementation
pass
Swagger UI at /api/docs/ for interactive testing.
Results
- Performance: 500 req/s on single instance
- Uptime: 99.9% over 6 months
- Users: 2000+ concurrent students
- API Calls: 50,000+ daily
Key takeaway: Good APIs are invisible - they just work.