Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3210

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T12:38:08+00:00 2024-11-26T12:38:08+00:00

How to throttle your API with Django Rest Framework

  • 61k

Control the rate of requests that clients can make to your API.

But what is throttling…?

As DRF says, throttling is

Throttling is similar to permissions, in that it determines if a request should be authorized. Throttles indicate a temporary state, and are used to control the rate of requests that clients can make to an API

In other words, we can say that throttling is a mechanism for limiting how many requests my API can accept in a period of time, we can specify this limit per user, IP address, etc. This is similar to how some APIs limit the number of requests you can make in a day or an hour.

How throttling is handle in DRF

As with permissions and authentication, throttling in DRF is always defined as a list of classes.

Before running the main body of the view each throttle in the list is checked. If any throttle check fails an exceptions.Throttled exception will be raised, and the main body of the view will not run.

And the same as permissions we can set these throttles globally and per views.

Global Throttles

We can set a global default throttling policy using the DEFAULT_THROTTLE_CLASSES and DEFAULT_THROTTLE_RATES settings.

REST_FRAMEWORK = {     'DEFAULT_THROTTLE_CLASSES': [         'rest_framework.throttling.AnonRateThrottle',         'rest_framework.throttling.UserRateThrottle'     ],     'DEFAULT_THROTTLE_RATES': {         'anon': '20/day',         'user': '50/day'     } } 
Enter fullscreen mode Exit fullscreen mode

The rate descriptions used in DEFAULT_THROTTLE_RATES may include second, minute, hour, or day as the throttle period.

With these settings,

  • Unauthenticated users will be able to only make 20 requests per day to our API, the IP address of the incoming request is used to generate a unique key to throttle against.

  • Authenticated users will be able to make 50 requests per day to our API, for these the id of the user is going to be used to generate the unique key.

When they reached the maximum of requests our API will respond with the error code 429 – Too Many Requests

Since this is the global configuration this will apply to all views, but we can still override these settings per view.

Throttles Per View

It's always better to have control over what views we want to limit, for this DRF offers us the possibility of setting these throttles classes per view.

We do this by passing a list of throttles classes to the throttle_classes attribute on the APIView class-based views.

from rest_framework.response import Response from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView  class ExampleView(APIView):     throttle_classes = [UserRateThrottle]      def get(self, request, format=None):         content = {             'status': 'request was permitted'         }         return Response(content) 
Enter fullscreen mode Exit fullscreen mode

In this way, ExampleView will use the UserRateThrottle class to limit the number of requests that this view can receive, the rates, in this case, are still defined on the DEFAULT_THROTTLE_CLASSES settings key.

But what if we want to specify a different rate for a specific view, we can do this by extending the UserRateThrottle class and specifying a new rate.

from rest_framework.response import Response from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView  class CustomUserRateThrottle(UserRateThrottle):     rate= '5/day'  class VeryLimitedView(APIView):     throttle_classes = [CustomUserRateThrottle]      def get(self, request, format=None):         content = {             'status': 'request was permitted'         }         return Response(content) 
Enter fullscreen mode Exit fullscreen mode

Now only for this view the authenticated users have 5 requests per day, even though the global settings say that the users have 50 requests per day.

ScopedRateThrottles

The ScopedRateThrottle class can be used to restrict access to specific parts of the API. This throttle will only be applied if the view that is being accessed includes a .throttle_scope attribute.

The allowed request rate is determined by the DEFAULT_THROTTLE_RATES setting using a key from the request “scope”.

For example, given the following views…

class ContactListView(APIView):     throttle_scope = 'contacts'     ...  class ContactDetailView(APIView):     throttle_scope = 'contacts'     ...  class UploadView(APIView):     throttle_scope = 'uploads'     ... 
Enter fullscreen mode Exit fullscreen mode

…and the following settings.

REST_FRAMEWORK = {     'DEFAULT_THROTTLE_CLASSES': [         'rest_framework.throttling.ScopedRateThrottle',     ],     'DEFAULT_THROTTLE_RATES': {         'contacts': '100/day',         'uploads': '50/day'     } } 
Enter fullscreen mode Exit fullscreen mode

User requests to either ContactListView or ContactDetailView would be restricted to a total of 100 requests per day. User requests to UploadView would be restricted to 50 requests per day.

So, what's next…

As we saw, throttles is a powerful and really helpful feature that we can implement on our API if we need to impose different constraints on different parts of the API, due to some services being particularly resource-intensive. Also, it's worth mentioning that DRF provides a BaseThrottle class which you can override to create custom throttles with custom implementations.

If you plan to use this method as a security feature just consider that the DRF throttling isn't intended as a security feature, it has some weaknesses and you shouldn't rely only upon the throttling.

Yet, it's a cool feature and you should definitely try it on your API.

You can follow me on Twitter and GitHub to be up to date with all my projects and content.

djangoprogrammingpythonwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.