API Rate Limiting & Throttling
When to use
Use this skill when you need to protecting backend services from traffic spikes and abuse using token bucket, leaky bucket, and Redis counters.
Prerequisites
- Working knowledge of backend architecture, HTTP protocols, and systems programming.
- Appropriate runtime installed (Node.js, Python, Go, Rust, Java, or .NET depending on stack).
Step-by-step workflow
- Define resource contracts: Establish unambiguous request and response schemas, validation rules, and error envelopes.
- Implement routing & handlers: Structure modular controllers or handlers keeping transport logic isolated from domain services.
- Enforce input validation: Validate and sanitize all inbound parameters, headers, and payloads before reaching business logic.
- Implement robust error handling: Return standardized error responses with consistent HTTP status codes and correlation IDs.
- Optimize resilience & telemetry: Instrument endpoints with structured logging, health checks, metrics, and timeout boundaries.
Commands / code examples
// Example implementation pattern for api-rate-limiting-throttling
import { Request, Response, NextFunction } from 'express';
export interface StandardApiResponse<T> {
success: boolean;
data?: T;
error?: {
code: string;
message: string;
details?: unknown;
};
meta?: {
timestamp: string;
requestId: string;
};
}
export function apiResponseHandler<T>(data: T, req: Request): StandardApiResponse<T> {
return {
success: true,
data,
meta: {
timestamp: new Date().toISOString(),
requestId: (req.headers['x-request-id'] as string) || 'req_auto_gen',
},
};
}
Common mistakes & pitfalls
- Exposing raw database exceptions or internal stack traces directly in client responses.
- Neglecting request timeouts, allowing hung external dependencies to exhaust server connection pools.
- Omitting correlation IDs (
X-Request-ID), making distributed debugging impossible across microservices.
Verification checklist
Related skills
- restful-api-design-best-practices
- api-rate-limiting-throttling
- distributed-tracing-opentelemetry-sdk
- secure-code-review-static-analysis-sast
1---2name: api-rate-limiting-throttling3description: Use this skill when protecting backend services from traffic spikes and abuse using token bucket, leaky bucket, and Redis counters.4metadata:5 source: https://skillmd.com/skills/nextboxis/api-rate-limiting-throttling6---78# API Rate Limiting & Throttling910## When to use11Use this skill when you need to protecting backend services from traffic spikes and abuse using token bucket, leaky bucket, and Redis counters.1213## Prerequisites14- Working knowledge of backend architecture, HTTP protocols, and systems programming.15- Appropriate runtime installed (Node.js, Python, Go, Rust, Java, or .NET depending on stack).1617## Step-by-step workflow181. **Define resource contracts**: Establish unambiguous request and response schemas, validation rules, and error envelopes.192. **Implement routing & handlers**: Structure modular controllers or handlers keeping transport logic isolated from domain services.203. **Enforce input validation**: Validate and sanitize all inbound parameters, headers, and payloads before reaching business logic.214. **Implement robust error handling**: Return standardized error responses with consistent HTTP status codes and correlation IDs.225. **Optimize resilience & telemetry**: Instrument endpoints with structured logging, health checks, metrics, and timeout boundaries.2324## Commands / code examples25```ts26// Example implementation pattern for api-rate-limiting-throttling27import { Request, Response, NextFunction } from 'express';2829export interface StandardApiResponse<T> {30 success: boolean;31 data?: T;32 error?: {33 code: string;34 message: string;35 details?: unknown;36 };37 meta?: {38 timestamp: string;39 requestId: string;40 };41}4243export function apiResponseHandler<T>(data: T, req: Request): StandardApiResponse<T> {44 return {45 success: true,46 data,47 meta: {48 timestamp: new Date().toISOString(),49 requestId: (req.headers['x-request-id'] as string) || 'req_auto_gen',50 },51 };52}53```5455## Common mistakes & pitfalls56- Exposing raw database exceptions or internal stack traces directly in client responses.57- Neglecting request timeouts, allowing hung external dependencies to exhaust server connection pools.58- Omitting correlation IDs (`X-Request-ID`), making distributed debugging impossible across microservices.5960## Verification checklist61- [ ] Endpoints adhere strictly to standardized HTTP status codes and contract specifications.62- [ ] Inbound requests are validated against strict type schemas before processing.63- [ ] Errors return structured envelopes without leaking sensitive infrastructure details.64- [ ] All database queries and external service calls have explicit timeout deadlines.6566## Related skills67- restful-api-design-best-practices68- api-rate-limiting-throttling69- distributed-tracing-opentelemetry-sdk70- secure-code-review-static-analysis-sast