<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Yasser Shkeir</title>
    <description>The latest articles on DEV Community by Yasser Shkeir (@yasser_shkeir).</description>
    <link>https://dev.to/yasser_shkeir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3351086%2F7cf50a09-5856-467a-af20-4975c702296f.png</url>
      <title>DEV Community: Yasser Shkeir</title>
      <link>https://dev.to/yasser_shkeir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yasser_shkeir"/>
    <language>en</language>
    <item>
      <title>Protect your Django API with Smart Ratelimiting (Async + Redis) 🛡️</title>
      <dc:creator>Yasser Shkeir</dc:creator>
      <pubDate>Wed, 14 Jan 2026 12:10:56 +0000</pubDate>
      <link>https://dev.to/yasser_shkeir/protect-your-django-api-with-smart-ratelimiting-async-redis-19l7</link>
      <guid>https://dev.to/yasser_shkeir/protect-your-django-api-with-smart-ratelimiting-async-redis-19l7</guid>
      <description>&lt;p&gt;Rate limiting is one of those things you don't think about until you really need it. Maybe a script kiddie is hammering your login endpoint, or a legitimate partner's cron job went rogue and is eating up all your database connections.&lt;/p&gt;

&lt;p&gt;If you're using Django, you might have used &lt;code&gt;django-ratelimit&lt;/code&gt; in the past. It's a classic, but frankly, it hasn't kept up with modern Django needs—especially when it comes to Async/Await support and High Availability.&lt;/p&gt;

&lt;p&gt;I just released &lt;code&gt;django-smart-ratelimit&lt;/code&gt; v1.0.1 to solve exactly these problems. Here is how you can use it to build a production-ready defense system for your API.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem: When Redis Dies, Your App Shouldn't 💀
&lt;/h2&gt;

&lt;p&gt;Most rate limiters are simple wrappers around a cache (like Redis). If Redis slows down or times out, your rate limiter throws an exception, and your users get a 500 error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We can do better.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This library implements a Circuit Breaker pattern. If the backend (Redis/MongoDB) starts failing, the rate limiter detects it and "fails open" (or closed, your choice). This means your site stays up even if your rate limiting infrastructure is having a bad day.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;pip install django-smart-ratelimit[redis]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Basic Decorator
&lt;/h3&gt;

&lt;p&gt;It looks familiar if you've used Django before, but under the hood, it's fully typed and async-compatible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.http import JsonResponse
from django_smart_ratelimit import ratelimit

@ratelimit(key='ip', rate='10/m', block=True)
def my_view(request):
    return JsonResponse({"message": "Hello World!"})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the user exceeds 10 requests per minute, they receive a &lt;code&gt;429 Too Many Requests&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Going Async ⚡
&lt;/h3&gt;

&lt;p&gt;This is where the library shines. It uses coredis for true non-blocking Redis operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ratelimit(key='ip', rate='50/s', block=True)
async def my_async_endpoint(request):
    # This won't block your event loop while checking limits!
    data = await fancy_async_logic()
    return JsonResponse(data)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Advanced: Dynamic Limits by User Type
&lt;/h3&gt;

&lt;p&gt;You usually want to give authenticated users higher limits than anonymous ones. You can stack decorators nicely for this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@ratelimit(key='ip', rate='10/m', group='anon')
@ratelimit(key='user', rate='100/m', group='auth')
def api_view(request):
    # ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or use a custom callable for the rate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def get_rate(group, request):
    if request.user.is_staff:
        return '1000/h'
    return '100/h'

@ratelimit(key='user', rate=get_rate, block=True)
def expensive_view(request):
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Choice of Algorithms 🧠
&lt;/h3&gt;

&lt;p&gt;Not all endpoints are the same. We support three algorithms:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Token Bucket:&lt;/strong&gt; Great for allowing "Bursts" of traffic (e.g., 100 requests at once, then refill slowly).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sliding Window:&lt;/strong&gt; Precise limiting (prevents the "double-limit at the top of the hour" loophole).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fixed Window:&lt;/strong&gt; Simple and fast.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can configure this globally or per-view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Migration
&lt;/h2&gt;

&lt;p&gt;If you are currently using &lt;code&gt;django-ratelimit&lt;/code&gt;, migration is effortless. In v1.0.1, we added an alias so you don't even need to rename your decorators immediately.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Old
from ratelimit.decorators import ratelimit

# New
from django_smart_ratelimit import ratelimit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Links &amp;amp; Source
&lt;/h2&gt;

&lt;p&gt;I'd love for you to try it out and break it!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/YasserShkeir/django-smart-ratelimit" rel="noopener noreferrer"&gt;https://github.com/YasserShkeir/django-smart-ratelimit&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;PyPI: &lt;a href="https://pypi.org/project/django-smart-ratelimit/" rel="noopener noreferrer"&gt;https://pypi.org/project/django-smart-ratelimit/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy coding! 🚀&lt;/p&gt;

</description>
      <category>api</category>
      <category>django</category>
      <category>python</category>
      <category>security</category>
    </item>
  </channel>
</rss>
