![]() |
VOOZH | about |
Throttling in Django REST Framework (DRF) controls how many requests a client can make within a specific time. It helps keep the API stable and prevents overuse.
Django REST Framework (DRF) includes built-in throttling classes that control how many requests are allowed within a set time period.
AnonRateThrottle is used to limit the rate of requests made by unauthenticated users. It identifies each request using the IP address of the incoming connection, generating a unique key to track and throttle requests accordingly.
The allowed request rate can be determined from:
To enable throttling globally, define the throttle classes and rate limits in the project’s configuration:
The throttle rate can be defined using any of the following time units: second, minute, hour, or day, depending on the desired rate-limiting policy.
Example Request: The following HTTP i.e., command retrieves the list of robots without authentication.
http GET :8000/robot/
Output
HTTP/1.1 200 OK
Allow: GET, POST, HEAD, OPTIONS
Content-Language: en
Content-Length: 2106
Content-Type: application/json
Date: Sat, 02 Oct 2021 14:29:40 GMT
Referrer-Policy: same-origin
Server: WSGIServer/0.2 CPython/3.7.5
Vary: Accept, Accept-Language
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
[
{
"currency": "USD",
"currency_name": "US Dollar",
"manufacturer": "ABB",
"manufacturing_date": "2020-05-10T00:00:00Z",
"name": "IRB 1100",
"owner": "sonu",
"price": 25000,
"robot_category": "Articulated Robots",
"url": "http://localhost:8000/robot/7/"
},
{
"currency": "USD",
"currency_name": "US Dollar",
"manufacturer": "ABB",
"manufacturing_date": "2020-08-10T00:00:00Z",
"name": "IRB 120",
"owner": "sonu",
"price": 35000,
"robot_category": "Articulated Robots",
"url": "http://localhost:8000/robot/8/"
},
]
The AnonRateThrottle rate is configured as 2 API requests per day. This means an unauthenticated user cannot make more than two requests within a 24-hour period. If the number of requests exceeds this configured limit, the server responds with the following message:
👁 ImageUserRateThrottle controls the rate of requests sent by both authenticated and unauthenticated users.
The allowed request rate can be determined in two ways:
To configure throttling globally, update the REST_FRAMEWORK settings:
Throttling Rules:
Example Request: The following authenticated HTTPie command allows up to 5 requests per day:
http -a "sonu":"sn@pswrd" GET :8000/robot/
If the number of requests exceeds the configured limit (5 per day), the API will respond with a 'Too Many Requests' (HTTP 429) error.
ScopedRateThrottle allows controlling the rate of requests for specific features or views in a RESTful API. To use it, the target view must include the throttle_scope property.
Adding a Throttle Scope: For example, the RobotDetail class can have a throttle scope for robot-related endpoints. First, import the ScopedRateThrottle.
from rest_framework.throttling import ScopedRateThrottle
Add the throttle scope and class to the view:
Configuring the Throttle Rate: Next, add the throttle rate for the robots scope in the REST framework settings.
The robots scope allows a maximum of 3 requests per day.
Example Request: The following authenticated HTTP i.e. command retrieves a robot by ID using the RobotDetail view:
http -a "sonu":"sn@pswrd" GET :8000/robot/2/
Output
👁 ImageIf the number of requests exceeds the scoped limit (3 per day), the API will respond with a "429 Too Many Requests" error.
For example, the 4th request within a day will return:
👁 Image