0Pricing
API Rate Limiting & Scalability Patterns · レッスン

適切なレート制限アルゴリズムの選択

固定ウィンドウ、スライディングウィンドウ、トークンバケット、リーキーバケットという主要なレート制限アルゴリズムを比較し、それぞれがトラフィック特性や公平性の目標に適する場面を学びます。

「適切なレート制限アルゴリズムの選択」はCoddyKit上の無料API Rate Limiting & Scalability Patternsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAPI Rate Limiting & Scalability Patterns学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 API Rate Limiting & Scalability Patternsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why the Algorithm Matters

A rate limit policy is only as good as the algorithm that enforces it. The same limit of 100 requests/minute behaves very differently depending on how you count.

In this lesson we compare four classic approaches and learn how to pick one based on your fairness, burst, and accuracy needs.

Fixed Window Counter

The simplest approach: count requests in a fixed time window (for example each calendar minute) and reset the counter at the boundary.

  • Pros: trivial to implement, low memory
  • Cons: allows a burst of 2x the limit around the window edge
def allow(counter, limit):
    if counter['count'] >= limit:
        return False
    counter['count'] += 1
    return True

The Boundary Burst Problem

With a fixed window, a client can send limit requests at 00:59 and another limit at 01:00. That is double the intended rate in a one-second span.

Sliding window algorithms exist to smooth out exactly this spike.

Sliding Window Log

Store a timestamp for every request. To decide, drop timestamps older than the window and count what remains.

  • Pros: exact, no boundary burst
  • Cons: memory grows with request volume
def allow(log, now, window, limit):
    cutoff = now - window
    log[:] = [t for t in log if t > cutoff]
    if len(log) >= limit:
        return False
    log.append(now)
    return True

Sliding Window Counter

A hybrid: keep the current and previous fixed-window counts, then estimate the rate using a weighted overlap.

It approximates the sliding log with far less memory, which is why API gateways and CDNs favor it.

weighted = prev_count * overlap + curr_count
allowed = weighted < limit

Token Bucket

A bucket holds tokens up to a capacity. Tokens refill at a steady rate; each request spends one token. Empty bucket means reject.

  • Allows controlled bursts up to the bucket capacity
  • Smooths the long-term average to the refill rate
def allow(bucket, now, rate, capacity):
    elapsed = now - bucket['ts']
    bucket['tokens'] = min(capacity, bucket['tokens'] + elapsed * rate)
    bucket['ts'] = now
    if bucket['tokens'] < 1:
        return False
    bucket['tokens'] -= 1
    return True

Leaky Bucket

Requests enter a queue that leaks at a constant rate. If the queue overflows, requests are dropped.

Unlike the token bucket, the leaky bucket enforces a steady output rate — ideal when a downstream service cannot handle spikes.

Token vs. Leaky Bucket

  • Token bucket lets traffic burst up to capacity, then throttles — good for user-facing APIs that should feel responsive.
  • Leaky bucket forces a smooth, constant flow — good for protecting fragile backends.

Memory and Accuracy Trade-offs

Pick based on constraints:

  • Lowest memory: fixed window
  • Highest accuracy: sliding window log
  • Best balance: sliding window counter
  • Burst-friendly: token bucket

Distributed Considerations

Across many servers, each node cannot keep its own counter or you multiply the real limit. Use a shared store like Redis with atomic operations so the count is global.

Token bucket and sliding window counter both map cleanly to Redis primitives.

-- Redis atomic counter with expiry
INCR rate:user:42
EXPIRE rate:user:42 60

A Decision Checklist

Ask:

  • Do I need to allow short bursts? → token bucket
  • Must downstream see a steady rate? → leaky bucket
  • Is exactness critical for billing? → sliding window log
  • Do I want simple and cheap? → fixed or sliding window counter

Quick Check

Test your understanding of algorithm selection.

Recap

You compared four rate limiting algorithms:

  • Fixed window — cheap but allows edge bursts
  • Sliding window — accurate, smooths boundaries
  • Token bucket — burst-friendly, averages out
  • Leaky bucket — constant output rate

Choose based on burst tolerance, accuracy, and memory budget.

よくある質問

「適切なレート制限アルゴリズムの選択」レッスンは無料ですか?

はい。「適切なレート制限アルゴリズムの選択」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、API Rate Limiting & Scalability Patternsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 API Rate Limiting & Scalability Patternsコースには全4レッスンが含まれています。

「適切なレート制限アルゴリズムの選択」で何を学びますか?

固定ウィンドウ、スライディングウィンドウ、トークンバケット、リーキーバケットという主要なレート制限アルゴリズムを比較し、それぞれがトラフィック特性や公平性の目標に適する場面を学びます。 ブラウザで直接実行するハンズオンコードでAPI Rate Limiting & Scalability Patternsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

API Rate Limiting & Scalability Patternsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAPI Rate Limiting & Scalability Patternsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「適切なレート制限アルゴリズムの選択」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAPI Rate Limiting & Scalability Patternsレッスンでコードを書いて実行できますか?

はい。すべてのAPI Rate Limiting & Scalability Patternsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. スロットリングとレート制限の違い
  2. バーストとグレースピリオドのポリシー
  3. クライアント側とサーバー側の制限
  4. 適切なレート制限アルゴリズムの選択
← API Rate Limiting & Scalability Patternsに戻る