Skip to content

Add rate limiting and caching to API endpoints#2

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/add-rate-limits-and-caching
Draft

Add rate limiting and caching to API endpoints#2
Copilot wants to merge 6 commits intomainfrom
copilot/add-rate-limits-and-caching

Conversation

Copy link

Copilot AI commented Nov 14, 2025

Implements API rate limiting and response caching to prevent abuse and improve performance.

Changes

Rate Limiting

  • Applied django-ratelimit decorators to all API endpoints with IP/user-based limits
  • Configured DRF throttling: 100 req/h anonymous, 1000 req/h authenticated
  • Per-endpoint limits: health check 200/h, ask question 50/h, conversations 100/h

Caching

  • Local memory cache (dev) with Redis support (prod) in settings.py
  • Health check cached 1 min, user stats cached 5 min with auto-invalidation
  • Cache keys scoped per-user to prevent data leakage

New REST API Endpoints

  • GET /api/health/ - cached health check
  • GET /api/conversations/stats/ - cached user statistics
  • DELETE /api/conversations/<uuid>/ - conversation deletion with cache invalidation

Implementation Example

@api_view(['GET'])
@throttle_classes([HealthCheckRateThrottle])
@cache_page(60)
def api_health_check(request):
    # Cached for 1 minute, rate limited to 200/hour
    return Response({'status': 'healthy'})

@api_view(['GET'])
@throttle_classes([ConversationRateThrottle])
def api_conversation_stats(request):
    cache_key = f'conversation_stats_{request.user.id}'
    cached_data = cache.get(cache_key)
    if cached_data:
        return Response(cached_data)
    # ... query and cache for 5 minutes

Testing

  • 12 tests covering rate limiting, caching, authentication, and cache invalidation
  • CodeQL: 0 vulnerabilities

Configuration

Default uses local memory cache. For production with Redis:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.redis.RedisCache',
        'LOCATION': os.environ.get('REDIS_URL'),
    }
}

Dependencies

  • djangorestframework==3.16.1
  • django-ratelimit==4.1.0
  • redis==7.0.1
Original prompt

how can i add Rate limits and chaching in my app for api ?


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Nov 14, 2025
Copilot AI and others added 5 commits November 14, 2025 22:42
Co-authored-by: teman67 <48212448+teman67@users.noreply.github.com>
Co-authored-by: teman67 <48212448+teman67@users.noreply.github.com>
Co-authored-by: teman67 <48212448+teman67@users.noreply.github.com>
Co-authored-by: teman67 <48212448+teman67@users.noreply.github.com>
Co-authored-by: teman67 <48212448+teman67@users.noreply.github.com>
Copilot AI changed the title [WIP] Add rate limits and caching for API Add rate limiting and caching to API endpoints Nov 14, 2025
Copilot AI requested a review from teman67 November 14, 2025 22:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants