0
0
Djangoframework~20 mins

Throttling for rate limiting in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Throttling Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a user exceeds the throttle limit in Django REST Framework?

Consider a Django REST Framework API view with a throttle class set to limit requests to 5 per minute per user. What is the expected behavior when a user sends the 6th request within the same minute?

Django
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView
from rest_framework.response import Response

class FivePerMinuteThrottle(UserRateThrottle):
    rate = '5/minute'

class ExampleView(APIView):
    throttle_classes = [FivePerMinuteThrottle]

    def get(self, request):
        return Response({'message': 'Request successful'})
AThe 6th request is accepted but logged for review later.
BThe 6th request resets the throttle count and is accepted normally.
CThe 6th request is blocked and the API returns a 429 Too Many Requests response.
DThe 6th request causes a server error (500 Internal Server Error).
Attempts:
2 left
πŸ’‘ Hint

Think about what HTTP status code is used when rate limits are exceeded.

πŸ“ Syntax
intermediate
2:00remaining
Identify the correct way to set a custom throttle rate in Django REST Framework

Which of the following code snippets correctly sets a throttle rate of 10 requests per hour for a custom throttle class?

A
class CustomThrottle(UserRateThrottle):
    rate = 10/hour
B
class CustomThrottle(UserRateThrottle):
    rate = '10/hour'
C
class CustomThrottle(UserRateThrottle):
    rate = 10 per hour
D
class CustomThrottle(UserRateThrottle):
    rate = '10 per hour'
Attempts:
2 left
πŸ’‘ Hint

Remember the rate must be a string with a number and time unit separated by a slash.

πŸ”§ Debug
advanced
2:00remaining
Why does the custom throttle class not limit requests as expected?

Given the following throttle class, users are not being limited to 3 requests per minute as intended. What is the cause?

Django
from rest_framework.throttling import UserRateThrottle

class ThreePerMinuteThrottle(UserRateThrottle):
    rate = '3/minute'
AThe rate value should be a string, so rate = '3/minute' is required.
BThe UserRateThrottle class does not support per-minute limits.
CThe throttle class must override the allow_request method to work.
DThe rate should be set in settings.py, not in the class.
Attempts:
2 left
πŸ’‘ Hint

Check the data type of the rate attribute.

🧠 Conceptual
advanced
2:00remaining
How does Django REST Framework differentiate users for throttling?

When using UserRateThrottle, how does Django REST Framework identify different users to apply rate limits?

AIt uses the user’s authentication credentials from request.user to identify unique users.
BIt uses the client IP address to identify users.
CIt applies the same limit to all users without differentiation.
DIt uses session cookies to track users.
Attempts:
2 left
πŸ’‘ Hint

Think about how authentication works in Django REST Framework.

❓ state_output
expert
2:00remaining
What is the output of this Django REST Framework throttle configuration?

Given the following settings and code, what will be the response status code of the 4th request made by the same authenticated user within one minute?

Django
REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_CLASSES': [
        'rest_framework.throttling.UserRateThrottle',
    ],
    'DEFAULT_THROTTLE_RATES': {
        'user': '3/minute'
    }
}

from rest_framework.views import APIView
from rest_framework.response import Response

class TestView(APIView):
    def get(self, request):
        return Response({'detail': 'Success'})
A403 Forbidden
B200 OK
C500 Internal Server Error
D429 Too Many Requests
Attempts:
2 left
πŸ’‘ Hint

Check the throttle rate and how many requests are allowed per minute.