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

กลยุทธ์ตัวนับหน้าต่างเลื่อน

เรียนรู้เกี่ยวกับตัวนับหน้าต่างเลื่อน ซึ่งเป็นวิธีที่ใช้หน่วยความจำมีประสิทธิภาพกว่าและประมาณผลจากวิธีบันทึกเพื่อการใช้งานจริง

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

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

Intro to Sliding Window Counter

Welcome to our lesson on the Sliding Window Counter (SWC) algorithm!

This algorithm is a clever way to implement rate limiting. It aims to offer better accuracy than the simple Fixed Window Counter, while being more memory-efficient than the precise Sliding Window Log.

Fixed Window's Flaw

Recall that the Fixed Window Counter can suffer from a 'burst' problem. If a user makes requests right at the end of one window and then again right at the start of the next, they can effectively double their allowed requests in a short period.

The Sliding Window Counter helps to mitigate this issue.

The Core Idea: Blending Windows

Instead of just looking at the current fixed window, SWC looks at two fixed windows:

  • The current window.
  • The previous window.

It then combines their counts using a weighted average to estimate the true request rate over a 'sliding' period.

How the 'Slide' Happens

The 'sliding' effect comes from how we weight the previous window's count. We calculate an overlap percentage based on how far we are into the current window.

For example, if our window size is 60 seconds and we are 30 seconds into the current window, 50% of the previous window is still 'relevant' to our current sliding view.

Components for Calculation

To apply the Sliding Window Counter, you need to track a few pieces of information:

  • The total request count for the previous fixed window.
  • The total request count for the current fixed window.
  • The current timestamp (to determine how far into the current window we are).
  • The defined window size (e.g., 60 seconds, 1 minute).

SWC in Action: Scenario

Let's use an example:

  • Rate Limit: 10 requests per minute.
  • Current Time: 30 seconds into the current minute.
  • Previous Minute's Count: 8 requests.
  • Current Minute's Count: 3 requests so far.

How many requests have we 'used' in our sliding window?

Step-by-Step Calculation

Here's how we calculate the estimated count:

  1. Overlap Percentage: We are 30 seconds into a 60-second window, so 30/60 = 0.5 (or 50%).
  2. Weighted Previous Count: The previous window's count (8) is weighted by (1 - overlap percentage). So, 8 * (1 - 0.5) = 8 * 0.5 = 4.
  3. Estimated Total: Add the weighted previous count to the current count: 4 (weighted prev) + 3 (current) = 7.

So, 7 requests are estimated, leaving 3 requests remaining.

Implementing SWC Logic

This simple Java code snippet demonstrates how to calculate the estimated request count based on the current state of two windows.

Try running it to see the calculation in action!

public class RateLimitCalculator {

    public static double calculateEstimatedRequests(
            int previousWindowCount,
            int currentWindowCount,
            long timeElapsedInCurrentWindowMillis,
            long windowSizeMillis) {

        double overlapPercentage = (double) timeElapsedInCurrentWindowMillis / windowSizeMillis;

        // The core Sliding Window Counter calculation
        // It weights the previous window's count based on the *overlap*
        // and adds it to the current window's count.
        double estimatedCount = previousWindowCount * (1 - overlapPercentage) + currentWindowCount;

        return estimatedCount;
    }

    public static void main(String[] args) {
        int maxRequestsPerMinute = 10;
        long windowSizeMillis = 60 * 1000; // 1 minute

        // Scenario: 30 seconds into the current minute
        long timeElapsed = 30 * 1000;

        // Previous minute had 8 requests
        int prevCount = 8;
        // Current minute has 3 requests so far
        int currentCount = 3;

        double estimated = calculateEstimatedRequests(
            prevCount,
            currentCount,
            timeElapsed,
            windowSizeMillis
        );

        System.out.println("Prev count: " + prevCount);
        System.out.println("Current count: " + currentCount);
        System.out.println("Elapsed in window: " + (timeElapsed / 1000) + "s");
        System.out.println("Window size: " + (windowSizeMillis / 1000) + "s");
        System.out.println("\nEstimated requests: " + String.format("%.2f", estimated));

        if (estimated < maxRequestsPerMinute) {
            System.out.println("Request would likely be allowed.");
        } else {
            System.out.println("Request would likely be denied.");
        }
    }
}

SWC's Key Benefits

The Sliding Window Counter offers several advantages:

  • Improved Accuracy: It provides a better approximation of the true rate than Fixed Window, especially around window boundaries.
  • Memory Efficiency: Unlike Sliding Window Log, it doesn't need to store every request timestamp, making it less demanding on memory.
  • Better Burst Handling: It reduces the chance of allowing excessive bursts compared to the Fixed Window algorithm.

SWC: Approximation, Not Perfect

While powerful, the Sliding Window Counter is still an approximation. It's not perfectly accurate like the Sliding Window Log.

It can still allow slight overages at window boundaries, though significantly less than a purely Fixed Window approach. For high-precision requirements, the Sliding Window Log might still be preferred, if memory allows.

Quick Check on SWC

Let's test your understanding of the Sliding Window Counter calculation!

Recap & What's Next

Great job! You've learned about the Sliding Window Counter algorithm.

  • It combines counts from two fixed windows to approximate a sliding window.
  • It's more accurate than Fixed Window and more memory-efficient than Sliding Window Log.
  • It calculates an estimated count using an overlap percentage.

Next, we'll compare all the algorithms you've learned to understand their trade-offs.

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

บทเรียน “กลยุทธ์ตัวนับหน้าต่างเลื่อน” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กลยุทธ์ตัวนับหน้าต่างเลื่อน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “กลยุทธ์ตัวนับหน้าต่างเลื่อน” ใช้เวลานานแค่ไหน

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

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

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

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

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