跳到正文
产品桌面工具TASTECAD图纸查看TASTEFILES文件管理TASTEFTP文件传输TASTESSH远程工作空间TASTEZIP压缩与解压TASTEWINM文件管理开发工具TASTECODERust 集成开发环境TASTESTUDIO集成开发与测试环境TASTEETL数据整合(ETL)服务器TASTEREMOTE远程桌面服务器TASTEGEO空间信息服务器TASTEMAIL邮件工作空间投资TASTESTOCK股票自动交易桌面应用开始使用下载文档购买全部产品
市场
资讯
技术资讯今日开发资讯开发文档编程语言RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDartWeb 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular服务端框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express应用与桌面框架TauriFlutterReact NativeElectron算法算法基础核心算法进阶算法实用算法
社区项目开发日志提示词AI 工具问题作品展示社区指南
合作技术传授网站构建桌面应用构建发布与支付流水线销售合作伙伴联系我们
服务远程桌面安装包定制
TASTEDEV
产品

桌面工具

TASTECAD复杂图纸,轻松打开TASTEFILES只要需要的文件,最快送达TASTEFTP让文件往来,干净利落TASTESSH你与服务器之间,连接更牢靠TASTEZIP压得更快,解得更稳TASTEWINM以键盘为主、用两个窗格整理文件的文件管理器

开发工具

TASTECODE在一个窗口中完成 Rust 应用的编辑、构建、调试和测试TASTESTUDIO从编辑到远程测试,再到经批准的 AI 修复TASTEETL边看数值,边搬数据

服务器

TASTEREMOTE在自己的服务器上运行的远程桌面TASTEGEO用 Rust 重新构建兼容 GeoServer 的空间信息服务器TASTEMAIL把邮件的主导权拿回自己手中

投资

TASTESTOCK何时、做什么——按你定下的规则运行的股票自动交易

开始使用

下载所有产品的最新版本文档各产品使用方法购买订阅与永久许可全部产品一览全部产品
全部产品
市场资讯
技术

资讯

今日开发资讯语言、框架、GitHub 与 AI 官方发布开发文档语言、框架与算法指南

编程语言

RustTypeScriptJavaScriptPythonGoC#JavaCC++KotlinDart

框架

Web 前端框架Next.jsReactVue.js · NuxtSvelteKitAngular
服务端框架Spring BootDjangoFastAPIASP.NET CoreNestJS · Express
应用与桌面框架TauriFlutterReact NativeElectron

算法

算法基础复杂度分析 · 排序 · 查找 · 基础数据结构 · 树与堆核心算法图遍历 · 最短路径与最小生成树 · 动态规划 · 贪心算法 · 分治法 · 回溯法进阶算法字符串算法 · 数学与数论 · 位运算 · 线段树 · 并查集实用算法编程面试解题策略 · LRU 缓存 · 限流 · 一致性哈希
查看全部开发文档
社区
项目发现开发者与作品开发日志决策与试错提示词可复用的指令AI 工具问题Claude、Codex、Gemini 的问题与变通办法作品展示已完成的成果社区指南我们如何公开构建
合作
技术传授用 Rust 和 egui 打造原生应用网站构建10 种语言、账户、文档与下载桌面应用构建Rust 和 egui,不用 WebView发布与支付流水线签名、MSI、deb、rpm、AppImage、哈希销售合作伙伴推荐即得 30%联系我们开发、合作与投资咨询
服务
远程桌面用浏览器连接你的电脑安装包定制只装需要的,设置预先放好
购买
ZH
한국어EnglishDeutschEspañolFrançaisItalianoPortuguês (Brasil)日本語简体中文繁體中文
登录
登录
TASTEDEV

从小工具到更好的开发。

support@tastedev.net支持 TASTEDEV
产品产品线购买文档下载市场客户支持
社区项目开发日志提示词AI 工具问题作品展示
政策关于我们隐私政策服务条款订阅条款退款政策社区规范
账户登录注册
访客
今天
昨天
累计浏览
公司名称
GXSOFT
法定代表人
CHOI YOUNG GU
营业执照号
853-44-00368
地址
9F V24, 351 Gyeongin-ro, Michuhol-gu, Incheon, Republic of Korea
电话
+82-10-4404-1523
电子邮件
whitecalvin@tastedev.net
个人信息保护负责人
CHOI YOUNG GU

© 2026 TASTEDEV产品信息以已验证的开发源码为准。已发布·持续改进

限流 文档
限流
ConceptsHow It WorksImplementationComplexityPractice ProblemsReal-World Use
讨论

限流 指南 · 4/6

Complexity

本章目前仅提供英文版。

A rate limiter runs in front of every request, so the cost of one decision and the memory each key consumes translate directly into the cost of running your service. This chapter compares the five algorithms on time, space, accuracy and burst behavior. Below, L is the limit per window and K is the number of active keys.

Time per decision

  • Fixed window: compute the window number and bump one counter. O(1).
  • Sliding window log: drop expired entries from the front, then count. With a deque each timestamp is appended once and removed once, so the cost is amortized O(1), although a single call may drop up to O(L) entries. Implemented with a Redis sorted set, adds and range deletes cost O(log n).
  • Sliding window counter: one multiplication and one addition. O(1).
  • Token bucket: add elapsed time × rate and compare. O(1).
  • Leaky bucket (queue): enqueue and dequeue are each O(1).

Memory per key

AlgorithmState per keySpace
Fixed windowwindow number, counterO(1)
Sliding window logtimestamps of accepted requestsO(L)
Sliding window counterwindow number, two countersO(1)
Token buckettoken count, last update timeO(1)
Leaky bucketqueue of waiting requestsO(queue size)

Multiply by K for the total. With a limit of 10,000 per hour and 100,000 active users, a sliding log may have to hold a billion timestamps in the worst case, while token buckets need just 200,000 numbers. That gap is why large services rarely use the log approach.

def worst_case_bytes(keys: int, limit: int, bytes_per_number: int = 8) -> dict[str, int]:
    return {
        "fixed_window": keys * 2 * bytes_per_number,
        "sliding_log": keys * limit * bytes_per_number,
        "sliding_counter": keys * 3 * bytes_per_number,
        "token_bucket": keys * 2 * bytes_per_number,
    }


for name, size in worst_case_bytes(keys=100_000, limit=10_000).items():
    print(f"{name:16} {size / 2**20:10.1f} MiB")
# sliding_log needs about 7,629 MiB; the others need a few MiB

Real usage is higher because of language and storage overhead, but the orders of magnitude hold.

Accuracy and bursts

对本章有疑问?
上一章Implementation下一章Practice Problems
Algorithm
Most admitted in any window-length span
Burst
Character
Fixed windowup to 2L (at a boundary)up to L at each window startsimplest
Sliding window logexactly Lup to Lexact but heavy
Sliding window counterabout L (approximate)around Llight and practical
Token bucketcapacity + rate × T over a span Tup to capacityaverage rate plus bursts
Leaky bucket (queue)rate × T (output side)absorbed by the queue, output stays evenadds waiting time

The token bucket bound is a simple formula: starting from a full bucket, at most capacity + rate × T requests pass during a span of length T. That is why it is configured with two numbers, such as "10 per second on average, bursts of up to 20".

def max_admitted(rate: float, capacity: float, seconds: float) -> int:
    # Most requests that can pass in `seconds`, starting from a full bucket
    return int(capacity + rate * seconds)


print(max_admitted(rate=10, capacity=20, seconds=1))    # 30
print(max_admitted(rate=10, capacity=20, seconds=60))   # 620

Measuring the boundary problem

This snippet sends requests just before and just after a boundary and counts how many each approach admits.

from collections import deque


def fixed_window(times, limit, window):
    counts = {}
    ok = 0
    for t in times:
        w = int(t // window)
        counts[w] = counts.get(w, 0) + 1
        ok += counts[w] <= limit
    return ok


def sliding_log(times, limit, window):
    log, ok = deque(), 0
    for t in times:
        while log and log[0] <= t - window:
            log.popleft()
        if len(log) < limit:
            log.append(t)
            ok += 1
    return ok


burst = [59.5] * 5 + [60.5] * 5        # five on each side of the boundary
print(fixed_window(burst, 5, 60))      # 10
print(sliding_log(burst, 5, 60))       # 5

The cost of going distributed

When several servers share one limit, every decision needs a round trip to a shared store such as Redis. At that point the network round trip, typically hundreds of microseconds to a few milliseconds, dominates the algorithm's O(1). Bundling several commands into one Lua script keeps it to a single round trip. At very high volume, some systems keep a local bucket on each server and only sync with the shared store periodically, trading a little accuracy for lower latency.

Summary

  • Fixed window, sliding window counter and token bucket are O(1) in time and in space per key.
  • A sliding window log is exact but needs O(L) memory per key, which hurts at scale.
  • A token bucket admits at most capacity + rate × T requests over a span T.
  • Once distributed, network round trips, not the algorithm, set the cost.

0 条评论

热门评论最新优先

登录 · 登录后即可发表评论。