تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة
احمِ المصادقة وواجهات API في تطبيق SaaS من إساءة الاستخدام عبر تحديد معدل الطلبات، وقفل الحسابات، والتراجع الأسي باستخدام مخزن سريع مثل Redis.
تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة درس مجاني في AI Powered SaaS: Stripe + Auth + Billing + Deploy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Powered SaaS: Stripe + Auth + Billing + Deploy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Powered SaaS: Stripe + Auth + Billing + Deploy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Rate Limit?
Without limits, attackers can hammer your login endpoint to guess passwords, scrape data, or run up costs on metered APIs. Rate limiting caps how many requests a client can make in a window.
Identifying the Client
Limits are keyed on something that identifies the caller: an IP address, a user ID, or an API key. Choose the key based on what you are protecting.
const key = 'login:' + (userId ?? clientIp);The Fixed Window Algorithm
The simplest method counts requests per fixed time window. If the count exceeds the limit, reject until the window resets.
// allow 5 requests per 60 seconds
if (count > 5) return reject();Counting in Redis
Redis is ideal: INCR bumps a counter atomically, and a TTL auto-expires the window. The first request sets the expiry.
const n = await redis.incr(key);
if (n === 1) await redis.expire(key, 60);
if (n > 5) throw new Error('Too many requests');Sliding Window & Token Bucket
Fixed windows allow bursts at the edges. Sliding window smooths this, and token bucket permits short bursts while enforcing an average rate. Libraries like Upstash Ratelimit implement these for you.
import { Ratelimit } from '@upstash/ratelimit';
const rl = new Ratelimit({ redis, limiter: Ratelimit.slidingWindow(5, '60 s') });Applying in Middleware
Centralize limiting in Next.js middleware so it runs before every matched request.
export async function middleware(req) {
const { success } = await rl.limit(req.ip ?? 'anon');
if (!success) return new Response('Rate limited', { status: 429 });
}Returning 429 Properly
When limited, respond with status 429 and a Retry-After header telling clients when to try again.
return new Response('Too many requests', {
status: 429,
headers: { 'Retry-After': '60' }
});Account Lockout
For login specifically, track failed attempts per account. After several failures, temporarily lock the account to stop targeted brute force.
const fails = await redis.incr('fail:' + email);
if (fails >= 5) await redis.expire('lock:' + email, 900);Exponential Backoff
Increase the delay after each failure: 1s, 2s, 4s, 8s. This frustrates automated guessing while barely affecting legitimate users.
const delay = Math.min(2 ** fails, 60) * 1000;Avoiding False Positives
Be careful not to punish real users:
- Shared office IPs share a limit — prefer per-user keys when authenticated
- Reset counters on success
- Set generous limits for normal usage
Best Practices
Protect endpoints well:
- Key limits on IP, user, or API key
- Use Redis with sliding window or token bucket
- Return 429 with Retry-After
- Add lockout and backoff for login
Quick Check
Test your rate-limiting knowledge.
Recap
You learned to defend against abuse:
- Key rate limits on IP, user, or API key
- Count with Redis
INCRand TTL, or use sliding window libraries - Return
429withRetry-After - Add account lockout and exponential backoff for logins
Your auth and APIs now resist brute force and flooding.
تعلم AI Powered SaaS: Stripe + Auth + Billing + Deploy مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 48
الأسئلة الشائعة
هل درس «تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة» مجاني؟
نعم — نص درس «تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Powered SaaS: Stripe + Auth + Billing + Deploy، انتقل إلى CoddyKit PRO. تتضمن دورة AI Powered SaaS: Stripe + Auth + Billing + Deploy 4 دروس في المجموع.
ماذا ستتعلم في «تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة»؟
احمِ المصادقة وواجهات API في تطبيق SaaS من إساءة الاستخدام عبر تحديد معدل الطلبات، وقفل الحسابات، والتراجع الأسي باستخدام مخزن سريع مثل Redis. تتمرن على AI Powered SaaS: Stripe + Auth + Billing + Deploy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Powered SaaS: Stripe + Auth + Billing + Deploy؟
لا تُشترط خبرة سابقة. AI Powered SaaS: Stripe + Auth + Billing + Deploy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Powered SaaS: Stripe + Auth + Billing + Deploy هذا؟
نعم. كل درس في AI Powered SaaS: Stripe + Auth + Billing + Deploy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- تكامل OAuth 2.0
- المصادقة متعددة العوامل (MFA)
- التحكم في الوصول المستند إلى الأدوار (RBAC)
- تحديد معدل الطلبات والحماية من هجمات القوة الغاشمة