0Pricing
Secure Coding & OWASP Top 10 for Backend · บทเรียน

การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ

เรียนรู้ว่าการจำกัดอัตราการส่งคำขอช่วยปกป้องเอพีไอจากการใช้งานในทางที่ผิด การเดารหัสแบบรุนแรง และการปฏิเสธการให้บริการอย่างไร พร้อมวิธีนำกลยุทธ์บักเก็ตโทเค็นและหน้าต่างเลื่อนไปใช้

การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ เป็นบทเรียน Secure Coding & OWASP Top 10 for Backend ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Secure Coding & OWASP Top 10 for Backend และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Rate Limiting?

Rate limiting caps how many requests a client can make in a time window. It protects APIs from brute-force attacks, scraping, accidental loops, and denial-of-service.

It is a key control listed under API security best practices.

Throttling vs Limiting

Rate limiting rejects requests over a hard cap; throttling slows them down (queuing or delaying) instead of rejecting outright. Both manage load and abuse, often used together.

What to Limit On

Choose a key to count requests against:

  • API key or user ID for authenticated traffic
  • IP address for anonymous traffic
  • Endpoint sensitivity (stricter limits on login)

Combining keys gives finer control and resists simple bypasses.

Fixed Window

The simplest approach counts requests in a fixed time window, resetting the counter each period. It is easy but allows bursts at window edges (twice the limit across a boundary).

import time

window = {}
LIMIT = 5
PERIOD = 60

def allow(key):
    now = int(time.time() // PERIOD)
    count = window.get((key, now), 0)
    if count >= LIMIT:
        return False
    window[(key, now)] = count + 1
    return True

Token Bucket

The token bucket refills tokens at a steady rate up to a capacity. Each request consumes a token; an empty bucket means the request is rejected. It allows controlled bursts while enforcing an average rate.

import time

class TokenBucket:
    def __init__(self, rate, capacity):
        self.rate = rate
        self.capacity = capacity
        self.tokens = capacity
        self.last = time.time()
    def allow(self):
        now = time.time()
        self.tokens = min(self.capacity, self.tokens + (now - self.last) * self.rate)
        self.last = now
        if self.tokens >= 1:
            self.tokens -= 1
            return True
        return False

Sliding Window

The sliding window tracks timestamps of recent requests and counts only those within the last N seconds. It avoids the burst problem of fixed windows at the cost of more bookkeeping.

Distributed Rate Limiting

With multiple servers, counters must be shared. A central store like Redis holds the counters so limits apply across the whole cluster, not per instance. Use atomic operations to avoid race conditions.

Communicating Limits

Tell clients about their limits with response headers so well-behaved clients can back off.

headers = {
    'X-RateLimit-Limit': '100',
    'X-RateLimit-Remaining': '42',
    'X-RateLimit-Reset': '1717000000',
    'Retry-After': '30',
}
for k, v in headers.items():
    print(k + ': ' + v)

Status Codes

Return 429 Too Many Requests when a client exceeds the limit, ideally with a Retry-After header. This is the standard signal clients and SDKs expect.

Protecting Sensitive Endpoints

Apply stricter limits to high-risk endpoints like login, password reset, and OTP verification. Tight limits here directly blunt brute-force and credential-stuffing attacks.

  • Login: a few attempts per minute
  • Password reset: a few per hour
  • General reads: generous limits

Avoiding Pitfalls

Watch for bypasses: rotating IPs, missing limits on some routes, and limits that reset on server restart. Place rate limiting at the gateway or middleware layer so every route is covered consistently.

Quick Check

Test your understanding of rate limiting.

Recap

You learned why APIs need rate limiting, how to choose a limiting key, and the trade-offs of fixed-window, token-bucket, and sliding-window strategies. You also saw distributed limiting with Redis, the 429 response, and stricter limits for sensitive endpoints.

คำถามที่พบบ่อย

บทเรียน “การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Secure Coding & OWASP Top 10 for Backend ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Secure Coding & OWASP Top 10 for Backend มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ”

เรียนรู้ว่าการจำกัดอัตราการส่งคำขอช่วยปกป้องเอพีไอจากการใช้งานในทางที่ผิด การเดารหัสแบบรุนแรง และการปฏิเสธการให้บริการอย่างไร พร้อมวิธีนำกลยุทธ์บักเก็ตโทเค็นและหน้าต่างเลื่อนไปใช้ คุณปฏิบัติ Secure Coding & OWASP Top 10 for Backend ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Secure Coding & OWASP Top 10 for Backend หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Secure Coding & OWASP Top 10 for Backend บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Secure Coding & OWASP Top 10 for Backend นี้ได้ไหม

ได้ บทเรียน Secure Coding & OWASP Top 10 for Backend ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การออกแบบ RESTful API ที่ปลอดภัย
  2. ความปลอดภัยของ GraphQL API
  3. การป้องกันการโจมตี SSRF
  4. การจำกัดและควบคุมอัตราการส่งคำขอของเอพีไอ
← กลับไปที่ Secure Coding & OWASP Top 10 for Backend