0Pricing
API Rate Limiting & Scalability Patterns · 课时

选择合适的限流算法

比较核心限流算法——固定窗口、滑动窗口、令牌桶和漏桶——并学习每种算法适合何种流量特征和公平性目标。

选择合适的限流算法 是 CoddyKit 上的免费 API Rate Limiting & Scalability Patterns 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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.

常见问题解答

「选择合适的限流算法」课时是免费的吗?

是的 — 「选择合适的限流算法」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Rate Limiting & Scalability Patterns 课程的其余内容,请升级到 CoddyKit PRO。 API Rate Limiting & Scalability Patterns 课程共包含 4 节课。

「选择合适的限流算法」这节课中我会学到什么?

比较核心限流算法——固定窗口、滑动窗口、令牌桶和漏桶——并学习每种算法适合何种流量特征和公平性目标。 你通过在浏览器中直接运行的动手代码来练习 API Rate Limiting & Scalability Patterns,全天候 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