Communitygithub.com

レート制限

トークンバケット、スライディングウィンドウ、ユーザー/IPごとの制限、Redis、Upstashパターン。

レート制限 とは?

このスキルは、APIエンドポイントや認証ルートに対して堅牢なレート制限を実装するためのベストプラクティスとパターンを提供します。トークンバケットアルゴリズムとスライディングウィンドウアルゴリズムを適切に使い分け、APIキーとIPアドレスに基づいた呼び出し元の特定、適切なHTTPヘッダー(Retry-After、X-RateLimit-Remaining)の返却、RedisまたはUpstashを用いた分散ストレージが推奨されています。Next.jsアプリケーション向けのミドルウェア配置や、無料・プロ・エンタープライズといったティアごとの制限設定、Webhookや公開フォームへのレート制限適用など、実践的なガイドラインが含まれています。また、サーバーレス環境でのメモリ内レート制限の危険性や、CDN/エッジでの追加制限の必要性についても言及しています。

対応~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/rate-limiting

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

レート制限 は何をしますか?

ルール

  • APIエンドポイントにはトークンバケット、認証/ログインルートにはスライディングウィンドウを使用する
  • 呼び出し元はAPIキーで特定し、フォールバックとしてIPを使用する — 認証ルートではIPのみに依存しない
  • 429 Too Many Requests と Retry-After ヘッダー(秒)、X-RateLimit-Remaining ヘッダーを返す
  • カウンターはRedis(またはサーバーレス向けUpstash)に保存 — マルチインスタンスデプロイメントではインメモリに保存しない
  • 賢いデフォルト値を設定: 一般APIは100 req/min、認証は5 req/min、読み取りは1000 req/min
  • サーバーレスには @upstash/ratelimit を使用 — Redisのアトミック性とスライディングウィンドウをネイティブに処理する
  • レート制限はミドルウェアとして実装 — グローバルに適用し、ルートごとに厳しい制限でオーバーライドする
  • Next.jsの場合: middleware.ts またはAPIルートハンドラでレート制限を行い、Reactコンポーネント内では行わない
  • ティア(無料、プロ、エンタープライズ)ごとに制限を分ける — ティアはJWTまたはデータベースルックアップに保存する
  • レート制限ヒットを呼び出し元のIDとともにログに記録 — 不正使用パターンを早期に検出する

パターン

// Upstash サーバーレスパターン
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(10, "10 s"),
  analytics: true,
});

const { success, limit, remaining, reset } = await ratelimit.limit(identifier);

避けるべきこと

  • サーバーレスまたはマルチインスタンス設定でのインメモリレート制限 — 呼び出し間で状態が失われる
  • アプリケーション層のみでのレート制限 — CDN/エッジの制限(Cloudflare、Vercel)も追加する
  • 過度に厳しい制限で正規ユーザーをブロックする — 緩やかに開始し、データに基づいて厳しくする
  • Webhookや公開フォームへのレート制限を忘れる

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

関連スキル