0Pricing
API Rate Limiting & Scalability Patterns · บทเรียน

การออกแบบตัวจำกัดอัตราในหน่วยความจำ

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

การออกแบบตัวจำกัดอัตราในหน่วยความจำ เป็นบทเรียน API Rate Limiting & Scalability Patterns ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน API Rate Limiting & Scalability Patterns และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส API Rate Limiting & Scalability Patterns มีบทเรียนทั้งหมด 4 บทเรียน

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

Intro to In-Memory Limiting

Welcome to designing an in-memory rate limiter! This is the simplest type of rate limiter, perfect for understanding the core concepts.

An in-memory rate limiter stores all its tracking data (like how many requests a user has made) directly in the application's RAM, not in a separate database or service.

This makes it fast and easy to set up, but it comes with specific limitations we'll explore.

Why Use In-Memory?

In-memory rate limiters are ideal for:

  • Single-instance applications: Where your application runs on just one server.
  • Quick prototypes: To test rate limiting concepts without complex infrastructure.
  • Non-critical APIs: Where occasional dropped requests due to server restarts are acceptable.

They are simple to implement because they don't need to communicate with external data stores.

Core Design Concepts

Every rate limiter needs to track a few key pieces of information:

  • Client ID: Who is making the request? (e.g., IP address, user ID, API key)
  • Request Limit: How many requests are allowed? (e.g., 100 requests)
  • Time Window: Over what period? (e.g., per minute, per hour)

Our in-memory design will use these concepts to decide if a request is allowed or denied.

Choosing a Strategy: Fixed Window

For our basic in-memory limiter, we'll use the Fixed Window Counter algorithm. It's straightforward:

  • Requests are counted within a specific, fixed time window (e.g., 0-59 seconds, 60-119 seconds).
  • When a new window starts, the counter resets to zero.
  • If the request count exceeds the limit within the current window, new requests are denied.

While simple, it's a great starting point for understanding rate limiting mechanics.

Data Structures for Tracking

To keep track of requests for different clients within their time windows, we'll use Java's ConcurrentHashMap:

  • counts: A map to store the number of requests for each clientId (e.g., "user1" -> 5).
  • windowStarts: A map to store the start time of the current window for each clientId (e.g., "user1" -> 1678886400000L).

ConcurrentHashMap is thread-safe, which is important when multiple requests might hit our limiter at the same time.

Building the Limiter Class

Let's start by defining our InMemoryRateLimiter class. It will hold our configuration (limit and window duration) and the maps for tracking.

Here's the basic structure:

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryRateLimiter {
    private final int limit; // Max requests allowed
    private final long windowMillis; // Time window in milliseconds

    private final ConcurrentHashMap<String, AtomicInteger> counts = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Long> windowStarts = new ConcurrentHashMap<>();

    public InMemoryRateLimiter(int limit, long windowMillis) {
        this.limit = limit;
        this.windowMillis = windowMillis;
    }

    // The allowRequest method will go here
}

Implementing `allowRequest` - Part 1

The heart of our limiter is the allowRequest(String clientId) method. This method will determine if a request from a given client should be allowed.

First, we get the current time and initialize the window start time for the client if it's their first request:

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryRateLimiter {
    private final int limit;
    private final long windowMillis;

    private final ConcurrentHashMap<String, AtomicInteger> counts = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Long> windowStarts = new ConcurrentHashMap<>();

    public InMemoryRateLimiter(int limit, long windowMillis) {
        this.limit = limit;
        this.windowMillis = windowMillis;
    }

    public boolean allowRequest(String clientId) {
        long currentTime = System.currentTimeMillis();

        // Get or initialize window start time for this client
        long currentWindowStart = windowStarts.computeIfAbsent(clientId, k -> currentTime);

        // ... more logic to come ...
        return false; // Placeholder
    }
}

Implementing `allowRequest` - Part 2

Next, we add the logic to check if the current time window has expired. If it has, we reset the window start time and the request count for that client.

This ensures that when a new window begins, clients get a fresh quota of requests.

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryRateLimiter {
    private final int limit;
    private final long windowMillis;

    private final ConcurrentHashMap<String, AtomicInteger> counts = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Long> windowStarts = new ConcurrentHashMap<>();

    public InMemoryRateLimiter(int limit, long windowMillis) {
        this.limit = limit;
        this.windowMillis = windowMillis;
    }

    public boolean allowRequest(String clientId) {
        long currentTime = System.currentTimeMillis();
        long currentWindowStart = windowStarts.computeIfAbsent(clientId, k -> currentTime);

        // If the current window has expired, reset it
        if (currentTime - currentWindowStart >= windowMillis) {
            windowStarts.put(clientId, currentTime); // Start a new window
            counts.put(clientId, new AtomicInteger(0)); // Reset count
        }

        // ... more logic to come ...
        return false; // Placeholder
    }
}

Implementing `allowRequest` - Part 3

Finally, we increment the request count for the client and check if it's still within the allowed limit. If it is, the request is allowed; otherwise, it's denied.

The AtomicInteger ensures thread-safe increments.

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryRateLimiter {
    private final int limit;
    private final long windowMillis;

    private final ConcurrentHashMap<String, AtomicInteger> counts = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Long> windowStarts = new ConcurrentHashMap<>();

    public InMemoryRateLimiter(int limit, long windowMillis) {
        this.limit = limit;
        this.windowMillis = windowMillis;
    }

    public boolean allowRequest(String clientId) {
        long currentTime = System.currentTimeMillis();
        long currentWindowStart = windowStarts.computeIfAbsent(clientId, k -> currentTime);

        if (currentTime - currentWindowStart >= windowMillis) {
            windowStarts.put(clientId, currentTime);
            counts.put(clientId, new AtomicInteger(0));
        }

        // Increment count and check if within limit
        AtomicInteger clientCount = counts.computeIfAbsent(clientId, k -> new AtomicInteger(0));
        if (clientCount.incrementAndGet() <= limit) {
            return true; // Request allowed
        } else {
            return false; // Request denied
        }
    }

    public static void main(String[] args) {
        // Example usage will go here
    }
}

Full Example and Testing

Let's put it all together and test our in-memory rate limiter! This example creates a limiter allowing 3 requests per 5 seconds for a specific user.

Run the code and observe how requests are allowed initially, then denied, and finally allowed again after the time window resets.

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class InMemoryRateLimiter {
    private final int limit;
    private final long windowMillis;

    private final ConcurrentHashMap<String, AtomicInteger> counts = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Long> windowStarts = new ConcurrentHashMap<>();

    public InMemoryRateLimiter(int limit, long windowMillis) {
        this.limit = limit;
        this.windowMillis = windowMillis;
    }

    public boolean allowRequest(String clientId) {
        long currentTime = System.currentTimeMillis();
        long currentWindowStart = windowStarts.computeIfAbsent(clientId, k -> currentTime);

        if (currentTime - currentWindowStart >= windowMillis) {
            windowStarts.put(clientId, currentTime);
            counts.put(clientId, new AtomicInteger(0));
        }

        AtomicInteger clientCount = counts.computeIfAbsent(clientId, k -> new AtomicInteger(0));
        if (clientCount.incrementAndGet() <= limit) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        // Allow 3 requests per 5 seconds for "user1"
        InMemoryRateLimiter limiter = new InMemoryRateLimiter(3, 5000);

        String user = "user1";
        System.out.println("Testing rate limiter for " + user + ": 3 requests / 5 seconds\n");

        for (int i = 0; i < 5; i++) {
            boolean allowed = limiter.allowRequest(user);
            System.out.println("Request " + (i + 1) + ": " + (allowed ? "Allowed" : "Denied"));
            if (i == 2) { // After 3rd request, wait for window to reset
                System.out.println("\n--- Max requests reached. Waiting for window reset (5.5s) ---\n");
                Thread.sleep(5500); // Wait for window to reset
            }
        }

        System.out.println("\n--- Testing after window reset ---\n");
        for (int i = 0; i < 2; i++) {
            boolean allowed = limiter.allowRequest(user);
            System.out.println("Request " + (i + 1) + ": " + (allowed ? "Allowed" : "Denied"));
        }
    }
}

Understanding In-Memory Limitations

While simple and fast, in-memory rate limiters have a critical limitation. Imagine you deploy your application on multiple servers to handle more traffic.

What happens if requests from the same user go to different servers?

Recap: In-Memory Rate Limiting

You've successfully designed and understood a basic in-memory rate limiter!

  • We defined an in-memory rate limiter and its use cases for single-instance apps.
  • We explored key concepts: client ID, limit, and time window.
  • We implemented a Fixed Window Counter using ConcurrentHashMap in Java.
  • You now understand its primary limitation: it's not suitable for distributed systems due to its lack of shared state.

This foundational knowledge is crucial before diving into more advanced, distributed rate limiting solutions!

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

บทเรียน “การออกแบบตัวจำกัดอัตราในหน่วยความจำ” ฟรีหรือไม่

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

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

ออกแบบและสร้างตัวจำกัดอัตราพื้นฐานในหน่วยความจำ ซึ่งเหมาะกับแอปพลิเคชันที่มีอินสแตนซ์เดียว โดยใช้รูปแบบการเขียนโปรแกรมทั่วไป คุณปฏิบัติ API Rate Limiting & Scalability Patterns ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน API Rate Limiting & Scalability Patterns หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน API Rate Limiting & Scalability Patterns บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

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

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

ฉันเขียนและรันโค้ดในบทเรียน API Rate Limiting & Scalability Patterns นี้ได้ไหม

ได้ บทเรียน API Rate Limiting & Scalability Patterns ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การออกแบบตัวจำกัดอัตราในหน่วยความจำ
  2. การจำกัดอัตราแบบกระจายด้วย Redis
  3. การจัดการเมื่อเกินขีดจำกัดอัตรา
  4. การทดสอบและการตรวจสอบตัวจำกัดอัตราการส่งคำขอ
← กลับไปที่ API Rate Limiting & Scalability Patterns