Rate limit the public search API

branch · main@8f1c2ab → cursor/rate-limit-search@d4e91f0 · claude-opus-5 (cursor)

One integration partner was issuing ~40 search requests a second, which saturated the read replica and slowed checkout for everyone. This adds a fixed-window limiter in front of /api/search only: 100 requests per minute per API key, 429 with Retry-After when exceeded.

It deliberately does not touch the other routes. The window is stored in Redis with INCR + EXPIRE, so it is shared across the four app instances rather than per-process.

riskunspecified70% confidence
90
Explained
44 of 49 added lines
86%
claimed verified
sourced
8
inferred
1
stubs
0
high risk
1
todos
3
questions
1
  • npm test -- rate-limit
    11 tests, including the boundary at exactly 100
    pass
  • npm run typecheck
    pass
  • npm run test:e2e
    needs a staging Redis; not available on this machine
    skipped
Execution evidence · none recorded

Author confidence and passing claims are not measured accuracy. Records are unauthenticated; source hashes establish freshness, not who ran a command.

No execution records. The percentages above count author claims.

annotations11 of 11 shown · j k to step through

src/lib/rate-limit.ts

+35 −0read-only794%
@@ -0,0 +1,35 @@
1+import { redis } from "@/lib/redis";
2+ 
3+export interface Decision {
4+ allowed: boolean;
5+ remaining: number;
6+ resetSeconds: number;
7+}
8+ 
9+const LIMIT = 100;
10+const WINDOW_SECONDS = 60;
11+ 
12+/**
13+ * Fixed window per key. Two Redis commands per request, pipelined, so the
14+ * limiter costs one round trip.
15+ */
16+export async function take(key: string): Promise<Decision> {
17+ const bucket = `rl:${key}:${Math.floor(Date.now() / 1000 / WINDOW_SECONDS)}`;
18+ const [hits] = await redis
19+ .pipeline()
20+ .incr(bucket)
21+ .expire(bucket, WINDOW_SECONDS)
22+ .exec();
23+ 
24+ const count = Number(hits);
25+ return {
26+ allowed: count <= LIMIT,
27+ remaining: Math.max(0, LIMIT - count),
28+ resetSeconds:
29+ WINDOW_SECONDS - (Math.floor(Date.now() / 1000) % WINDOW_SECONDS),
30+ };
31+}
32+ 
33+export function bucketFor(key: string, at = Date.now()): string {
34+ return `rl:${key}:${Math.floor(at / 1000 / WINDOW_SECONDS)}`;
35+}
intentunspecified95% confidence
intentunspecified90% confidence
intentunspecified90% confidence
intentunspecified85% confidence
assumptionunspecified50% confidence
intentunspecified85% confidence
testunspecified90% confidence

src/app/api/search/route.ts

+14 −2read-only379%
@@ -1,9 +1,14 @@
11 import { NextResponse } from "next/server";
22 import { search } from "@/lib/search";
3+import { take } from "@/lib/rate-limit";
34  
45 export async function GET(request: Request) {
56 const url = new URL(request.url);
67 const query = url.searchParams.get("q") ?? "";
7− const results = await search(query);
8− return NextResponse.json({ results });
8+ const apiKey = request.headers.get("x-api-key");
9+ if (apiKey) {
10+ const decision = await take(apiKey);
11+ if (!decision.allowed) return tooMany(decision.resetSeconds);
12+ }
13+ return NextResponse.json({ results: await search(query) });
914 }
@@ -10,2 +15,9 @@
1015  
1116 export const runtime = "nodejs";
17+ 
18+function tooMany(resetSeconds: number) {
19+ return NextResponse.json(
20+ { error: "rate_limited", retryAfter: resetSeconds },
21+ { status: 429, headers: { "retry-after": String(resetSeconds) } },
22+ );
23+}
noteunspecified90% confidence
securityunspecified80% confidence
intentunspecified95% confidence