Implement rate limiting middleware with a sliding-window algorithm, reliable client identification, 429 responses with Retry-After, configuration and safe internal bypass.
Implement rate limiting for the API below. Choose the algorithm for the stated limits, identify clients in a way attackers cannot spoof, and return standard responses when a limit is exceeded. ## Inputs API type and framework (REST or GraphQL; for example Express, FastAPI, NestJS): [API_TYPE] Limits per client and window, per route group if different: [LIMITS] Storage (in-memory for one instance, Redis or another shared store for several instances): [STORAGE] How clients are identified (API key, user ID, IP address) and the proxy/CDN chain in front of the app: [CLIENT_IDENTITY] Internal callers that need different limits: [INTERNAL_CALLERS] ## Instructions 1. Choose a sliding-window algorithm: a sliding log (exact, more memory) or a sliding-window counter (approximate, less memory). State the tradeoff for [LIMITS]. 2. Make the check-and-increment atomic in shared storage (for example a Redis Lua script or a transaction) so concurrent requests cannot exceed the limit. 3. Identify clients by authenticated identity where possible. Use IP addresses only as a fallback, and read forwarded-for headers only from proxies you trust. 4. When the limit is exceeded, return 429 with a `Retry-After` header. You may also send rate-limit information headers; note that the IETF `RateLimit` and `RateLimit-Policy` fields are still an Internet-Draft and `X-RateLimit-*` headers are informal conventions, so document what clients should rely on. 5. Give internal services separate limits or an exemption based on authenticated service identity, never on a header a client can set. 6. For GraphQL, consider limiting by query cost rather than request count. 7. Decide fail-open or fail-closed behaviour when the store is unavailable, and log it. 8. Expose configuration (limits per route group, window, identity strategy, store) without code changes. ## Return 1. Middleware code. 2. Storage script or data structure. 3. Client identification function. 4. Response headers and 429 body. 5. Configuration format with defaults. 6. Tests: under the limit, at the limit, window rollover, concurrent requests, internal caller, store outage. ## Acceptance checks - Concurrent requests cannot exceed the limit. - Client identity cannot be changed by setting an unauthenticated header. - 429 responses include `Retry-After`. - Draft or informal headers are labeled as such.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
API: REST on Express 5 running 4 instances behind one load balancer Limits: 100 requests per 60 seconds per API key for /v1/*; 5 login attempts per 15 minutes per account and per IP for /auth/login Storage: Redis 7 Identity: API key in Authorization header; load balancer sets X-Forwarded-For; app trusts only the load balancer Internal callers: billing worker authenticated with a service token
Express middleware backed by a Redis Lua script implementing a sliding-window log (or counter, with the tradeoff stated) that atomically trims, counts and adds; key prefixes per route group; client identity from the API key, falling back to IP with app.set('trust proxy', 1) for the single load balancer; 429 with Retry-After computed from the oldest entry in the window; separate limits for the billing service token; a documented fail-open or fail-closed choice; and the six tests. Illustrative expectation; the sliding-window log arithmetic, including the Retry-After value, was executed locally on synthetic timestamps (see report).Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
Models