MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Rate Limiting & Throttling0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
7.1API Versioning Strategies7.2Rate Limiting & Throttling7.3Best Practices
HomeTutorialsREST API Development with DjangoRate Limiting & Throttling
Best Practices

Rate Limiting & Throttling

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

DRF Throttling: AnonRate, UserRate, and Scoped Throttles

Unrestricted APIs are vulnerable to brute-force attacks, scraping, and Denial of Service (DoS) outages. Throttling (Rate Limiting) restricts the number of requests a client can issue within a given time window. Django REST Framework includes a robust, cache-backed throttling engine supporting anonymous rate limits, authenticated user tiers, and scoped endpoint limits.


1. How Throttling Operates in DRF

DRF tracks request counts in Django's cache backend (typically Redis or Memcached). On every incoming request:

  1. 1
    DRF identifies the client: by IP address for anonymous users, or by user_id for authenticated users.
  2. 2
    It checks request history in the cache against the configured rate.
  3. 3
    If the threshold is exceeded, DRF raises Throttled, returning HTTP 429 Too Many Requests with a Retry-After header.

2. Built-in Throttle Classes

  • AnonRateThrottle: Limits unauthenticated users based on their IP address (REMOTE_ADDR or X-Forwarded-For).
  • UserRateThrottle: Limits authenticated users based on their unique user.pk.
  • ScopedRateThrottle: Applies custom limits to specific sensitive views via a throttle_scope attribute (e.g. login, checkout, password reset).

3. Global Configuration in settings.py

1# config/settings.py
2REST_FRAMEWORK = {
3 "DEFAULT_THROTTLE_CLASSES": [
4 "rest_framework.throttling.AnonRateThrottle",
5 "rest_framework.throttling.UserRateThrottle",
6 ],
7 "DEFAULT_THROTTLE_RATES": {
8 "anon": "60/minute", # Max 60 requests/min per IP
9 "user": "1000/hour", # Max 1000 requests/hour per authenticated user
10 "burst": "10/second", # Burst protection
11 "auth_login": "5/minute", # Sensitive brute-force protection
12 "payment": "3/minute", # Payment mutation guard
13 },
14}

Allowed rate time periods: second, minute, hour, day (e.g. "100/day", "5/sec").


4. Applying ScopedRateThrottle to Sensitive Endpoints

Guarding authentication endpoints against credential stuffing and brute-force attacks:

1# accounts/views.py
2from rest_framework.views import APIView
3from rest_framework.throttling import ScopedRateThrottle
4from rest_framework.response import Response
5 
6class LoginAPIView(APIView):
7 # Enforce scoped rate limit
8 throttle_classes = [ScopedRateThrottle]
9 throttle_scope = "auth_login" # Maps to 'auth_login': '5/minute' in settings!
10 
11 def post(self, request):
12 # Even if attacker has thousands of passwords, they can only attempt 5/min!
13 ...

5. Tiered Subscription Rate Limiting (SaaS Plans)

For SaaS platforms with Free, Pro, and Enterprise pricing tiers, create a custom throttle class:

1# api/throttles.py
2from rest_framework.throttling import UserRateThrottle
3 
4class SubscriptionTierThrottle(UserRateThrottle):
5 def get_rate(self):
6 user = self.request.user
7 if not user or not user.is_authenticated:
8 return "60/minute"
9 
10 tier = getattr(user, "subscription_tier", "free")
11 if tier == "enterprise":
12 return "10000/hour"
13 elif tier == "pro":
14 return "3000/hour"
15 return "500/hour"

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What HTTP status code is returned when a client breaches an API throttle limit?
Question 2
How does AnonRateThrottle identify unauthenticated callers?
Question 3
Where does DRF store request timestamps and counters for active throttles?
Question 4
How does ScopedRateThrottle determine which rate limit to enforce on a view?
Question 5
What header informs the client how many seconds they must wait before sending another request after being throttled?
Next Lesson

Best Practices

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
API Versioning StrategiesBest Practices

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext